#
#                                                                        DRAGONS
#
#
#                                                           transport_request.py
# ------------------------------------------------------------------------------
from os.path import join
from os.path import basename
from pprint  import pformat
from xml.dom import minidom

import urllib.request
import urllib.parse
import urllib.error

from gemini_instruments.common import Section

from gempy.utils import logutils
from . import calurl_dict
# ------------------------------------------------------------------------------
CALURL_DICT   = calurl_dict.calurl_dict
_CALMGR       = CALURL_DICT["CALMGR"]
UPLOADPROCCAL = CALURL_DICT["UPLOADPROCCAL"]
UPLOADCOOKIE  = CALURL_DICT["UPLOADCOOKIE"]
# ------------------------------------------------------------------------------
# sourced from fits_storage.gemini_metadata_utils.cal_types
CALTYPES = [
    "arc",
    "bias",
    "dark",
    # flats
    "flat",
    "domeflat",
    "lampoff_flat",
    "lampoff_domeflat",
    "polarization_flat",
    "qh_flat",
    # masks (use caltype='mask' for MDF queries.)
    "mask",
    "pinhole_mask",
    "ronchi_mask",
    # processed cals
    "processed_arc",
    "processed_bias",
    "processed_dark",
    "processed_flat",
    "processed_fringe",
    "processed_standard",
    "processed_slitillum",
    "slitillum",
    # other ...
    "specphot",
    "spectwilight",
    "astrometric_standard",
    "photometric_standard",
    "telluric_standard",
    "polarization_standard"
]
# -----------------------------------------------------------------------------
RESPONSESTR = """########## Request Data BEGIN ##########
%(sequence)s
########## Request Data END ##########

########## Calibration Server Response BEGIN ##########
%(response)s
########## Calibration Server Response END ##########

########## Nones Report (descriptors that returned None):
%(nones)s
########## Note: all descriptors shown above, scroll up.
        """
# -----------------------------------------------------------------------------
log = logutils.get_logger(__name__)

# -----------------------------------------------------------------------------
def upload_calibration(filename, is_science=False):
    """
    Uploads a calibration file to the FITS Store.

    Parameters
    ----------
    filename: <str>, File to be uploaded

    Returns
    -------
    <void>

    """
    fn = basename(filename)
    url = UPLOADPROCCAL
    if is_science:
        url = url.replace('upload_processed_cal', 'upload_file')
    url = join(url, fn)
    postdata = open(filename, 'rb').read()
    try:
        rq = urllib.request.Request(url)
        rq.add_header('Content-Length', '%d' % len(postdata))
        rq.add_header('Content-Type', 'application/octet-stream')
        rq.add_header('Cookie', 'gemini_fits_upload_auth=%s' % UPLOADCOOKIE)
        u = urllib.request.urlopen(rq, postdata)
        response = u.read()
    except urllib.error.HTTPError as error:
        log.error(str(error))
        raise
    return

def calibration_search(rq, howmany=1, return_xml=False):
    """
    Receives a CalibrationRequest object, encodes the data and make the request
    on the appropriate server. Returns a URL, if any, and the MD5 hash checksum.

    Parameters
    ----------
    rq: <instance>, CalibrationRequest obj

    howmany: <int>
        Maxinum number of calibrations to return

    return_xml: <bool>
        Return the xml message to the caller when no URL is returned from the
        calibration server.

    Returns
    -------
    calurlel, calurlmd5: <list>, <list>
        A tuple of the matching URLs and md5 hash checksums

    """
    rqurl = None
    calserv_msg = None
    CALMGR = _CALMGR
    if rq.caltype not in CALTYPES:
        calserv_msg = "Unrecognised caltype '{}'".format(rq.caltype)
        return (None, calserv_msg)

    rqurl = join(CALMGR, rq.caltype)
    log.stdinfo("CENTRAL CALIBRATION SEARCH: {}".format(rqurl))
    rqurl = rqurl + "/{}".format(rq.filename)
    # encode and send request
    sequence = [("descriptors", rq.descriptors), ("types", rq.tags)]
    postdata = urllib.parse.urlencode(sequence).encode('utf-8')
    response = "CALIBRATION_NOT_FOUND"
    try:
        calRQ = urllib.request.Request(rqurl)
        u = urllib.request.urlopen(calRQ, postdata)
        response = u.read()
    except urllib.error.HTTPError as err:
        log.error(str(err))
        return (None, str(err))
    except urllib.error.URLError as err:
        log.error(str(err))
        return (None, str(err))

    if return_xml:
        return (None, response)

    nones = []
    for dname, dval in list(rq.descriptors.items()):
        if dval is None:
            nones.append(dname)

    preerr = RESPONSESTR % {"sequence": pformat(sequence),
                            "response": response.strip(),
                            "nones"   : ", ".join(nones) \
                            if len(nones) > 0 else "No Nones Sent"}

    try:
        dom = minidom.parseString(response)
        calurlel = [d.childNodes[0].data
                    for d in dom.getElementsByTagName('url')[:howmany]]
        calurlmd5 = [d.childNodes[0].data
                     for d in dom.getElementsByTagName('md5')[:howmany]]
    except IndexError:
        return (None, preerr)

    for url in calurlel:
        log.stdinfo(repr(url))

    return (calurlel, calurlmd5)

def handle_returns(dv):
    # TODO: This sends "old style" request for data section, where the section
    #       is converted to a regular 4-element list. In "new style" requests,
    #       we send the Section as-is. This will need to be revised when
    #       (eventually) FitsStorage upgrades to new AstroData
    if isinstance(dv, list) and isinstance(dv[0], Section):
        return [[el.x1, el.x2, el.y1, el.y2] for el in dv]

    return dv


import http.client
import json
import requests
import re
from application.logger import get_middleware_logger
import math
from database import database
import datetime  # datetime
import time

LOGGER = get_middleware_logger('jijin_http')
myheaders = {'Content-type': 'application/json',"content":"text/html; charset=UTF-8"}
url = "http://vip.stock.finance.sina.com.cn/fund_center/data/jsonp.php/IO.XSRV2.CallbackList['Gb3sH5uawH5WCUZ9']/NetValue_Service.getNetValueOpen?page=1&num=40&sort=nav_date&asc=0&ccode=&type2=2&type3="
size:int=40

def get_api(url):
    """
    :param inputdata: 单个样本的输入参数，是json格式的数据
    :return: 单个样本的探真查询变量结果数据
    """
    # 调用接口
    response = requests.get(url=url, headers=myheaders)
    response.encoding = "UTF-8"
    LOGGER.info(response.json()['weatherinfo'])
    res = response.json()
    # 接口有正确的数据才读入，否则为空
    return res

def get_fund_history(fund_id):
    url = "http://vip.stock.finance.sina.com.cn/fund_center/data/jsonp.php/IO.XSRV2.CallbackList['Gb3sH5uawH5WCUZ9']/NetValue_Service.getNetValueOpen?page="+str(page)+"&num="+str(size)+"&sort=nav_date&asc=0&ccode=&type2=2&type3="
    print("获取page="+str(fund_id))
    print(url)
    response = requests.post(url,   headers = myheaders)
    print(response.text)
    response=re.findall(r"\((.+?)\);",response.text)
    data=response[0]
    print(data)
    res = json.loads(data)['data']
    return res

def get_page_finance(page):
    url = "http://vip.stock.finance.sina.com.cn/fund_center/data/jsonp.php/IO.XSRV2.CallbackList['Gb3sH5uawH5WCUZ9']/NetValue_Service.getNetValueOpen?page="+str(page)+"&num="+str(size)+"&sort=nav_date&asc=0&ccode=&type2=2&type3="
    print("获取page="+str(page))
    print(url)
    # json_foo = json.dumps(inputdata)
    response = requests.post(url,   headers = myheaders)
    print(response.text)
    response=re.findall(r"\((.+?)\);",response.text)
    data=response[0]
    print(data)
    res = json.loads(data)['data']
    return res

def update_fund_date_nav(fund_info):
    sql=" REPLACE  INTO `tianqi`.`fund_date_nav`(`fund_id`, `fund_name`, `per_nav`, `total_nav`, `yesterday_nav`, `nav_rate`, `nav_a`, `nav_date`, `fund_manager`, `fund_type`, `fund_total_num`) " \
        "VALUES ('%(fund_id)s','%(fund_name)s', '%(per_nav)s', '%(total_nav)s', '%(yesterday_nav)s', '%(nav_rate)s', '%(nav_a)s', '%(nav_date)s', '%(fund_manager)s', '%(fund_type)s', '%(fund_total_num)s');"\
        % {'fund_id':fund_info['fund_id'],'fund_name':fund_info['fund_name'],'per_nav':fund_info['per_nav'],'total_nav':fund_info['total_nav']
           ,'yesterday_nav':fund_info['yesterday_nav'],'nav_rate':fund_info['nav_rate'],'nav_a':fund_info['nav_a'],'nav_date':fund_info['nav_date']
           ,'fund_manager':fund_info['fund_manager'],'fund_type':fund_info['fund_type'],'fund_total_num':fund_info['fund_total_num']}
    print("更新：" +sql)
    database.insert(sql)

def update_fund_history_nav(fund_history_info):
    sql=" REPLACE  INTO `tianqi`.`fund_history_nav`(`fund_id`, `fund_date`, `fund_nav`, `fund_total_nav`) " \
        "VALUES ('%(fund_id)s','%(fund_date)s', '%(fund_nav)s', '%(fund_total_nav)s');"\
        % {'fund_id':fund_history_info['fund_id'],'fund_date':fund_history_info['fund_date'],'fund_nav':fund_history_info['fund_nav'],'fund_total_nav':fund_history_info['fund_total_nav']}
    print("更新：" +sql)
    database.insert(sql)

def get_all_finance():
    num=int(get_all_finance_num())
    pages=math.ceil(num/size)+1
    # pages=3
    fund_info={}

    time.sleep(5)
    for i in range(1,pages):
        res=get_page_finance(i)
        print(len(res))
        time.sleep(2)
        for x in res:
            print(x)
            time.sleep(1)
            fund_id=x['symbol']
            fund_info['fund_id']=fund_id
            fund_info['fund_name'] = x['sname']
            fund_info['per_nav'] = x['per_nav']
            fund_info['total_nav'] = x['total_nav']
            fund_info['yesterday_nav'] = x['yesterday_nav']
            fund_info['nav_rate'] = x['nav_rate']
            fund_info['nav_a'] = x['nav_a']
            fund_info['nav_date'] = x['nav_date']
            fund_info['fund_manager'] = x['fund_manager']
            fund_info['fund_type'] = x['jjlx']
            fund_info['fund_total_num'] = x['jjzfe']
            update_fund_date_nav(fund_info)
            get_fund_history(fund_id)
        print("=======")

t = datetime.date.today()
delat=datetime.timedelta(days=15)
format = "%Y-%m-%d"
dateto=t.strftime(format)
datefrom=(t-delat).strftime(format)
def get_finance_history_num(fund_id):
    url = "https://stock.finance.sina.com.cn/fundInfo/api/openapi.php/CaihuiFundInfoService.getNav?callback=jQuery1112006034245173649988_1597501048435&symbol="\
          +str(fund_id)+"&datefrom="+datefrom+"&dateto="+dateto+"&page=1&_=1597501048446"
    response = requests.post(url, headers = myheaders)
    print(response.text)
    response=re.findall(r"\((.+?)\)",response.text)
    print(response[0])
    res = json.loads(response[0])['result']['data']
    print(res)
    return res['total_num']

def get_finance_history_page(fund_id,page):
    url = "https://stock.finance.sina.com.cn/fundInfo/api/openapi.php/CaihuiFundInfoService.getNav?callback=jQuery1112006034245173649988_1597501048435&symbol="\
          +str(fund_id)+"&datefrom="+datefrom+"&dateto="+dateto+"&page="+str(page)+"&_=1597501048446"
    response = requests.post(url, headers = myheaders)
    print(response.text)
    response=re.findall(r"\((.+?)\)",response.text)
    print(response[0])
    res = json.loads(response[0])['result']['data']
    print(res)
    return res['data']
def get_fund_history(fund_id):
    histroy_size=21
    num=int(get_finance_history_num(fund_id))
    pages=math.ceil(num/histroy_size)+1
    fund_history_info={}
    for i in range(1,pages):
        res=get_finance_history_page(fund_id,i)
        print(len(res))
        for x in res:
            print(x)
            fund_history_info['fund_id']=fund_id
            fund_history_info['fund_date'] = x['fbrq']
            fund_history_info['fund_nav'] = x['jjjz']
            fund_history_info['fund_total_nav'] = x['ljjz']
            update_fund_history_nav(fund_history_info)
        print("=======")

def get_all_finance_num():

    inputdata={
        "page": 1,
        "num": 40,
        "sort": "nav_date",
        "asc": 0,
        "type2": 2,
    }
    json_foo = json.dumps(inputdata)
    response = requests.post(url, data =json_foo, headers = myheaders)
    print(response.text)
    response=re.findall(r"\((.+?)\);",response.text)
    print(response[0])
    res = json.loads(response[0])
    print(res['total_num'])
    return res['total_num']

if __name__ == '__main__':
    FORMAT = "%(asctime)s %(thread)d %(message)s"
    # print(get_fund_history("001113"))
    get_all_finance()


import json
from setuptools import setup, find_packages

with open("version.json") as versionf:
    versionJSON = json.load(versionf)

with open("requirements.txt", "r") as require:
    requirements = require.read()

setup(
    name='hacky-vonslacky',
    version=versionJSON["version"],
    install_requires=requirements,
    packages=find_packages(),
    setup_requires=["pytest-runner"],
    tests_require=["pytest"],
)


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

# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of CKAN Private Dataset Extension.

# CKAN Private Dataset Extension is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# CKAN Private Dataset Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with CKAN Private Dataset Extension.  If not, see <http://www.gnu.org/licenses/>.

import ckan.lib.helpers as helpers
import ckan.logic.auth as logic_auth
import ckan.plugins.toolkit as tk
import ckan.new_authz as new_authz
import db

from ckan.common import _, request

@tk.auth_allow_anonymous_access
def package_show(context, data_dict):
    user = context.get('user')
    user_obj = context.get('auth_user_obj')
    package = logic_auth.get_package_object(context, data_dict)

    # datasets can be read by its creator
    if package and user_obj and package.creator_user_id == user_obj.id:
        return {'success': True}

    # Not active packages can only be seen by its owners
    if package.state == 'active':
        # anyone can see a public package
        if user_obj or not package.private:
            return {'success': True}

        # if the user has rights to read in the organization or in the group
        if package.owner_org:
            authorized = new_authz.has_user_permission_for_group_or_org(
                package.owner_org, user, 'read')
        else:
            authorized = False

        # if the user is not authorized yet, we should check if the
        # user is in the allowed_users object
        if not authorized:
            # Init the model
            db.init_db(context['model'])

            # Branch not executed if the database return an empty list
            if db.AllowedUser.get(package_id=package.id, user_name=user):
                authorized = True

        if not authorized:
            # Show a flash message with the URL to acquire the dataset
            # This message only can be shown when the user tries to access the dataset via its URL (/dataset/...)
            # The message cannot be displayed in other pages that uses the package_show function such as
            # the user profile page

            if hasattr(package, 'extras') and 'acquire_url' in package.extras and request.path.startswith('/dataset/')\
                    and package.extras['acquire_url'] != '':
                helpers.flash_notice(_('This private dataset can be acquired. To do so, please click ' +
                                       'here') % package.extras['acquire_url'],
                                     allow_html=True)

            return {'success': False, 'msg': _('User %s not authorized to read package %s') % (user, package.id)}
        else:
            return {'success': True}
    else:
        return {'success': False, 'msg': _('User %s not authorized to read package %s') % (user, package.id)}

def package_update(context, data_dict):
    user = context.get('user')
    user_obj = context.get('auth_user_obj')
    package = logic_auth.get_package_object(context, data_dict)

    # Only the package creator can update it
    if package and user_obj and package.creator_user_id == user_obj.id:
        return {'success': True}

    # if the user has rights to update a dataset in the organization or in the group
    if package and package.owner_org:
        authorized = new_authz.has_user_permission_for_group_or_org(
            package.owner_org, user, 'update_dataset')
    else:
        authorized = False

    if not authorized:
        return {'success': False, 'msg': _('User %s is not authorized to edit package %s') % (user, package.id)}
    else:
        return {'success': True}

@tk.auth_allow_anonymous_access
def resource_show(context, data_dict):
    # This function is needed since CKAN resource_show function uses the default package_show
    # function instead of the one defined in the plugin.
    # A bug is openend in order to be able to remove this function
    # https://github.com/ckan/ckan/issues/1818
    # It's fixed now, so this function can be deleted when the new version is released.
    _model = context['model']
    user = context.get('user')
    resource = logic_auth.get_resource_object(context, data_dict)

    # check authentication against package
    query = _model.Session.query(_model.Package)\
        .join(_model.ResourceGroup)\
        .join(_model.Resource)\
        .filter(_model.ResourceGroup.id == resource.resource_group_id)
    pkg = query.first()
    if not pkg:
        raise tk.ObjectNotFound(_('No package found for this resource, cannot check auth.'))

    pkg_dict = {'id': pkg.id}
    authorized = package_show(context, pkg_dict).get('success')

    if not authorized:
        return {'success': False, 'msg': _('User %s not authorized to read resource %s') % (user, resource.id)}
    else:
        return {'success': True}

@tk.auth_allow_anonymous_access
def package_acquired(context, data_dict):
    # TODO: Improve security
    return {'success': True}

def acquisitions_list(context, data_dict):
    # Users can get only their acquisitions list
    return {'success': context['user'] == data_dict['user']}


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

'''
Created on Mar 30, 2016

@author: icekredit
'''
import socket
import struct

from appliance.Appliance import Appliance
from appliance.ApplianceImpl import ApplianceImpl
from debug.Debug import debug
from msg.Message import Message

class MessageAppliancesInfo(Message):
    '''
        携带所有家电信息的报文，用于所有家电信息显示使用
    '''

    def __init__(self,srcInetAddress,dstInetAddress,targetInetAddress,appliancesInfo=[]):
        self.messageType = Message.MESSAGE_TYPE_APPLIANCES_INFO
        self.srcInetAddress = srcInetAddress
        self.dstInetAddress = dstInetAddress
        self.targetInetAddress = targetInetAddress
        self.appliancesInfo = appliancesInfo

    def __del__(self):
        pass

    def __repr__(self):
        return """MessageAppliancesInfo:{secInetAddress:%s,dstInetAddress:%s,targetInetAddress:%s,appliancesInfo:%s}""" % \
            (self.srcInetAddress,self.dstInetAddress,self.targetInetAddress,self.appliancesInfo)

    def pack(self):
        data = struct.pack("!2s4s4s4s",
                           self.messageType.encode(encoding='utf_8', errors='strict'),
                           socket.inet_aton(self.srcInetAddress),
                           socket.inet_aton(self.dstInetAddress),
                           socket.inet_aton(self.targetInetAddress))

        for applianceInfo in self.appliancesInfo:
            data += applianceInfo.pack()

        return data

    @staticmethod
    def unpack(data):
        messageApplaincesInfoContent = struct.unpack("!2s4s4s4s",data[:14])

        appliancesInfo = []

        currentUnpackPosition = 14
        try:
            while currentUnpackPosition < len(data):
                appliance = ApplianceImpl.unpack(data[currentUnpackPosition:currentUnpackPosition + ApplianceImpl.APPLIANCE_IMPL_INFO_LENGTH])
                appliancesInfo.append(appliance)

#                 if operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_AC):
#                     currentUnpackPosition +=
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_LIGHTER):
#                     pass
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_REFRIGERATOE):
#                     pass
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_SETTOPBOX):
#                     pass
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_SPAN):
#                     pass
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_TV):
#                     pass
#                 elif operator.eq(appliance.applianceType.decode(encoding='utf_8', errors='strict'),Appliance.APPLIANCE_TYPE_OTHER):
#                     pass

                currentUnpackPosition += ApplianceImpl.APPLIANCE_IMPL_INFO_LENGTH
        except Exception as e:
            raise Exception("Sorry,unpack messageAppliancesInfo has encountered a problem!")

        messageAppliancesInfo = MessageAppliancesInfo(
                socket.inet_ntoa(messageApplaincesInfoContent[1]),
                socket.inet_ntoa(messageApplaincesInfoContent[2]),
                socket.inet_ntoa(messageApplaincesInfoContent[3]),
                appliancesInfo)

        return messageAppliancesInfo

def main():
    applianceImpl = ApplianceImpl(Appliance.APPLIANCE_TYPE_TV,"ff:ff:ff:ff:ff:ff",
                                  "192.168.1.1",ApplianceImpl.PRODUCER_UNKNOWN,"   Dell inspron 14r 5437")
    appliancesInfo = []
    appliancesInfo.append(applianceImpl)
    appliancesInfo.append(applianceImpl)
    appliancesInfo.append(applianceImpl)
    appliancesInfo.append(applianceImpl)

    messageAppliancesInfo = MessageAppliancesInfo("192.168.1.5","192.168.1.5","192.168.1.5",
                                                  appliancesInfo)

    debug("before pack")
    debug("%s" % messageAppliancesInfo)

    debug("pack")

    data = messageAppliancesInfo.pack()

    debug("after pack,data: %s" % data)

    debug("unpack")

    messageAppliancesInfo = MessageAppliancesInfo.unpack(data)

    debug("after unapck")
    debug("%s" % messageAppliancesInfo)

if __name__ == "__main__":
    main()


from figures3.base import Figure

class Circle(Figure):
    def __init__(self, radius, **kwargs):
        super().__init__(**kwargs)
        self.radius = radius

    def draw(self, turtle):
        super().draw(turtle)
        self.jump_to(turtle, self.center_x, self.center_y - self.radius)
        turtle.circle(self.radius)
        self.jump_to(turtle, 0, 0)

class Square(Figure):
    def __init__(self, side, **kwargs):
        super().__init__(**kwargs)
        self.side = side

    def draw(self, turtle):
        super().draw(turtle)
        left = self.center_x - self.side / 2
        down = self.center_y - self.side / 2
        self.jump_to(turtle, left, down)
        for _ in range(4):
            turtle.forward(self.side)
            turtle.left(90)
        self.jump_to(turtle, 0, 0)

class Rectangle(Figure):
    def __init__(self, height, width, **kwargs):
        super().__init__(**kwargs)
        self.height = height
        self.width = width

    def draw(self, turtle):
        super().draw(turtle)
        left = self.center_x - self.width / 2
        down = self.center_y - self.height / 2
        self.jump_to(turtle, left, down)
        for _ in range(2):
            turtle.forward(self.width)
            turtle.left(90)
            turtle.forward(self.height)
            turtle.left(90)
        self.jump_to(turtle, 0, 0)

class Pie(Figure):
    def __init__(self, radius, arg_degrees, **kwargs):
        super().__init__(**kwargs)
        self.radius = radius
        self.arg_degrees = arg_degrees

    def draw(self, turtle):
        super().draw(turtle)
        self.jump_to(turtle, self.center_x, self.center_y)
        turtle.begin_fill()
        turtle.forward(self.radius)
        turtle.left(90)
        turtle.circle(self.radius, extent=self.arg_degrees)
        turtle.left(90)
        turtle.forward(self.radius)
        turtle.end_fill()
        self.jump_to(turtle, 0, 0)

class RegularPolygon(Figure):
    def __init__(self, radius, num_sides, **kwargs):
        super().__init__(**kwargs)
        self.radius = radius
        self.num_sides = num_sides

    def draw(self, turtle):
        super().draw(turtle)
        self.jump_to(turtle, self.center_x, self.center_y - self.radius)
        turtle.circle(self.radius, steps=self.num_sides)
        self.jump_to(turtle, 0, 0)


# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2015 Zerbtech
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ds.engine.precheck import exception

class VariableHelper():
    def __init__(self, parameters, template):
        self.params = parameters
        self.template = template

    def var2Value(self, var):
        if var.find('$') == 0:
            var = var[1:]
        if var in self.params.keys():
            return self.params[var]
        elif var in self.template['Parameters'].keys():
            return self.template['Parameters'][var]['Default']
        else:
            raise exception.VariableNotFound(var=var)


from MockData.names_list import last_names, female_names, male_names
import random
import pprint
import json
import MockData.config as config
import pickle
import datetime
import unicodedata
import string
import random
from MockData.config import source_path, data_path

pp = pprint.PrettyPrinter(indent=4)

GENDER_BIAS = 53
last_name = {'last_name' : None, 'case' : None }
first_name = {'given_name' : None, 'case' : None, 'gender' : None, 'ordinal' : 0  }
full_name = { 'first_to_last' : None, 'last_then_first' : None,  'gender' : None,
              'given_names' : [], 'surname' : None }

stateNamexRef = {'Mississippi': 'MS', 'Iowa': 'IA', 'Oklahoma': 'OK',
    'Wyoming': 'WY', 'Minnesota': 'MN', 'Illinois': 'IL', 'Arkansas': 'AR',
    'New Mexico': 'NM', 'Indiana': 'IN', 'Maryland': 'MD', 'Louisiana': 'LA',
    'Texas': 'TX', 'Arizona': 'AZ', 'Wisconsin': 'WI', 'Michigan': 'MI',
    'Kansas': 'KS', 'Utah': 'UT', 'Virginia': 'VA', 'Oregon': 'OR',
    'Connecticut': 'CT', 'Montana': 'MT', 'California': 'CA', 'Massachusetts': 'MA',
    'West Virginia': 'WV', 'Delaware': 'DE', 'New Hampshire': 'NH', 'Vermont': 'VT',
    'Georgia': 'GA', 'North Dakota': 'ND', 'Hawaii': 'HI', 'Pennsylvania': 'PA',
    'Puerto Rico': 'PR', 'Florida': 'FL', 'Alaska': 'AK', 'Kentucky': 'KY',
    'Tennessee': 'TN', 'South Carolina': 'SC', 'Nebraska': 'NE', 'Missouri': 'MO',
    'Ohio': 'OH', 'Alabama': 'AL', 'Rhode Island': 'RI', 'South Dakota': 'SD',
    'Colorado': 'CO', 'Idaho': 'ID', 'New Jersey': 'NJ', 'Washington': 'WA',
    'North Carolina': 'NC', 'New York': 'NY', 'District of Columbia': 'DC',
    'Nevada': 'NV', 'Maine': 'ME'}

stateCodeList = [
    'MS', 'IA', 'OK', 'WY',
    'MN', 'IL', 'AR', 'NM',
    'IN', 'MD', 'LA', 'TX',
    'AZ', 'WI', 'MI', 'KS',
    'UT', 'VA', 'OR', 'CT',
    'MT', 'CA', 'MA', 'WV',
    'DE', 'NH', 'VT', 'GA',
    'ND', 'HI', 'PA', 'FL',
    'AK', 'KY', 'TN', 'SC',
    'NE', 'MO', 'OH', 'AL',
    'RI', 'SD', 'CO', 'ID',
    'NJ', 'WA', 'NC', 'NY',
    'NV', 'ME'
]

sa = open(data_path + 'state-address.json')
state_addresses = json.load(sa)

def gen_last_name(ucase=2, lcase=2,
                  compound_name=False, use_census_distribution=True):
    gen_name = {}

    ln = None
    cn = None
    if use_census_distribution:
        while ln is None:
            x = random.randrange(0, 905540)
            for (k, v) in last_names:
                if x > k:
                    ln = v
    else:
        while ln is None:
            x = random.randrange(len(last_names))
            ln = last_names[x][1]

    if compound_name == True:
        xc = random.randrange(0, 905540)
        # print(x)
        while cn is None:
            # try:
            #     cn = last_names[x]
            # except:
            #     x = x + 1
            #     pass
            if xc in last_names:
                cn = last_names[x]
            else:
                xc += 1
        ln = ln + '-' + cn
    u = random.randrange(0, 100)
    if u < ucase:
        gen_name['last_name'] = ln
        gen_name['case'] = 'u'
    elif u > 100 - lcase:
        gen_name['last_name'] = ln.swapcase()
        gen_name['case'] = 'l'
    else:
        gen_name['last_name'] =  ln.title()
        gen_name['case'] = 'p'
    gen_name['seed'] = x
    x = None
    return gen_name

def gen_first_name(ucase=2, lcase=2, gender=False):
    gen_name = {}
    ln = None
    x = random.randrange(0, 90040)
    while ln is None:
        try:
            if gender == 'f':
                ln = female_names[x]
            else:
                ln = male_names[x]
        except:
            x = x + 1
            pass
    u = random.randrange(0, 100)
    if u < ucase:
       gen_name['given_name'] = ln
       gen_name['case'] = 'u'
    elif u > 100 - lcase:
        gen_name['given_name'] = ln.swapcase()
        gen_name['case'] = 'l'
    else:
        gen_name['given_name'] = ln.title()
        gen_name['case'] = 'p'

    return gen_name

def gen_full_name(case=None, gender=None, gender_bias=GENDER_BIAS,
                  given_names=1, randomize_name_count = True):
    name = {}
    gns = []
    maiden_name = False
    compound_name = False
    if not gender:
        g = random.randrange(0, 99)
        if g <= gender_bias:
            name['gender'] = 'f'
        else:
            name['gender'] = 'm'
    else:
        name['gender'] = gender
    cn = random.randrange(1, 100)
    if cn > 95:
        compound_name = True
    gn = gen_last_name(compound_name = compound_name)
    name['surname'] = gn['last_name']

    name['first_to_last'] = gn['last_name']
    name['last_then_first'] = gn['last_name'] + ','

    if randomize_name_count:
        gnc = random.randrange(1, 100)
        if gender == 'm':
            if gnc < 50:
                given_names = 1
            elif gnc >= 50 and gnc <= 99:
                given_names = 2
            elif gnc == 100:
                given_names = 3
        else:
            if gnc < 70:
                given_names = 1
            elif gnc >= 70 and gnc <= 90:
                given_names = 2
                maiden_name = True
            elif gnc > 90 and gnc < 100:
                given_names = 2
            elif gnc == 100:
                given_names = 3
    names_list = "" # used to store the names.
    for x in range(given_names):
        if maiden_name and x > 0:
            #print 'Maiden'
            mn = gen_last_name(compound_name = False)
            nn =  {'given_name' : None, 'case' : None, 'gender' : None, 'ordinal' : 0  }
            nn['given_name'] = mn['last_name']
            nn['ordinal'] = x + 1
        else:
            nn = gen_first_name(gender=name['gender'])
            nn['ordinal'] = x + 1
        gns.append(nn)
        names_list = names_list + ' ' + nn['given_name']
    name['first_to_last'] = names_list + ' ' + name['surname']
    name['last_then_first'] = name['surname'] + ', ' + names_list.strip()
    name['given_names'] = gns
    gns = []
    return name

def gen_personal_email(first_name, last_name):
    domains = ['gmail.com', 'yahoo.com', 'hotmail.com',
               'icloud.com', 'aol.com', 'outlook.com']

    x = random.randrange(0, len(domains))

    f = random.randrange(0, 2)
    n = ''
    if f == 0:
        n = '{0}.{1}@'.format(first_name, last_name)
    elif f == 1:
        n = '{0}{1}@'.format(first_name[:1], last_name)
    else:
        n = '{0}{1}@'.format(first_name, last_name[:1])

    return n + domains[x]

def gen_address(state=None):
    # pkf = open(config.pickle_path + 'addresses.pkl', 'rb')
    # addresses = pickle.load(pkf)
    # pkf.close()
    # sublist = [address for address in addresses
    #         if address['state'] == state]
    # x = random.randrange(0, len(sublist))
    if state:
        if state in state_addresses:
            sc = len(state_addresses[state])
            x = random.randrange(0, sc - 1)
            # print(state_addresses[state])
            return state_addresses[state][x]
        else:
            raise ValueError('Unknown State Code')
    else:
        scl = random.randrange(0, len(stateCodeList))
        state = stateCodeList[scl]

        x = random.randrange(0, len(state_addresses[state]))
        return state_addresses[state][x]

    return address

def gen_employer(state):
    print(config.pickle_path)
    pkf = open(config.pickle_path +'employers.pkl', "rb")

    employers = pickle.load(pkf)
    pkf.close()

    sublist = [employer for employer in employers
            if employer['state'] == state]
    x = random.randrange(0, len(sublist))
    return sublist[x]

def gen_business_email(first, last, company):
    company = company.replace('&', ' ')
    company = company.replace('  ', ' ')
    y = company.split()
    if len(y) > 3:
        company = y[0] + y[1] #+ y[2]
    elif len(y) > 1:
        company = "".join([part for part in y if part != y[len(y) - 1]])
    else:
        company = y[0]
    company = company.replace(", Inc.", "").replace("Inc." , "").replace("Inc", "").replace('/', '')
    company = company.replace('|', '').replace(',', '').replace(';', '').replace("'", "").strip()
    if len(company) > 30:
        company = company[:20]
    email = gen_personal_email(first, last)
    email = email.split('@')[0] + '@' + company + '.com'
    email = email.replace('..', '.')
    return email

def gen_favorites(gender="f"):
    cuisine = ''
    dessert = ''
    music = ''
    snack = ''
    hobby = ''
    religion = ''
    drink = ''

    cuisines = ['American', 'Italian', 'Mexican', '???', 'French', 'Japanese',
                'Chinese', 'italian', 'Tex/Mex', 'Seafood', 'anything',
                'mexican', '', '', 'none', '' ]
    desserts = ['Apple Pie', 'Ice Cream', '', '', '', '', 'Cheesecake',
                'pumpkin pie', 'Yogurt', 'Fruit', 'Chocolate Cake']
    music_genres = ['Classical', 'jazz', 'Jazz', 'country', 'Country/Western',
                    'punk', 'punk-rock', 'disco', '', '', '', 'Pop', 'rock',
                    'Rock and Roll', '', 'Hip-Hop']
    religions = ['', '', '', '', '', '', 'Baptist', 'Catholic', 'Methodist',
                 'Jewish', 'atheist', 'None', 'Baptist', 'Lutheran', 'Unitarian',
                 'Presbyterian', 'catholic', 'Buddhist' ]
    snacks = ['peanuts', 'twinkies', 'Gummy Bears', "M&M's", 'pretzels', '',
              '', '', '', 'Chocolate Chip Cookies', 'Oreo''s', 'Swedish Fish',
              'Goldfish', 'Chocolate', 'Chocolate', 'chocolate', 'Candy',
              'Snickers', 'Apples', 'Bananas', 'banana', 'chips', 'almonds' ]
    drinks = ['Coffee', 'Coffee', 'coffee', 'tea', 'Tea', 'Red Bull', 'Monster',
              '', '', '', 'Coke', 'Coke Zero', 'Pepsi', 'Diet Coke', 'Coke',
              'Pepsi', 'Water', 'water', 'Orange Juice', 'Espresso', 'Ice Tea',
              'Lemonade']
    cuisine = cuisines[random.randrange(0, len(cuisines))]
    dessert = desserts[random.randrange(0, len(desserts))]
    music = music_genres[random.randrange(0, len(music_genres))]
    religion = religions[random.randrange(0, len(religions))]
    snack = snacks[random.randrange(0, len(snacks))]
    drink = drinks[random.randrange(0, len(drinks))]

    favorites = {'cuisine' : cuisine, 'dessert' : dessert,
                 'music' : music, 'snack' : snack, 'hobby' : hobby,
                 'religion' : religion, 'drink' : drink }

    return favorites

def gen_dates(birth_year=None):
    birthdate = None
    wedding = ''

    if birth_year:
        byear = random.randrange(birth_year - 5, birth_year + 5)
    else:
        byear = random.randrange(1944, 1992)
    birthdate = datetime.date(byear, random.randrange(1,12), random.randrange(1,28))

    wyear = random.randrange(byear + 18, byear + 35)

    if wyear > 2012: wyear  = 2012

    wedding = datetime.date(wyear, random.randrange(1,12), random.randrange(1, 28))

    results = {'birth' : birthdate, 'wedding' : wedding }

    return results

def gen_financials():
    net_worth = random.randrange(5, 33) * 10000
    liquid_assets = net_worth / random.randrange(1, 10)
    annual_income = random.randrange(7, 42) * 5000

    financials = {'net_worth' : net_worth, 'liquid_assets' : liquid_assets, 'annual_income' : annual_income }

    return financials

def gen_school(state):
    colleges = pickle.load(open(config.pickle_path + 'colleges.pkl', 'rb'))
    state_colleges = colleges[state]
    if state_colleges:
        return state_colleges[random.randrange(0, len(state_colleges))]

def gen_phone_number():
    area_code = random.randrange(100, 799)
    phone1 = random.randrange(100, 999)
    phone2 = random.randrange(1000, 9999)
    return str(area_code) + str(phone1) + str(phone2)

def gen_SSN():
    area_code = random.randrange(100, 799)
    phone1 = random.randrange(10, 99)
    phone2 = random.randrange(1000, 9999)
    return str(area_code) + "-" + str(phone1) + "-" + str(phone2)

def gen_bank_account():
    num_len = random.randrange(7, 12)
    x = 1
    for i in range(num_len):
        x = x * 10
    x = x - 1
    account_number = random.randrange(1, x)
    first_letter_seed = 22  #the percentage of account numbers with 1-2 initial letters.
    f = random.randrange(0, 99)
    if f <= first_letter_seed:
        account_number = 'AB' + str(account_number)
    return str(account_number)

def gen_drivers_license():
    num_len = random.randrange(7, 12)
    x = 1
    for i in range(num_len):
        x = x * 10
    x = x - 1
    account_number = random.randrange(1, x)
    first_letter_seed = 22  #the percentage of account numbers with 1-2 initial letters.
    f = random.randrange(0, 99)
    if f <= first_letter_seed:
        account_number = random.choice(string.ascii_letters).upper() + str(account_number)
    if f < (first_letter_seed / 2):
        account_number = random.choice(string.ascii_letters).upper() + str(account_number)
    return str(account_number)

def _luhn_total(num):
    digits = [int(x) for x in str(num)]
    total = sum(digits[-1::-2])
    for digit in digits[-2::-2]:
        digit *= 2
        if digit > 9: digit -= 9
        total += digit
    return total

def luhn_check(num):
    """Returns true if the number has a valid luhn checksum"""
    return _luhn_total(num) % 10 == 0

def luhn_gen(num):
    """Returns the check digit for the given number"""
    return (_luhn_total(num + "0") * 9) % 10

def _cc_range(ranges):
    """Generates a flat list of numbers from a list of individual numbers
        and (start, stop) pairs."""
    o_range = []
    for r in ranges:
        try:
            start, end = r
            o_range.extend(list(range(start, end+1)))
        except TypeError:
            o_range.append(r)
    return o_range

def cc(name, BINs, lengths, checksum=True):
    BINs = _cc_range(BINs)
    lengths = _cc_range(lengths)
    def _cc_gen():
        card_len = random.choice(lengths)
        BIN = str(random.choice(BINs))
        account_num_len = card_len - len(BIN)
        if checksum:
            account_num_len -= 1
        account_num = random.randrange(0, (10 ** (account_num_len + 1)) - 1)
        card_num = BIN + str(account_num)
        if checksum:
            card_num += str(luhn_gen(card_num))
        return card_num
    _cc_gen.__name__ = name
    return _cc_gen

# All information from: https://en.wikipedia.org/wiki/Payment_card_number
CC_TYPES_INACTIVE = [
    cc("Bankcard", [5610, (560221, 560225)], [16]),
    cc("Diners Club enRoute", [2014, 2149], [15], checksum=False),
    cc("Laser", [6304, 6706, 6771, 6709], [(16, 19)]),
    cc("MasterCard", [(2221, 2720)], [16]),
    cc("Solo", [6334, 6767], [16, 18, 19]),
    cc("Switch", [4903, 4905, 4911, 4936, 564182, 633110, 6333, 6759], [16, 18, 19])
]

CC_TYPES_ACTIVE = [
    cc("American Express", [34, 37], [16]),
    cc("China UnionPay", [62], [(16, 19)]),
    cc("Diners Club Carte Blanche", [(300, 305)], [14]),
    cc("Diners Club International",
            [(300, 305), 309, 36, 38, 39], [14]),
    cc("Discover Card",
            [6011, (622126, 622925), (644, 649), 65], [16, 19]),
    cc("InterPayment", [636], [(16, 19)]),
    cc("InstaPayment", [(637,639)], [16]),
    cc("JCB", [(3528, 3589)], [16]),
    cc("Maestro", [50, (56, 69)], [(12, 19)]),
    cc("Dankort", [5019], [16]),
    cc("Dankort + Visa", [4, 4175, 4571], [16]),
    cc("NSPK MIR", [(2200, 2204)], [16]),
    cc("MasterCard", [(51, 55)], [16]),
    cc("Visa", [4], [13, 16, 19]),
    cc("UATP", [1], [15]),
    cc("Verve", [(506099, 506198), (650002, 650027)], [16, 19]),
    cc("CARDGUARD EAD BG ILS", [5392], [16])
]

# There could be many better sampling methods, for example sampling according
# to popular distribution of cards. I imagine there are many more visas than
# "InterPayment" cards, at least in the U.S.
# Even sampling according to the possible number of unique cards would probably
# be better.
def gen_credit_card_number():
    return random.choice(CC_TYPES_ACTIVE)()

if __name__ == '__main__':
    # print('10 random names')
    # for i in range(10):
    #     print(gen_full_name())
    # print('10 names matching distribution')
    for i in range(10):
        print(gen_bank_account())


#coding:utf-8

"""
82. Single Number

Given 2 * n + 1 numbers, every numbers occurs twice except one, find it.
Example

Example 1:

Input：[1,1,2,2,3,4,4]
Output：3
Explanation:
Only 3 appears once

Example 2:

Input：[0,0,1]
Output：1
Explanation:
Only 1 appears once

Challenge

One-pass, constant extra space.
Notice

    n≤100
"""

#异或运算，相同的数字异或运算后为0
class Solution:
    """
    @param A: An integer array
    @return: An integer
    """
    def singleNumber(self, A):
        # write your code here
        if not len(A):
            return None
        size = len(A)
        ret = A[0]
        for i in range(1, size):
            ret = ret^A[i]
        return ret


#ABA_OOP_A.py: very simple object-oriented design example.
# Entry and non-persistent storage of name.

def addressBook():
    aba = ABA_OOP_A()
    aba.go()

class ABA_OOP_A:
    def go(self):
        book = []
        name = input("Enter contact name ('exit' to quit): ")
        while name != "exit":
            book.append(name)
            name = input("Enter contact name ('exit' to quit): ")

        print()
        print("TEST: Display contents of address book")
        print("TEST: The address book contains the following contacts")
        for name in book:
            print(name)


"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.

Add arbitrary yum repo, specified by URL of repo file, to a list of
repos which should be injected into built image by the inject_yum_repo
plugin.

This plugin has to run _BEFORE_ the inject_yum_repo plugin, which
actually places the repo file in the build environment.

Example configuration to add content of repo file at URL:

{
    "name": "add_yum_repo_by_url",
    "args": {
        "repourls": ["http://example.com/myrepo/myrepo.repo"]
    }
}

"""
from atomic_reactor.constants import YUM_REPOS_DIR
from atomic_reactor.plugin import PreBuildPlugin
from atomic_reactor.util import get_retrying_requests_session
from hashlib import md5
import os
import os.path

try:
    # py2
    from urlparse import unquote, urlsplit
    import ConfigParser as configparser
    # We import BytesIO as StringIO as configparser can't properly write
    from io import BytesIO, BytesIO as StringIO
except ImportError:
    # py3
    from urllib.parse import unquote, urlsplit
    import configparser
    from io import BytesIO, StringIO

class YumRepo(object):
    def __init__(self, repourl, dst_repos_dir=YUM_REPOS_DIR):
        self.repourl = repourl
        self.dst_repos_dir = dst_repos_dir
        self.content = None

    @property
    def filename(self):
        '''Returns the filename to be used for saving the repo file.

        The filename is derived from the repo url by injecting a suffix
        after the name and before the file extension. This suffix is a
        partial md5 checksum of the full repourl. This avoids multiple
        repos from being written to the same file.
        '''
        urlpath = unquote(urlsplit(self.repourl, allow_fragments=False).path)
        basename = os.path.basename(urlpath)
        suffix = '-' + md5(self.repourl.encode('utf-8')).hexdigest()[:5]
        final_name = suffix.join(os.path.splitext(basename))
        return final_name

    @property
    def dst_filename(self):
        return os.path.join(self.dst_repos_dir, self.filename)

    def fetch(self):
        session = get_retrying_requests_session()
        response = session.get(self.repourl)
        response.raise_for_status()
        self.content = response.content

    def is_valid(self):
        # Using BytesIO as configparser in 2.7 can't work with unicode
        # see http://bugs.python.org/issue11597
        with BytesIO(self.content) as buf:
            self.config = configparser.ConfigParser()
            try:
                # Try python3 method
                try:
                    self.config.read_string(self.content.decode('unicode_escape'))
                except AttributeError:
                    # Fallback to py2 method
                    self.config.readfp(buf)
            except configparser.Error:
                self.log.warn("Invalid repo file found: '%s'", self.content)
                return False
            else:
                return True

    def set_proxy_for_all_repos(self, proxy_name):
        for section in self.config.sections():
            self.config.set(section, 'proxy', proxy_name)

        with StringIO() as output:
            self.config.write(output)
            self.content = output.getvalue()

class AddYumRepoByUrlPlugin(PreBuildPlugin):
    key = "add_yum_repo_by_url"
    is_allowed_to_fail = False

    def __init__(self, tasker, workflow, repourls=None, inject_proxy=None):
        """
        constructor

        :param tasker: DockerTasker instance
        :param workflow: DockerBuildWorkflow instance
        :param repourls: list of str, URLs to the repo files
        :param inject_proxy: set proxy server for this repo
        """
        # call parent constructor
        super(AddYumRepoByUrlPlugin, self).__init__(tasker, workflow)
        self.repourls = repourls or []
        self.inject_proxy = inject_proxy

    def run(self):
        """
        run the plugin
        """
        if self.repourls:
            for repourl in self.repourls:
                yumrepo = YumRepo(repourl)
                yumrepo.fetch()
                self.log.info("fetched repo from '%s'", yumrepo.repourl)
                if self.inject_proxy:
                    if yumrepo.is_valid():
                        yumrepo.set_proxy_for_all_repos(self.inject_proxy)
                self.workflow.files[yumrepo.dst_filename] = yumrepo.content
                self.log.debug("saving repo '%s', length %d", yumrepo.dst_filename,
                               len(yumrepo.content))


# 🚨 Don't change the code below 👇
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
# 🚨 Don't change the code above 👆

#Write your code below this line 👇
user_height = float(height)
user_weight = int(weight)
# print(type(user_height))
# print(type(user_weight))
bmi = user_weight/(user_height*user_height)
print (int(bmi))


import time
import csv
import datetime
import urllib.request#获取okcoin行情,并储存为CSV文件
import json
import socket
def httpget(url,params='btc_cny'):
    url='https://www.okcoin.cn/api/v1/'+url+'?'+params
    response=urllib.request.urlopen(url,timeout=3)
    d=response.read().decode('utf-8')
    data=json.loads(d)
    return data
def saveData():
    #headers=['dateTime','timeStamp','highPrice','newestPrice','lowPrice','vol']
    timeStamp=int(ticker['date'])
    timeArray=time.localtime(timeStamp)
    dateTime=time.strftime("%Y-%m-%d %H:%M:%S",timeArray)
    timeStamp=ticker['date']
    highPrice=ticker['ticker']['high']
    newestPrice=ticker['ticker']['last']
    lowPrice=ticker['ticker']['low']
    vol=ticker['ticker']['vol']
    rows=[(dateTime,timeStamp,highPrice,newestPrice,lowPrice,vol)]
    filedate=datetime.datetime.now().strftime('%Y-%m-%d')
    with open('%s.csv'%filedate,'a') as f:
        f_csv = csv.writer(f)
        f_csv.writerows(rows)
    print("the newest price is %s"%newestPrice)
    return
while True:
    try:
        ticker=httpget('ticker.do')
        saveData()
        print('数据获取成功')
        time.sleep(5)
    except urllib.error.URLError:
        print('数据获取失败,正在重新获取...urlError')
        time.sleep(3)
    except socket.timeout:
        print('数据获取成功，正在重新获取...socket.time.out')
        time.sleep(3)


import numpy as np
import pandas as pd
from sklearn.manifold import TSNE

csv_data = pd.read_csv('/Users/darmanm/workspace/digitaltwin-experiments/clustering/material_embeddings.csv')
embeddings_data = csv_data.iloc[3:]
metadata = csv_data.iloc[:3]
print(embeddings_data.shape)

print("TSNE Transformation")
tsne = TSNE(n_components=3, n_iter=250)
tsne_result = tsne.fit_transform(embeddings_data)

print("Concating data, writing to CSV")
pd.concat([metadata, tsne_result], axis=1).write_csv('/Users/darmanm/workspace/digitaltwin-experiments/clustering/material_embeddings_tsne.csv')


#!/usr/bin/env python3

from urllib.request import urlopen
from bs4 import BeautifulSoup
from optparse import OptionParser

def main():
    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option("--domain", dest = "domain",
                        help = "待查询的ISP Domain Name")

    (options, args) = parser.parse_args()

    html = urlopen('https://www.spamhaus.org/sbl/listings/' + options.domain)
    bs_obj = BeautifulSoup(html, 'html.parser')
    ip_list = bs_obj.findAll(width="210")

    for x in ip_list:
        print(x.text)

if __name__ == "__main__":
    main()


'''Utilities function for train model module.'''
import pandas as pd

DATASET_DIR = 'dataset/'
DATA_DRIFT_DIR = DATASET_DIR + 'drifts/'
INCOMING_DRIFT_DIR = DATASET_DIR + 'drifts_incoming/'
REGRESSION_DATA_DIR = DATASET_DIR + 'regression/'

OUTPUT_DIR = 'output/'
OUTPUT_NLP_DIR = OUTPUT_DIR + 'nlp_drift/'

def load_data(path):
    '''Loads data from a specific path.'''
    return pd.read_csv(
        path,
        sep='\t',
        names=['label', 'message']
    )


import tkinter as tk
from tkinter import ttk

import warnings
warnings.filterwarnings("ignore")

from gui.timeseries import TimeSeries
from gui.mlp import MultiLayerPerceptron
from gui.supportvectormachine import SupportVectorMachine
from gui.random_forest import RandomForest
from gui.lgbm import LGBM
from gui.catboost_arch import CatBoost
from gui.xg import XGB
from gui.ridge import Ridge
from gui.linear_regression import LinearModel
from gui.generalregression import GeneralRegressionNeuralNetwork
from gui.random_walk import RandomWalk
from gui.sarima import SARIMA
from gui.elm import ELM
from gui.feature_selection import FS
# from gui.montecarlo import MonteCarlo
# from gui.movingaverage import MovingAverage
# from gui.hybrid import Hybrid

class GUI:
    def __init__(self):
        self.gui = tk.Tk()
        self.parent = ttk.Notebook(self.gui)

        time_series = TimeSeries()
        self.add(time_series, "Time Series")

        mlp = MultiLayerPerceptron()
        self.add(mlp, "MLP")

        svm = SupportVectorMachine()
        self.add(svm, "SVM")

        rf = RandomForest()
        self.add(rf, "Random Forest")

        lgbm = LGBM()
        self.add(lgbm, "LightGBM")

        catboost = CatBoost()
        self.add(catboost, "CatBoost")

        xgb = XGB()
        self.add(xgb, "XGBoost")

        ridge = Ridge()
        self.add(ridge, "Ridge Regression")

        lr = LinearModel()
        self.add(lr, "Linear Regression")

        grnn = GeneralRegressionNeuralNetwork()
        self.add(grnn, "GRNN")

        sarima = SARIMA()
        self.add(sarima, "SARIMA")

        elm = ELM()
        self.add(elm, "ELM")

        rw = RandomWalk()
        self.add(rw, "Random Walk")

        fs = FS()
        self.add(fs, "Feature Selection")

        # monte = MonteCarlo()
        # self.add(monte, "Monte Carlo")
        #
        # moving_average = MovingAverage()
        # self.add(moving_average, "Moving Average")
        #
        # hybrid = Hybrid()
        # self.add(hybrid, "Hybrid")

        self.parent.pack(expand=1, fill='both')

    def add(self, frame, text):
        self.parent.add(frame.root, text=text)

    def start(self):
        self.gui.mainloop()

s = GUI()
s.start()


n=int(input())
tot=0
while(n>0):
    dig=n%10
    tot=tot+dig
    n=n//10
temp=tot
rev=0
while(tot>0):
    dig=tot%10
    rev=rev*10+dig
    tot=tot//10
if(temp==rev):
    print("YES")
else:
    print("NO")


"""add light_switch_state to sensor_data

Revision ID: e361748b3d99
Revises: 40efd4b5ddee
Create Date: 2017-06-27 20:41:23.199028

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'e361748b3d99'
down_revision = '40efd4b5ddee'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column(
        'sensor_data',
        sa.Column('light_switch_state', sa.Boolean())
    )

def downgrade():
    op.drop_column('sensor_data', 'light_switch_state')


#! /usr/bin/env python
import logging
log = None
handler = None
LOG_PATH = '../data/log/'
#---------------------------------------
def init(filename):
	global log, handler
	if log and handler : return "Error: log is already initialized. Try log.close() first."
	if type(filename) != str or filename == '' : return "Error: wrong filename"
	log = logging.getLogger();
	log.setLevel(logging.INFO)
	filename = LOG_PATH + str(filename)
	handler = logging.FileHandler(filename)
	handler.setLevel(logging.INFO)
	formatter = logging.Formatter(
		fmt='%(asctime)s %(levelname)s: %(message)s',
		datefmt='%m-%d %H:%M'
		)
	handler.setFormatter(formatter)
	log.addHandler(handler)
	return
#------------------------------------
def msg(message):
	if not message : return "Error: empty message"
	if not log or not handler : return "Error: log is not initialized"
	log.info(message);
	return
#------------------------------------
def close():
	global log,handler
	if not log or not handler : return "Error: log is already closed."
	log.removeHandler(handler)
	log, handler = None, None
	return
#----------------------------------------


import os
import sys
import glob

try:
    from setuptools import setup, Extension, find_packages
except ImportError:
    from distutils.core import setup, Extension, find_packages

HERE = os.path.dirname(os.path.abspath(__file__))

# -*- py3k -*-
is_py3k = sys.version_info[0] == 3
extras = {}
if is_py3k:
    extras["use_2to3"] = True

# -*- Distribution Meta -*-

import re
re_meta = re.compile(r'__(\w+?)__\s*=\s*(.*)')
re_vers = re.compile(r'VERSION\s*=\s*\((.*?)\)')
re_doc = re.compile(r'^"""(.+?)"""')
rq = lambda s: s.strip("\"'")

def add_default(m):
    attr_name, attr_value = m.groups()
    return ((attr_name, rq(attr_value)), )

def add_version(m):
    v = list(map(rq, m.groups()[0].split(", ")))
    return (("VERSION", ".".join(v[0:4]) + "".join(v[4:])), )

def add_doc(m):
    return (("doc", m.groups()[0]), )

pats = {re_meta: add_default,
        re_vers: add_version,
        re_doc: add_doc}
here = os.path.abspath(os.path.dirname(__file__))
meta_fh = open(os.path.join(here, "billiard/__init__.py"))
try:
    meta = {}
    for line in meta_fh:
        if line.strip() == '# -eof meta-':
            break
        for pattern, handler in pats.items():
            m = pattern.match(line.strip())
            if m:
                meta.update(handler(m))
finally:
    meta_fh.close()

# check __version__ in release mode
if len(sys.argv) > 1 and "dist" in sys.argv[1]:
    mp = os.path.join(HERE, "Lib", "billiard", "__init__.py")
    for line in open(mp):
        if line.startswith("__version__"):
            expected = "__version__ = '%s'" % VERSION
            if line.strip() != expected:
                raise ValueError("Version line in %s is wrong: %s\n"
                                 "expected: %s" %
                                 (mp, line.strip(), expected))
            break

if sys.version_info < (2, 4):
    raise ValueError("Versions of Python before 2.4 are not supported")

if sys.platform == 'win32': # Windows
    macros = dict()
    libraries = ['ws2_32']
elif sys.platform.startswith('darwin'): # Mac OSX
    macros = dict(
        HAVE_SEM_OPEN=1,
        HAVE_SEM_TIMEDWAIT=0,
        HAVE_FD_TRANSFER=1,
        HAVE_BROKEN_SEM_GETVALUE=1
        )
    libraries = []
elif sys.platform.startswith('cygwin'): # Cygwin
    macros = dict(
        HAVE_SEM_OPEN=1,
        HAVE_SEM_TIMEDWAIT=1,
        HAVE_FD_TRANSFER=0,
        HAVE_BROKEN_SEM_UNLINK=1
        )
    libraries = []
elif sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
    # FreeBSD's P1003.1b semaphore support is very experimental
    # and has many known problems. (as of June 2008)
    macros = dict(                  # FreeBSD
        HAVE_SEM_OPEN=0,
        HAVE_SEM_TIMEDWAIT=0,
        HAVE_FD_TRANSFER=1,
        )
    libraries = []
elif sys.platform.startswith('openbsd'):
    macros = dict(                  # OpenBSD
        HAVE_SEM_OPEN=0,            # Not implemented
        HAVE_SEM_TIMEDWAIT=0,
        HAVE_FD_TRANSFER=1,
        )
    libraries = []
else:                                   # Linux and other unices
    macros = dict(
        HAVE_SEM_OPEN=1,
        HAVE_SEM_TIMEDWAIT=1,
        HAVE_FD_TRANSFER=1
        )
    libraries = ['rt']

if sys.platform == 'win32':
    multiprocessing_srcs = ['Modules/_billiard/multiprocessing.c',
                            'Modules/_billiard/semaphore.c',
                            'Modules/_billiard/pipe_connection.c',
                            'Modules/_billiard/socket_connection.c',
                            'Modules/_billiard/win32_functions.c'
                           ]
else:
    multiprocessing_srcs = ['Modules/_billiard/multiprocessing.c',
                            'Modules/_billiard/socket_connection.c'
                           ]

    if macros.get('HAVE_SEM_OPEN', False):
        multiprocessing_srcs.append('Modules/_billiard/semaphore.c')

is_jython = sys.platform.startswith("java")
is_pypy = hasattr(sys, "pypy_version_info")
extensions = []
if not (is_jython or is_pypy or is_py3k):
    extensions = [
        Extension('_billiard',
                sources=multiprocessing_srcs,
                define_macros=macros.items(),
                libraries=libraries,
                include_dirs=["Modules/_billiard"],
                depends=(glob.glob('Modules/_billiard/*.h') +
                        ['setup.py'])
                ),
        ]

long_description = open(os.path.join(HERE, 'README.rst')).read()
long_description += """

===========
Changes
===========

"""
long_description += open(os.path.join(HERE, 'CHANGES.txt')).read()

setup(
    name='billiard',
    version=meta["VERSION"],
    description=meta["doc"],
    long_description=long_description,
    packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']),
    ext_modules=extensions,
    author=meta["author"],
    author_email=meta["author_email"],
    maintainer=meta["maintainer"],
    maintainer_email=meta["contact"],
    url=meta["homepage"],
    zip_safe=False,
    license='BSD',
    tests_require = ["nose", "nose-cover3"],
    test_suite="nose.collector",
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'Programming Language :: Python',
        'Programming Language :: C',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX',
        'License :: OSI Approved :: BSD License',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    **extras
)


from __future__ import division

import os
import time
import numpy as np
import tensorflow as tf

from ops import batch_norm, lrelu, conv2d, dilated_conv2d, conv2d_transpose, linear
from utils import dataset_files, get_image

D_ITERATIONS = 4

class WGANTensor(object):
    def __init__(self,
                 sess,
                 learning_rate=0.0001,
                 beta1=0.0,
                 beta2=0.5,
                 batch_size=32,
                 image_size=64,
                 lam=100,
                 checkpoint_dir=None):
        self.sess = sess
        self.batch_size = batch_size
        self.image_size = image_size
        self.image_shape = [image_size, image_size, 3]
        self.lam = lam
        self.checkpoint_dir = checkpoint_dir
        self.global_step = tf.Variable(0, trainable=False)
        self.d_bns = [batch_norm(name='d_bn{}'.format(i)) for i in range(10)]
        self.g_bns = [batch_norm(name='g_bn{}'.format(i, )) for i in range(15)]

        self.images = tf.placeholder(tf.float32, [batch_size] + self.image_shape, name='images')
        self.masks = tf.placeholder(tf.float32, [batch_size] + self.image_shape, name='masks')
        self.masked_images = tf.multiply(self.images, self.masks)
        self.G = self.generator(self.masked_images)
        self.G_final = self.masked_images + tf.multiply(self.G, (1 - self.masks))

        self.d_loss_real = tf.reduce_mean(self.discriminator(self.images))
        self.d_loss_fake = tf.reduce_mean(self.discriminator(self.G, reuse=True))
        self.d_loss = self.d_loss_fake - self.d_loss_real
        self.g_loss_mse = tf.reduce_mean(tf.contrib.layers.flatten(tf.square(self.G - self.images)))
        self.g_loss = self.lam * self.g_loss_mse - self.d_loss_fake

        self.epsilon = tf.random_uniform([], 0.0, 1.0)
        self.G_epsilon = self.epsilon * self.images + (1 - self.epsilon) * self.G
        self.D_epsilon = self.discriminator(self.G_epsilon, reuse=True)
        self.gradients = tf.gradients(self.D_epsilon, self.G_epsilon)[0]
        self.slopes = tf.sqrt(tf.reduce_sum(tf.square(self.gradients), reduction_indices=[1]))
        self.gradient_penalty = 100.0 * tf.reduce_mean((self.slopes - 1.0) ** 2)
        self.d_loss += self.gradient_penalty

        self.d_loss_real_summary = tf.summary.scalar("d_loss_real", self.d_loss_real)
        self.d_loss_fake_summary = tf.summary.scalar("d_loss_fake", self.d_loss_fake)
        self.gradient_penalty_summary = tf.summary.scalar("d_loss_gp", self.gradient_penalty)
        self.d_loss_summary = tf.summary.scalar("d_loss", self.d_loss)
        self.d_summary = tf.summary.merge([
            self.d_loss_real_summary, self.d_loss_fake_summary, self.gradient_penalty_summary, self.d_loss_summary])

        self.masked_images_summary = tf.summary.image("masked_image", self.masked_images)
        self.G_summary = tf.summary.image("g", self.G)
        self.G_final_summary = tf.summary.image("g_final", self.G_final)
        self.g_loss_mse_summary = tf.summary.scalar("g_mse", self.g_loss_mse)
        self.g_loss_summary = tf.summary.scalar("g", self.g_loss)
        self.g_summary = tf.summary.merge([
            self.G_summary, self.masked_images_summary, self.G_final_summary,
            self.g_loss_mse_summary, self.g_loss_summary])

        t_vars = tf.trainable_variables()
        self.d_vars = [var for var in t_vars if 'd_' in var.name]
        self.g_vars = [var for var in t_vars if 'g_' in var.name]
        self.d_optim = tf.train.AdamOptimizer(learning_rate, beta1=beta1, beta2=beta2).minimize(
            self.d_loss, var_list=self.d_vars, global_step=self.global_step)
        self.g_optim = tf.train.AdamOptimizer(learning_rate, beta1=beta1, beta2=beta2).minimize(
            self.g_loss, var_list=self.g_vars)

        self.writer = tf.summary.FileWriter(os.path.join(self.checkpoint_dir, "logs"), self.sess.graph)
        self.saver = tf.train.Saver(max_to_keep=10)

    def train(self, config):
        tf.global_variables_initializer().run()
        start_time = time.time()
        if self.load(self.checkpoint_dir):
            print("""Loading existing model""")
        else:
            print("""Initalizing new model""")

        for epoch in range(config.epoch):
            data = dataset_files(config.dataset)
            np.random.shuffle(data)
            batch_idx = min(len(data), config.train_size) // self.batch_size
            for idx in range(batch_idx):
                batch_files = data[idx * config.batch_size: (idx + 1) * config.batch_size]
                batch = [get_image(batch_file, self.image_size) for batch_file in batch_files]
                batch_images = np.array(batch).astype(np.float32)
                masks = np.ones([config.batch_size, config.image_size, config.image_size, 3])
                masks[:, 16:48, 16:48, :] = 0.0

                _, d_loss, summary_str = self.sess.run(
                    [self.d_optim, self.d_loss, self.d_summary],
                    feed_dict={self.images: batch_images, self.masks: masks})
                self.writer.add_summary(summary_str, self.global_step.eval())

                if idx > 0 and idx % D_ITERATIONS == D_ITERATIONS - 1:
                    _, g_loss, summary_str = self.sess.run(
                        [self.g_optim, self.g_loss, self.g_summary],
                        feed_dict={self.images: batch_images, self.masks: masks})
                    self.writer.add_summary(summary_str, self.global_step.eval())
                    print("Epoch: [{:2d}] [{:4d}/{:4d}] time: {:4.4f}, d_loss: {:.8f}, g_loss: {:.8f}".format(
                        epoch, idx, batch_idx, time.time() - start_time, d_loss, g_loss))

                if idx % 100 == 0:
                    self.save(config.checkpoint_dir, self.global_step.eval())

    def generator(self, images):
        with tf.variable_scope("generator"):
            g_h0 = tf.nn.relu(conv2d(images, 64, 5, 5, 1, 1, name='g_h0'))
            g_h1 = tf.nn.relu(self.g_bns[0](conv2d(g_h0, 128, 3, 3, 2, 2, name='g_h1')))
            g_h2 = tf.nn.relu(self.g_bns[1](conv2d(g_h1, 128, 3, 3, 1, 1, name='g_h2')))
            g_h3 = tf.nn.relu(self.g_bns[2](conv2d(g_h2, 256, 3, 3, 2, 2, name='g_h3')))
            g_h4 = tf.nn.relu(self.g_bns[3](conv2d(g_h3, 256, 3, 3, 1, 1, name='g_h4')))
            g_h5 = tf.nn.relu(self.g_bns[4](conv2d(g_h4, 256, 3, 3, 1, 1, name='g_h5')))
            g_h6 = tf.nn.relu(self.g_bns[5](dilated_conv2d(g_h5, 256, 3, 3, 2, name='g_h6')))
            g_h7 = tf.nn.relu(self.g_bns[6](dilated_conv2d(g_h6, 256, 3, 3, 4, name='g_h7')))
            g_h8 = tf.nn.relu(self.g_bns[7](dilated_conv2d(g_h7, 256, 3, 3, 8, name='g_h8')))
            g_h9 = tf.nn.relu(self.g_bns[8](dilated_conv2d(g_h8, 256, 3, 3, 16, name='g_h9')))
            g_h10 = tf.nn.relu(self.g_bns[9](conv2d(g_h9, 256, 3, 3, 1, 1, name='g_h10')))
            g_h11 = tf.nn.relu(self.g_bns[10](conv2d(g_h10, 256, 3, 3, 1, 1, name='g_h11')))
            g_h12 = tf.nn.relu(self.g_bns[11](conv2d_transpose(
                g_h11, [self.batch_size, int(self.image_size/2), int(self.image_size/2), 128], 4, 4, 2, 2, name='g_h12')))
            g_h13 = tf.nn.relu(self.g_bns[12](conv2d(g_h12, 128, 3, 3, 1, 1, name='g_h13')))
            g_h14 = tf.nn.relu(self.g_bns[13](conv2d_transpose(
                g_h13, [self.batch_size, self.image_size, self.image_size, 64], 4, 4, 2, 2, name='g_h14')))
            g_h15 = tf.nn.relu(self.g_bns[14](conv2d(g_h14, 32, 3, 3, 1, 1, name='g_h15')))
            g_h16 = tf.nn.tanh(conv2d(g_h15, 3, 3, 3, 1, 1, name='g_h16'))
            return g_h16

    def discriminator(self, images, reuse=False):
        with tf.variable_scope('discriminator', reuse=reuse):
            gd_h0 = lrelu(conv2d(images, 64, name="d_gd_h0_conv")) # 32*32*64
            gd_h1 = lrelu(self.d_bns[0](conv2d(gd_h0, 128, name='d_gd_h1_conv'))) # 16*16*128
            gd_h2 = lrelu(self.d_bns[1](conv2d(gd_h1, 256, name='d_gd_h2_conv'))) # 8*8*256
            gd_h3 = lrelu(self.d_bns[2](conv2d(gd_h2, 512, name='d_gd_h3_conv'))) # 4*4*512
            gd_fc0 = linear(tf.reshape(gd_h3, [self.batch_size, int(4 * 4 * 512)]), 1024, 'd_gd_fc0')
            gd_fc1 = linear(gd_fc0, 1, 'd_gd_fc1')
            return gd_fc1

    def save(self, checkpoint_dir, step):
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)
        self.saver.save(self.sess,
                        os.path.join(checkpoint_dir, "wgan_tensor"),
                        global_step=step)

    def load(self, checkpoint_dir):
        print(" [*] Reading checkpoints...")
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            self.saver.restore(self.sess, ckpt.model_checkpoint_path)
            return True
        else:
            return False

flags = tf.app.flags
flags.DEFINE_integer("epoch", 25, "Epoch to train [25]")
flags.DEFINE_float("learning_rate", 0.0001, "Learning rate of for adam [0.0001]")
flags.DEFINE_float("beta1", 0.0, "Momentum term of adam [0.0]")
flags.DEFINE_float("beta2", 0.9, "Beta 2 of adam [0.9]")
flags.DEFINE_integer("train_size", np.inf, "The size of train images [np.inf]")
flags.DEFINE_integer("batch_size", 64, "The size of batch images [32]")
flags.DEFINE_integer("image_size", 64, "The size of image to use")
flags.DEFINE_string("dataset", "lfw-aligned-64", "Dataset directory.")
flags.DEFINE_string("checkpoint_dir", "checkpoint", "Directory name to save the checkpoints [checkpoint]")
flags.DEFINE_string("sample_dir", "samples", "Directory name to save the image samples [samples]")
FLAGS = flags.FLAGS

if not os.path.exists(FLAGS.checkpoint_dir):
    os.makedirs(FLAGS.checkpoint_dir)
if not os.path.exists(FLAGS.sample_dir):
    os.makedirs(FLAGS.sample_dir)

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
    model = WGANTensor(
        sess,
        learning_rate=FLAGS.learning_rate,
        beta1=FLAGS.beta1,
        beta2=FLAGS.beta2,
        image_size=FLAGS.image_size,
        batch_size=FLAGS.batch_size,
        checkpoint_dir=FLAGS.checkpoint_dir)
    model.train(FLAGS)


import json
import datetime
import math

from airflow import DAG
from airflow.models import Variable
from airflow.contrib.kubernetes import secret
from airflow.contrib.operators import kubernetes_pod_operator
from airflow.operators.python_operator import PythonOperator
from helper_dag import generate_splitted_batches_for_audio_analysis

audio_analysis_config = json.loads(Variable.get("audio_analysis_config"))
source_path = Variable.get("sourcepathforaudioanalysis")
destination_path = Variable.get("destinationpathforaudioanalysis")
bucket_name = Variable.get("bucket")
env_name = Variable.get("env")
composer_namespace = Variable.get("composer_namespace")
project = Variable.get("project")

resource_limits = json.loads(Variable.get("audio_analysis_resource_limits"))

YESTERDAY = datetime.datetime.now() - datetime.timedelta(days=1)
LANGUAGE_CONSTANT = "{language}"

secret_file = secret.Secret(
    deploy_type="volume",
    deploy_target="/tmp/secrets/google",
    secret="gc-storage-rw-key",
    key="key.json",
)

def interpolate_language_paths(path, language):
    path_set = path.replace(LANGUAGE_CONSTANT, language)
    return path_set

def create_dag(dag_id, dag_number, default_args, args, max_records_threshold_per_pod):
    dag = DAG(
        dag_id,
        schedule_interval=datetime.timedelta(days=1),
        default_args=default_args,
        start_date=YESTERDAY,
    )

    with dag:
        audio_format = args.get("audio_format")
        language = args.get("language")
        parallelism = args.get("parallelism")
        source = args.get("source")
        print(args)
        print(f"Language for source is {language}")
        source_path_set = interpolate_language_paths(source_path, language)
        destination_path_set = interpolate_language_paths(destination_path, language)
        generate_batch_files = PythonOperator(
            task_id=dag_id + "_generate_batches",
            python_callable=generate_splitted_batches_for_audio_analysis,
            op_kwargs={
                "source": source,
                "source_path": source_path_set,
                "destination_path": destination_path_set,
                "max_records_threshold_per_pod": max_records_threshold_per_pod,
                "audio_format": audio_format,
                "bucket_name": bucket_name,
            },
            dag_number=dag_number,
        )

        generate_batch_files

        data_audio_analysis_task = kubernetes_pod_operator.KubernetesPodOperator(
            task_id=f"data-audio-analysis-{source}",
            name="data-audio-analysis",
            cmds=[
                "python",
                "invocation_script.py",
                "-b",
                bucket_name,
                "-a",
                "audio_analysis",
                "-rc",
                f"data/audiotospeech/config/config.yaml",
                "-as",
                source,
                "-l",
                language,
            ],
            namespace=composer_namespace,
            startup_timeout_seconds=300,
            secrets=[secret_file],
            image=f"us.gcr.io/{project}/ekstep_data_pipelines:{env_name}_1.0.0",
            image_pull_policy="Always",
            resources=resource_limits,
        )

        batch_file_path_dict = json.loads(Variable.get("embedding_batch_file_list"))
        list_of_batch_files = batch_file_path_dict[source]
        if len(list_of_batch_files) > 0:
            total_phases = math.ceil(len(list_of_batch_files) / parallelism)
            task_dict = {}
            for phase in range(0, total_phases):
                for pod in range(1, parallelism + 1):
                    if len(list_of_batch_files) > 0:
                        batch_file = list_of_batch_files.pop()
                        task_dict[
                            f"create_embedding_task_{pod}_{phase}"] = kubernetes_pod_operator.KubernetesPodOperator(
                            task_id=source + "_create_embedding_" + str(pod) + '_' + str(phase),
                            name="create-embedding",
                            cmds=[
                                "python",
                                "invocation_script.py",
                                "-b",
                                bucket_name,
                                "-a",
                                "audio_embedding",
                                "-rc",
                                f"data/audiotospeech/config/config.yaml",
                                "-fp",
                                batch_file,
                                "-as",
                                source,
                                "-l",
                                language,
                            ],
                            namespace=composer_namespace,
                            startup_timeout_seconds=300,
                            secrets=[secret_file],
                            image=f"us.gcr.io/{project}/ekstep_data_pipelines:{env_name}_1.0.0",
                            image_pull_policy="Always",
                            resources=resource_limits,
                        )
                        if phase == 0:
                            generate_batch_files >> task_dict[
                                f"create_embedding_task_{pod}_{phase}"] >> data_audio_analysis_task
                        # elif phase == total_phases - 1:
                        #     task_dict[f"create_embedding_task_{pod}_{phase - 1}"] >> task_dict[
                        #         f"create_embedding_task_{pod}_{phase}"] >> data_audio_analysis_task
                        else:
                            task_dict[f"create_embedding_task_{pod}_{phase - 1}"] >> task_dict[
                                f"create_embedding_task_{pod}_{phase}"] >> data_audio_analysis_task
            # if phase == total_phases - 1:
            #     task_dict[f"create_embedding_task_{last_pod_no}_{phase}"] >> data_audio_analysis_task
        else:
            generate_batch_files >> data_audio_analysis_task

    return dag

for source in audio_analysis_config.keys():
    source_info = audio_analysis_config.get(source)

    max_records_threshold_per_pod = source_info.get("batch_size")
    parallelism = source_info.get("parallelism", 5)
    audio_format = source_info.get("format")
    language = source_info.get("language").lower()

    dag_id = source + '_' + 'audio_embedding_analysis'

    dag_args = {
        "email": ["soujyo.sen@thoughtworks.com"],
    }

    args = {
        "audio_format": audio_format,
        "parallelism": parallelism,
        "language": language,
        "source": source
    }

    # schedule = '@daily'

    dag_number = dag_id + str(max_records_threshold_per_pod)

    globals()[dag_id] = create_dag(dag_id, dag_number, dag_args, args, max_records_threshold_per_pod)


"""
Faça um programa que calcule as raízes de uma equação do segundo grau, na forma ax2 + bx + c.
O programa deverá pedir os valores de a, b e c e fazer as consistências, informando ao usuário nas seguintes situações:
a) Se o usuário informar o valor de A igual a zero, a equação não é do segundo grau e o programa não deve fazer pedir os
demais valores, sendo encerrado;
b) Se o delta calculado for negativo, a equação não possui raizes reais. Informe ao usuário e encerre o programa;
c) Se o delta calculado for igual a zero a equação possui apenas uma raiz real; informe-a ao usuário;
d) Se o delta for positivo, a equação possui duas raiz reais; informe-as ao usuário;
"""
import math

print('digite os termos a, b, c da equação ax² + bx + c')

a = int(input('digite o termo a = '))
if a == 0:
    print('Não é uma equação do segundo grau')
else:
    b = int(input('digite o termo b = '))
    c = int(input('digite o termo c = '))
    delta = b*b - 4*a*c

    if delta < 0:
        print('A equação não possui raízes reais')
    if delta == 0:
            print('Delta =',  delta , '\nA equação possui apenas uma raiz real')

            raiz = ((-1)*b + delta/2)

            print('A raiz da equação é =', raiz)
    if delta > 0:
            print('Delta =', delta, 'A equação possui duas raizes reais')
            raiz1 = (-b + math.sqrt(delta)) / (2*a)
            raiz2 = (-b - math.sqrt(delta)) / (2*a)
            print('A primeira raiz é = ', raiz1)
            print('A segunda raiz é = ', raiz2)


# author:2632158294@qq.com
# github:https://github.com/PyCN/algorithm/tree/master/leetcode/3
# time: O(n)

class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        flag, length = {}, len(s)
        p, res = 0, 0
        for i in range(length):
            if s[i] not in flag:
                flag[s[i]] = True
            elif flag[s[i]]:
                while s[p] != s[i]:
                    flag[s[p]] = False
                    p += 1
                p += 1
            else:
                flag[s[i]] = True
            res = max(res, i - p + 1)
        return res


from marshmallow import fields, Schema

import models

class ItemSchema(Schema):
    id = fields.Int(dump_only=True)
    name = fields.Str()
    description = fields.Str()
    complete = fields.Boolean()

def init_app(app):
    app.add_url_rule('/v1/items', handler=get_items, method='GET')
    app.add_url_rule('/v1/items', handler=create_item, method='POST')
    app.add_url_rule('/v1/items/<int:id>', handler=update_item, method='PUT')
    app.add_url_rule('/v1/items/<int:id>', handler=delete_item, method='DELETE')
    app.add_url_rule('/v1/items/<int:id>', handler=get_item_by_id, method='GET')

def get_items(session, **kwargs):
    query_params = kwargs.get('querystring', {})
    items = models.get_items(session, incomplete_only=query_params.get('exclude_complete', True))

    items = ItemSchema(many=True).dump(items)

    return {'items': items.data}

def create_item(session, **kwargs):
    body = kwargs.get('body', {})
    item_kwargs, errors = ItemSchema().load(body)

    if errors:
        raise Exception

    item = models.create_item(session, **item_kwargs)
    session.commit()

    item = ItemSchema().dump(item)

    return {'item': item.data}

def get_item_by_id(session, id, **kwargs):
    item = models.get_items(session, id=id, incomplete_only=False).first()
    if not item:
        raise Exception

    item = ItemSchema().dump(item)

    return {'item': item.data}

def update_item(session, id, **kwargs):
    body = kwargs.get('body', {})
    item_kwargs, errors = ItemSchema().load(body)
    if errors:
        raise Exception

    item = models.update_item(session, id, **item_kwargs)
    if not item:
        raise Exception

    session.commit()

    item = ItemSchema().dump(item)

    return {'item': item.data}

def delete_item(session, id, **kwargs):
    item = models.delete_item(session, id)
    if not item:
        raise Exception

    session.commit()

    item = ItemSchema().dump(item)

    return {'item': item.data}


# coding: utf-8

# In[1]:

import pandas as pd
import os
import matplotlib.pyplot as plt
import statsmodels.api as sm
os.chdir('d:/')
import numpy as np

# In[2]:

df=pd.read_csv('wcs crude cadaud.csv',encoding='utf-8')

# In[3]:

df.set_index('date',inplace=True)

# In[4]:

df.index=pd.to_datetime(df.index,format='%m/%d/%Y')

# In[5]:

df=df.reindex(columns=
['cny',
 'gbp',
 'usd',
 'eur',
 'krw',
 'mxn',
 'gas',
 'wcs',
 'edmonton',
 'wti',
 'gold',
 'jpy',
 'cad'])

# In[6]:

var=locals()

for i in df.columns:
    if i!='cad':
            x=sm.add_constant(df[i])
            y=df['cad']
            m=sm.OLS(y,x).fit()
            var[str(i)]=m.rsquared

ax=plt.figure(figsize=(10,5)).add_subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

width=0.7
colorlist=['#9499a6','#9499a6','#9499a6','#9499a6',
           '#9499a6','#9499a6','#9499a6','#582a20',
           '#be7052','#f2c083','#9499a6','#9499a6']

temp=list(df.columns)
for i in temp:
    if i!='cad':
        plt.bar(temp.index(i)+width,
            var[str(i)],width=width,label=i,
               color=colorlist[temp.index(i)])

plt.title('Regressions on Loonie')
plt.ylabel('R Squared\n')
plt.xlabel('\nRegressors')
plt.xticks(np.arange(len(temp))+width,
           ['Yuan', 'Sterling', 'Dollar', 'Euro', 'KRW',
             'MXN', 'Gas', 'WCS', 'Edmonton',
             'WTI', 'Gold', 'Yen'],fontsize=10)
plt.show()

# In[7]:

ax=plt.figure(figsize=(10,5)).add_subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

(df['cny']/df['cny'].iloc[0]).plot(c='#283670',label='Yuan')
(df['gbp']/df['gbp'].iloc[0]).plot(c='#fe4d32',label='Sterling')
(df['cad']/df['cad'].iloc[0]).plot(c='#484a25',label='Loonie')
plt.legend(loc=0)
plt.xlabel('Date')
plt.ylabel('Normalized Value by 100')
plt.title('Loonie vs Yuan vs Sterling')
plt.show()

# In[8]:

ax=plt.figure(figsize=(10,5)).add_subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

(df['wti']/df['wti'].iloc[0]).plot(c='#2a78b2',label='WTI',alpha=0.5)
(df['wcs']/df['wcs'].iloc[0]).plot(c='#7b68ee',label='WCS',alpha=0.5)
(df['edmonton']/df['edmonton'].iloc[0]).plot(c='#110b3c',
                                             label='Edmonton',alpha=0.5)
(df['cad']/df['cad'].iloc[0]).plot(c='#cb8b8b',label='Loonie')
plt.legend(loc=0)
plt.xlabel('Date')
plt.ylabel('Normalized Value by 100')
plt.title('Loonie vs Crude Oil Blends')
plt.show()


# -*- coding:utf-8 -*-
import os
from cloud import app
from flask_wtf import FlaskForm
from wtforms import SubmitField
from flask import request,flash,render_template,redirect
from flask_wtf.file import FileField, FileRequired, FileAllowed
from flask_uploads import UploadSet, configure_uploads, IMAGES,DATA ,\
 patch_request_class

FLASKR_SETTINGS=os.path.join(app.root_path, 'setting.cfg')
app.config.from_pyfile(FLASKR_SETTINGS, silent=True)
pathReal=app.config['UPLOADED_DATA_DEST']

Data_list = os.listdir(pathReal) #文件位置
Data_file = UploadSet('data', DATA ) #指定文件类型

configure_uploads(app, Data_file)
patch_request_class(app) #文件大小

class UploadForm(FlaskForm):
    file = FileField(validators=[FileAllowed(Data_file, u'Data_file Only!'), \
            FileRequired(u'Choose a file!')])  #表单名
    submit = SubmitField(u'Upload')

@app.route('/del')
def del_file():
    Data_file_path=[Data_file.path(x) for x in Data_list]
    [os.remove(x) for x in Data_file_path]
    return render_template('del.html',Data_file_path=Data_file_path)

@app.route('/ds_upload', methods=['GET', 'POST'])
def ds_upload():
    form = UploadForm()
    file_urls= [Data_file.url(x)  for x in os.listdir(app.config['UPLOADED_DATA_DEST'])]
    if form.validate_on_submit():
        filename = Data_file.save(form.file.data) #安全文件名
        if fn == filename :
            os.remove(Data_file.path(filename))
        fn = filename
        file_urls.append(Data_file.url(filename))
        flash(file_urls)
    else:
        file_url = None
        flash("wait")
    return render_template('ds_upload.html', form=form, file_urls=file_urls)

@app.route('/uploads', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        al=True
        for f in request.files.getlist('file'):
            ls = os.listdir(pathReal) #去重
            if f.filename not in ls:
                f.save(os.path.join(pathReal,f.filename))
                ls.append(f.filename)
            # Data_list.append(os.path.join(pathReal, f))
    flash("(This is just a demo. Selected files are not actually uploaded to analyse)")
    return render_template('upload.html')

@app.route('/')
def index():
    return render_template('index.html')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 28 15:05:48 2019

Clustering animals
Figure 23.13; Introductin to Computation and Programming Using Python

@author: kemcrimmins
"""

def testTeeth(numClusters, numTrials):
    features, labels, species = readMammalData('dentalFormulas.txt')
    examples = buildMammalExamples(features, labels, species)
    bestClustering = trykmeans(examples, numClusters, numTrials)
    for c in bestClustering:
        names = ''
        for p in c.members():
            names += p.getName() + ', '
        print('\n' + names[:-2]) #remove trailing comma and space
        herbivores, carnivores, omnivores = 0, 0, 0
        for p in c.members():
            if p.getLabel() == '0':
                herbivores += 1
            elif p.getLabel() == '1':
                carnivores += 1
            else:
                omnivores += 1
        print(herbivores, 'herbivores', carnivores, 'carnivores',
              omnivores, 'omnivores')


from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm

W,H = A4

#margenes
m_top = H-2*cm #margen top
m_right = W-2*cm #margen
m_left = cm
m_bottom = cm

y_pos = m_top #posicion actual del cursor
y_rest = m_top - m_bottom #alto disponible para dibujo
x_rest = m_right - m_left
y_anchor = 0 #separacion vertical

#tamanios de texto
H1 = 34
H2 = 23
H3 = 18
P = 12


import keras
from keras.models import Sequential, Model, load_model
from keras.layers.core import Activation
from keras.layers import Dense, Dropout, Activation, Flatten, Input, Lambda
from keras.layers import Conv2D, MaxPooling2D, Conv1D, MaxPooling1D, LSTM, ConvLSTM2D, GRU, BatchNormalization, LocallyConnected2D, Permute
from keras.layers import Concatenate, Reshape, Softmax, Conv2DTranspose, Embedding, Multiply
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import regularizers
from keras import backend as K
import keras.losses

import tensorflow as tf

import isolearn.keras as iso

import numpy as np

#DragoNN Saved Model definition

def load_saved_predictor(model_path, library_context=None) :

	saved_model = Sequential()

	saved_model.add(Conv1D(120, 5, activation='relu', input_shape=(145, 4), name='dragonn_conv1d_1_copy'))
	saved_model.add(BatchNormalization(name='dragonn_batchnorm_1_copy'))
	saved_model.add(Dropout(0.1))

	saved_model.add(Conv1D(120, 5, activation='relu', name='dragonn_conv1d_2_copy'))
	saved_model.add(BatchNormalization(name='dragonn_batchnorm_2_copy'))
	saved_model.add(Dropout(0.1))

	saved_model.add(Conv1D(120, 5, activation='relu', name='dragonn_conv1d_3_copy'))
	saved_model.add(BatchNormalization(name='dragonn_batchnorm_3_copy'))
	saved_model.add(Dropout(0.1))

	saved_model.add(Flatten())
	saved_model.add(Dense(12, activation='linear', name='dragonn_dense_1_copy'))

	saved_model.compile(
		loss= "mean_squared_error",
		optimizer=keras.optimizers.SGD(lr=0.1)
	)

	saved_model.load_weights(model_path)
	#print(saved_model.summary())

	def _initialize_predictor_weights(predictor_model, saved_model=saved_model) :
		#Load pre-trained model
		predictor_model.get_layer('dragonn_conv1d_1').set_weights(saved_model.get_layer('dragonn_conv1d_1_copy').get_weights())
		predictor_model.get_layer('dragonn_conv1d_1').trainable = False
		predictor_model.get_layer('dragonn_batchnorm_1').set_weights(saved_model.get_layer('dragonn_batchnorm_1_copy').get_weights())
		predictor_model.get_layer('dragonn_batchnorm_1').trainable = False

		predictor_model.get_layer('dragonn_conv1d_2').set_weights(saved_model.get_layer('dragonn_conv1d_2_copy').get_weights())
		predictor_model.get_layer('dragonn_conv1d_2').trainable = False
		predictor_model.get_layer('dragonn_batchnorm_2').set_weights(saved_model.get_layer('dragonn_batchnorm_2_copy').get_weights())
		predictor_model.get_layer('dragonn_batchnorm_2').trainable = False

		predictor_model.get_layer('dragonn_conv1d_3').set_weights(saved_model.get_layer('dragonn_conv1d_3_copy').get_weights())
		predictor_model.get_layer('dragonn_conv1d_3').trainable = False
		predictor_model.get_layer('dragonn_batchnorm_3').set_weights(saved_model.get_layer('dragonn_batchnorm_3_copy').get_weights())
		predictor_model.get_layer('dragonn_batchnorm_3').trainable = False

		predictor_model.get_layer('dragonn_dense_1').set_weights(saved_model.get_layer('dragonn_dense_1_copy').get_weights())
		predictor_model.get_layer('dragonn_dense_1').trainable = False

	def _initialize_predictor_weights_old(predictor_model, saved_model=saved_model, model_path=model_path) :
		#Load pre-trained model
		#print(saved_model.summary())
		predictor_model.load_weights(model_path, by_name=True)

	def _load_predictor_func(sequence_input) :
		#DragoNN parameters
		seq_length = 145
		seq_input_shape = (seq_length, 4, 1)
		n_tasks = 1

		#Define model layers
		permute_input = Lambda(lambda x: x[..., -1])

		conv_1 = Conv1D(120, 5, padding='valid', activation='relu', name='dragonn_conv1d_1')
		batchnorm_1 = BatchNormalization(axis=-1, name='dragonn_batchnorm_1')
		drop_1 = Dropout(0.1)

		conv_2 = Conv1D(120, 5, padding='valid', activation='relu', name='dragonn_conv1d_2')
		batchnorm_2 = BatchNormalization(axis=-1, name='dragonn_batchnorm_2')
		drop_2 = Dropout(0.1)

		conv_3 = Conv1D(120, 5, padding='valid', activation='relu', name='dragonn_conv1d_3')
		batchnorm_3 = BatchNormalization(axis=-1, name='dragonn_batchnorm_3')
		drop_3 = Dropout(0.1)

		flatten_3 = Flatten()
		final_dense = Dense(12, name='dragonn_dense_1')

		#Execute functional model definition
		permuted_input = permute_input(sequence_input)

		drop_1_out = drop_1(batchnorm_1(conv_1(permuted_input), training=False), training=False)
		drop_2_out = drop_2(batchnorm_2(conv_2(drop_1_out), training=False), training=False)
		drop_3_out = drop_3(batchnorm_3(conv_3(drop_2_out), training=False), training=False)

		final_dense_out = final_dense(flatten_3(drop_3_out))

		predictor_inputs = []
		predictor_outputs = [final_dense_out, drop_1_out, drop_2_out, drop_3_out]

		return predictor_inputs, predictor_outputs, _initialize_predictor_weights

	return _load_predictor_func