from django.contrib.auth import logout
import re
from django.http import HttpResponseRedirect
import datetime
from django.conf import settings
from carddirector.cd_utils.collection_utils import to_list
from carddirector.cd_auth.services import get_source_ip_from_request,\
    user_has_otp_device
from IPy import IPSet, IP
import logging
from django.core.exceptions import MiddlewareNotUsed
from django.core.urlresolvers import reverse, resolve, NoReverseMatch
from django.shortcuts import redirect
import urllib
from django.contrib import messages

logger = logging.getLogger(__name__)

CHANGE_PWD_URL = "/user_account_update/"
ACCOUNT_DISABLED_URL = "/account_disabled/"

class CustomAuthenticationMiddleware:
    def process_request(self, request):
        check_middleware_and_processor(request)
        if not self.is_admin_pages(request):

            if request.user.is_authenticated() and not self.is_password_change_page(request):
                if self.is_required_force_change_pwd(request.user):
                    return HttpResponseRedirect(CHANGE_PWD_URL)

            if request.user.is_authenticated() and self.is_user_disabled(request.user):
                logout(request)
                return HttpResponseRedirect(ACCOUNT_DISABLED_URL)

    def is_admin_pages(self, request):
        return re.match(r'^/admin/?', request.path) or\
               re.match(r'^/accounts/login/', request.path) or\
               re.match(r'^/account_disabled/?', request.path) or\
               re.match(r'^/sign_out/?', request.path) or\
               re.match(r'^/status/?', request.path)

    def is_password_change_page(self, request):
        return request.path == CHANGE_PWD_URL

    def is_required_force_change_pwd(self, user):
        if user.is_superuser:
            return False
        return user.profile.force_password_change

    def is_user_disabled(self, user):
        if user.is_superuser:
            return False
        return user.profile.is_disabled

class StaffAccessRestrictionMiddleware(object):
    def __init__(self):
        self.staff_groups = to_list(settings.STAFF_GROUPS)
        self.staff_ip_set = IPSet([IP(ip_str) for ip_str in to_list(settings.STAFF_INTERNAL_IP)])
        if len(self.staff_groups) <= 0 or len(self.staff_ip_set.prefixes) <= 0:
            logger.warning('STAFF_GROUPS or STAFF_INTERNAL_IP empty, %(name)s disabled.', {'name':self.__class__.__name__})
            raise MiddlewareNotUsed

    @property
    def access_denied_url(self):
        if not hasattr(self, '_access_denied_url'):
            self._access_denied_url = reverse('cd_auth.access_denied')
        return self._access_denied_url

    def process_request(self, request):
        source_ips_string = get_source_ip_from_request(request)
        source_ip_string = [ip for ip in source_ips_string.split(',') if ip != ''][0]
        source_ip = IP(source_ip_string)
        if source_ip not in self.staff_ip_set:
            user = request.user
            if user.groups.filter(name__in=self.staff_groups).exists():
                logger.warning('Request for staff[%(user)s] not coming from allowed IPs [%(src_ip)s].', {'user':user.username, 'src_ip':source_ip})
                logout(request)
                return redirect(self.access_denied_url)

class ForceOTPSetup(object):
    bypass_url_names = ('cd_main.otp.setup_totp', 'carddirector.cd_main.views.sign_out','django.contrib.staticfiles.views.serve')

    def __init__(self, *args, **kwargs):
        try:
            self.otp_setup_url = reverse('cd_main.otp.setup_totp')
        except NoReverseMatch:
            logger.error('Unable to lookup TOTP setup view. %(name)s disabled', {'name':self.__class__.__name__}, exc_info=True)
            raise MiddlewareNotUsed
        init = super(ForceOTPSetup, self).__init__(*args, **kwargs)
        return init

    def process_request(self, request):
        user = request.user
        resolved_url = resolve(request.path)
        if resolved_url.url_name in self.bypass_url_names:
            return

        if request.path.startswith(settings.STATIC_URL):
            return

        if user.is_authenticated() and user.profile.is_two_factor_enabled == True and not user_has_otp_device(user):
            logger.debug('Redirecting to OTP setup page for view [%s]', resolved_url.url_name)
            next_url = self.otp_setup_url
            next_url += '?next={orig_url}'.format(orig_url=urllib.quote_plus(request.get_full_path()))
            messages.warning(request, 'OTP setup is required before continue')
            return redirect(next_url)

class SessionTimeOut(object):

    def process_request(self, request):
        if request.user.is_authenticated():
            if 'lastRequest' in request.session:
                elapsedTime = datetime.datetime.now() -\
                              request.session['lastRequest']
                if elapsedTime.seconds > settings.SESSION_EXPIRE_TIMEOUT:
                    del request.session['lastRequest']
                    logout(request)

            request.session['lastRequest'] = datetime.datetime.now()
        else:
            if 'lastRequest' in request.session:
                del request.session['lastRequest']

        return None

def check_middleware_and_processor(request):
    assert hasattr(request, 'user'),\
    "The Login Required middleware requires authentication middleware to be installed. Edit your\
     MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middlware.AuthenticationMiddleware'. "\
    "If that doesn't work, ensure your TEMPLATE_CONTEXT_PROCESSORS setting includes\
     'django.core.context_processors.auth'."


from flask_classful import FlaskView, route
from flask_login import login_required
from flask_menu.classy import register_flaskview
from io import BytesIO

from flask import make_response, request, render_template_string, send_file, Blueprint
from pdfkit import from_string

TEMPLATE_BODY = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            {{ content | safe }}
        </body>
        </html>
    """

class ExportView(FlaskView):

    @login_required
    @route('/export-pdf', methods=["POST"])
    def export_pdf(self):
        """
            returns pdf rendered from form 'template' data.
        :return:
        """
        content = request.form.get('template', '')
        as_attachment = True
        if request.form.get('attachment', '') == 'false':
            as_attachment = False
        template = render_template_string(TEMPLATE_BODY, content=content)
        output = BytesIO()
        pdf = from_string(template, output_path=False, options={'quiet': ''})
        output.write(pdf)
        output.seek(0)
        return send_file(output,
                         attachment_filename="ulakbus.pdf",
                         as_attachment=as_attachment)

    @login_required
    @route('/export-word', methods=["POST"])
    def export_word(self):
        """
            returns word rendered from form 'template' data.
        :return:
        """
        content = request.form.get('template', '')
        template = render_template_string(TEMPLATE_BODY, content=content)
        resp = make_response(template)

        resp.headers["Content-Type"] = "application/vnd.ms-word"
        resp.headers["Expires"] = "0"
        resp.headers["Cache-Control"] = "must-revalidate, post-check=0, pre-check=0"
        resp.headers["Content-Disposition"] = "attachment;filename=ulakbus.doc"

        return resp


# Cameron Walcott , cwalcott@usc.edu
# ITP 115, Spring 2020
# Assignment 5
# Description:
# Program encrypts a secret message by shifting the letters of the alphabet and decrypts the message as well.

def main():
    message = input("Enter a message: ")
    shift = int(input("Enter a number to shift by (0-25): "))

    alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    bet = []
    indicies = []

    for num in range(len(alpha)):
        index = num + shift
        if index >= len(alpha):
            index = index - 26
        bet.append(alpha[index])

    bet.append(" ")
    alpha.append(" ")

    encrypt = ""
    decrypt = ""

    for letter in message:
        for num in range(len(alpha)):
            if letter == alpha[num]:
                indicies.append(alpha.index(letter))
        if letter == "":
            indicies.append(bet.index(letter))

    print("Encrypting message....")
    for letter in range(len(indicies)):
        encrypt = encrypt + bet[indicies[letter]]
    print(" Encrypted message: " + encrypt)

    print("Decrypting message....")
    for letter in range(len(indicies)):
        decrypt = decrypt + alpha[indicies[letter]]
    print(" Decrypted message: " + decrypt)
    print("Original message: " + message)
main()


import rcDevTree
import wmi

class DevTree(rcDevTree.DevTree):

    def load_diskdrive(self):
        if not hasattr(self, 'wmi'):
            self.wmi = wmi.WMI()
        for drive in self.wmi.WIN32_DiskDrive():
            d = self.add_dev(drive.DeviceId, int(drive.size)//1024, "linear")
            d.set_devpath(drive.DeviceId)

    def load(self, di=None):
        self.load_diskdrive()

    def blacklist(self, devname):
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))


"""
ElasticBox Confidential
Copyright (c) 2012 All Right Reserved, ElasticBox Inc.

NOTICE:  All information contained herein is, and remains the property
of ElasticBox. The intellectual and technical concepts contained herein are
proprietary and may be covered by U.S. and Foreign Patents, patents in process,
and are protected by trade secret or copyright law. Dissemination of this
information or reproduction of this material is strictly forbidden unless prior
written permission is obtained from ElasticBox
"""

import argparse
import os
import tempfile
import subprocess
import sys

import ebcli
from ebcli import Boxes, AuthenticatedSession

from jinja2 import Environment, FileSystemLoader

EBX_AGENT_DIRECTORY = os.path.dirname(__file__)
EBX_TEMPORARY_FOLDER = '.elasticbox'
EBX_CONTAINER_PATH = '/opt/ebx'

def initialize_parser(subparsers):
    parser = subparsers.add_parser('build', help='Build container for the specified box')

    ebcli.add_standard_argument(parser, ebcli.IMAGE_ARG)
    ebcli.add_standard_argument(parser, ebcli.BOXES_PATH_ARG)
    parser.add_argument(dest='box_id')
    parser.add_argument('--dockerfile', type=argparse.FileType('w'), default=sys.stdout)

    parser.set_defaults(func=execute_command)

def execute_command(args, unknown_args):
    session = AuthenticatedSession(args.url, args.token)

    source = Boxes(args.boxes_path if 'boxes_path' in args else os.getcwd(), session)

    context = {
        'path': EBX_CONTAINER_PATH,
        'box': args.box_id,
        'image': args.image or 'ubuntu',
        'url': args.url
    }

    if 'dockerfile' in args and args.dockerfile:
        box_collection, scripts, ports = _get_setup_data(source, args.box_id)

        filename = os.path.abspath(args.dockerfile.name)

        context['boxes_path'] = os.path.relpath(os.path.dirname(filename), args.boxes_path)
        context['scripts'] = scripts
        context['ports'] = ports
        context['boxes'] = box_collection

        _generate_dockerfile(context, args.dockerfile)
    else:
        ebx_path = os.path.join(tempfile.gettempdir(), EBX_TEMPORARY_FOLDER)

        boxes = Boxes(os.path.join(ebx_path, 'boxes'), session)
        boxes.export_box(args.box_id, recursive=True, source=source)

        box_collection, scripts, ports = _get_setup_data(boxes, args.box_id)

        context['boxes_path'] = os.path.join(ebx_path, boxes)
        context['scripts'] = scripts
        context['ports'] = ports
        context['boxes'] = box_collection

        filename = os.path.join(ebx_path, args.box_id + '.dockerfile')
        with open(filename, 'w') as dockerfile:
            _generate_dockerfile(context, dockerfile)

        try:
            environment = os.environ.copy()
            environment['ELASTICBOX_PATH'] = EBX_CONTAINER_PATH

            if unknown_args:
                subprocess.call(
                    'docker build {0} -f {1} {2}'.format(' '.join(unknown_args), filename, ebx_path),
                    env=environment,
                    cwd=ebx_path,
                    shell=True)
            else:
                subprocess.call(
                    'docker build -f {0} {1}'.format(filename, ebx_path),
                    cwd=ebx_path,
                    shell=True,
                    env=environment)
        finally:
            os.remove(filename)

def _get_setup_data(boxes, box_id, scope=None):
    box = boxes.get_box(box_id)

    scripts = []
    ports = set()
    inner_boxes = set([box['name']])

    pre_install_script = os.path.join(box['name'], 'events', 'pre_install')
    if os.path.exists(os.path.join(boxes.get_path(), pre_install_script)):
        scripts.append({"scope": scope, "path": pre_install_script})

    for variable in box['variables']:
        name = variable['name']
        value = variable['value']

        if variable['type'] == 'Box':
            if scope:
                nested_boxes, nested_scripts, nested_ports = _get_setup_data(
                    boxes, value, "{0}.{1}".format(scope, name))
            else:
                nested_boxes, nested_scripts, nested_ports = _get_setup_data(boxes, value, name)

            scripts = scripts + nested_scripts
            ports = ports.union(nested_ports)
            inner_boxes = inner_boxes.union(nested_boxes)
        elif variable['type'] == 'Port':
            ports.add(value)

    install_script = os.path.join(box['name'], 'events', 'install')
    if os.path.exists(os.path.join(boxes.get_path(), install_script)):
        scripts.append({"scope": scope, "path": install_script})

    return inner_boxes, scripts, ports

def _generate_dockerfile(context, dockerfile):
    environment = Environment(
        loader=FileSystemLoader(EBX_AGENT_DIRECTORY),
        trim_blocks=True)

    print >> dockerfile, environment.get_template('Dockerfile.jinja').render(context)


numero=0
while numero==0 or numero<=9:
    numero=int(input("Numero: "))
numeros=[]
for n in str(numero):
    numeros.append(n)

n=len(numeros)-1
inv=""
while n>=0:
    inv+=str(numeros[n])
    n-=1

print(inv)


from cortex import Cortex

class Train():
	def __init__(self):
		self.c = Cortex(user, debug_mode=True)
		self.c.do_prepare_steps()

	def train(self,
			profile_name,
			training_action,
			number_of_train):

		stream = ['sys']
		self.c.sub_request(stream)

		profiles = self.c.query_profile()

		if profile_name not in profiles:
			status = 'create'
			self.c.setup_profile(profile_name, status)

		status = 'load'
		self.c.setup_profile(profile_name, status)

		print('begin train -----------------------------------')
		num_train = 0
		while num_train < number_of_train:
			num_train = num_train + 1

			print('start training {0} time {1} ---------------'.format(training_action, num_train))
			print('\n')
			status='start'
			self.c.train_request(detection='mentalCommand',
								action=training_action,
								status=status)

			print('accept {0} time {1} ---------------'.format(training_action, num_train))
			print('\n')
			status='accept'
			self.c.train_request(detection='mentalCommand',
								action=training_action,
								status=status)

		print('save trained action')
		status = "save"
		self.c.setup_profile(profile_name, status)

		status = 'unload'
		self.c.setup_profile(profile_name, status)

	def live(self, profile_name):
		print('begin live mode ----------------------------------')
		# load profile
		status = 'load'
		self.c.setup_profile(profile_name, status)
		# sub 'com' stream and view live mode
		stream = ['com']
		self.c.sub_request(stream)

user = {
	"license" : "",
	"client_id" : "id",
	"client_secret" : "secret",
	"debit" : 100
}

t=Train()
profile_name = 'Profil Ismi'
number_of_train = 1

training_action = 'neutral'
t.train(profile_name,
		training_action,
		number_of_train)

training_action = 'left'
t.train(profile_name,
		training_action,
		number_of_train)

training_action = 'right'
t.train(profile_name,
		training_action,
		number_of_train)

training_action = 'push'
t.train(profile_name,
		training_action,
		number_of_train)

training_action = 'drop'
t.train(profile_name,
		training_action,
		number_of_train)

t.live(profile_name)
# -----------------------------------------------------------


# This script demonstrates a paraxial ray trace from the object plane
# to an infinite amount of surfaces.
#
# Phillip K Poon

import numpy as np

######################## User Defined Variables ##########################

numSurf = 4 # The number of surfacee

# The slope of the initial ray after the object plane
uList = [0]

yList = [10]

tList = [2.0, 2.78, 11.05, 8.74]
nList = [1.00, 1.649, 1.00, 1.617, 1.00]
phiList = [-0.016327, -0.011662, 0, 0.015071]

##########################################################################

# Define the ray height transfer function
def transfer(y, u, t):
    yPrime = y + u*t
    return yPrime

# Define the ray angle refraction function
def refract(nPrime, n, u, y, phi):
    uPrime = (1/nPrime)*(n*u-y*phi)
    return uPrime

print(phiList)

for surfInd in range(0,numSurf):

    if surfInd == 0:
        print('\n Just after surface {} the slope is {} and height is {}.'.format(surfInd, uList[surfInd], yList[surfInd] ))

    # Transfer to the next surface

    yPrime = transfer(yList[surfInd],uList[surfInd],tList[surfInd])

    yList.append(yPrime)

    print('\n Just before surface {} the slope is {} and height is {}.'.format(surfInd + 1, uList[surfInd], yList[surfInd + 1] ))

    # Refract after the surface
    uPrime = refract(nList[surfInd+1], nList[surfInd], uList[surfInd], yList[surfInd + 1], phiList[surfInd])

    uList.append(uPrime)

    print('\n Just after surface {} the slope is {} and height is {}.'.format(surfInd+1, uList[surfInd+1], yList[surfInd+1] ))


def command():
    while (True):
        command = (input("#")).lower()
        if   command == "show": show()
        elif command == "search": search(inputsid())
        elif command == "changescore": changeScore(inputsid())
        elif command == "add": add(inputsid())
        elif command == "searchgrade": searchGrade()
        elif command == "remove": remove(inputsid())
        elif command == "quit": quit();break
        elif command == "q": break

def inputsid(): return (input("Student ID: ")).lower()

def grading (avg):
    if   avg >= 90: grade = "A"
    elif avg >= 80: grade = "B"
    elif avg >= 70: grade = "C"
    elif avg >= 60: grade = "D"
    else:
        grade ="F"
    return grade

def show():
    row_labels = ['Student','Name','Midterm','Final','Average','Grade']
    print(" {0:9} {1:>15}   {2:^7}   {3:^5}   {4:^7}   {5:^4}".format(*row_labels))
    print("-"*62)
    for k, v in sorted(students.items(), key=lambda item: item[1]["Average"], reverse = True):
        print(k, end='\t')
        printer(k)

def search(sid):
    if sid not in students:
        print("NO SUCH PERSON.")
    else:
        printer(sid)

def changeScore(sid):
    if sid not in students:
        print("NO SUCH PERSON.")
    else:
        mORf = (input("Mid/Final? ")).lower()
        if mORf != "mid" and mORf != "final": return
        newScore = int(input("Input new score: "))
        if newScore > 100 or newScore < 0: print("score not exist");return
        printer(sid)
        if mORf == "mid":     students[sid]["Midterm"] = newScore
        elif mORf == "final": students[sid]["Final"]   = newScore
        add_avgrade()
        print("Score Changed")
        printer(sid)

def searchGrade():
    targetGrade = input("Grade to search: ")
    if targetGrade in "ABCDF":
        for sid in students:
            if students[sid]["Grade"] == targetGrade:
                printer(sid)

def add(newID):
    if newID in students:
        print("ALREADY EXIST")
    else:
        name = input("Name: ")
        mid = int(input("Midterm Score: "))
        final = int(input("Final Score: "))
        students[newID] = {"Name" : name, "Midterm": mid, "Final" : final}
        add_avgrade()
        print("Student added")

def remove(sid):
    if sid not in students:
        print("NO SUCH PERSON.")
    else:
        del students[sid]
        print("Student removed")

def quit():
    yORn = input("Save data?[yes/no] ").lower()
    if yORn == "yes":
        fname = input("File name: ")
        data = ""
        for sid in sorted(students.items(), key=lambda item: item[1]["Average"], reverse = True):
            data += sid[0] + "\t"
            for key, value in sid[1].items():
                sid[1][key] = str(value)
            data += '\t'.join(sid[1].values()) + "\n"
        f_write = open(fname, "w")
        f_write.write(data)
        f_write.close()

def printer(sid):
    for values in students[sid].values():
        print(values, end="\t")
    print()

def add_avgrade():
    for sid in students:
        students[sid]["Average"] = (students[sid]["Midterm"] + students[sid]["Final"])/2
        students[sid]["Grade"] = grading(students[sid]["Average"])

import sys
f = open(sys.argv[1], "r")
students = {}
for s in f.readlines():
    s2 = s.rstrip().split()
    students[s2[0]] = {"Name" : " ".join(s2[1:3]), "Midterm": int(s2[3]), "Final" : int(s2[4])}
add_avgrade()
command()


# -*- coding : utf-8 -*-
from os import getcwd
import os
import subprocess
import time
import glob

current_path = getcwd()
sample_dicom_file_path = "sample.dcm"
sample_sp_dicom_file_folder = current_path + "\database\\"
dcmprscu_app = "dcmprscu.exe"
dcmpstat_cfg = "dcmpstat.cfg.scu"
dcmpsprt_app = "dcmpsprt.exe"
dcmprinters_cfg = "printers.cfg"

for file in os.listdir(sample_sp_dicom_file_folder):
    os.remove(os.path.join(sample_sp_dicom_file_folder, file))
time.sleep(5)

os.chdir(current_path)
subprocess.run([dcmpsprt_app, "-v", "-c", dcmpstat_cfg, "--printer", "IHEFULL",sample_dicom_file_path])

time.sleep(5)

os.chdir(sample_sp_dicom_file_folder)
dcm_list = glob.glob("SP_*.dcm")
if dcm_list.__len__() == 1:
    dcm_file = sample_sp_dicom_file_folder + dcm_list[0]
    os.chdir(current_path)
    subprocess.run([dcmprscu_app, "-d", "-v", "-c", dcmpstat_cfg,"--copies", "1", dcm_file])

time.sleep(10)


import tensorflow as tf
import dataset_maker as DM
import numpy as np
import os, sys

filedir = os.path.join(os.getcwd(), 'data','txt','')
iter = int(sys.argv[1])
expension = '.txt'
print(filedir)
#print(os.listdir(filedir))
label_filedir = os.getcwd()
label_file = 'Label.csv'
num_data=len(os.listdir(filedir))  #This number should equal the number of sampled images.

data = DM.DataSet(filedir=filedir, expension=expension, label_filedir=label_filedir, label_file=label_file, data_size=num_data)   #data size = the number of images in data folder

X = tf.placeholder(tf.float32, [None, 48, 48, 1]) #Pixel data
Y = tf.placeholder(tf.float32, [None, 3]) #Labe vector data
keep_prob = tf.placeholder(tf.float32)

#Begins convolutional training.
print(X)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))
L1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='SAME')
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print((L1.get_shape))
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print((L2.get_shape))
W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01))
L3 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')
L3 = tf.nn.relu(L2)
L3 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print((L3.get_shape))
W4 = tf.Variable(tf.random_normal([6 * 6 * 64, 512], stddev=0.01))
L4 = tf.reshape(L3, [-1, 6 * 6 * 64])
L4 = tf.matmul(L4, W4)
L4 = tf.nn.relu(L4)
L4 = tf.nn.dropout(L4, keep_prob)
print((L4.get_shape))
W5 = tf.Variable(tf.random_normal([512, 3], stddev=0.01))
model = tf.matmul(L4, W5)
print(model)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)

saver = tf.train.Saver(max_to_keep=10)  #

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

batch_size = 50
total_batch = int(num_data / batch_size)

for epoch in range(iter): #Change this number. This is the number of iterations. The higher this number, the more accurate your model becomes.
    total_cost = 0
    data_shuffle = True
    accumulate_accuracy = 0
    for i in range(total_batch):
        batch_xs, batch_ys = data.next_batch(batch_size, data_shuffle)
        batch_xs = np.asarray(batch_xs)
        batch_ys = np.asarray(batch_ys)
        batch_xs = batch_xs.reshape(-1, 48, 48, 1)

        _, cost_val = sess.run([optimizer, cost],
                               feed_dict={X: batch_xs,Y: batch_ys,keep_prob: 0.7})
        total_cost += cost_val
        data_shuffle = False

    data_shuffle_test = True

    print(('epoch: ' + str((epoch + 1)) + ' cost: ' + str(format(total_cost / total_batch))))

saver.save(sess,'./my_model.ckpt')
print('Training finish!')


#!/usr/bin/env python

# Author: HArrit Diwan
# Date: October 17, 2016
#

import rospy
import cv2
import threading
from bondpy import bondpy
from cv_bridge import CvBridge, CvBridgeError
from localization.lanedetection_udacity import *

from simulator.msg import LanesMsg
import geometry_msgs.msg
import sensor_msgs.msg
import std_msgs.msg

class LaneDetector:
    def __init__(self):
        self.camera_data = None

        self.lane_msg = LanesMsg()

        self.bridge = CvBridge()
        self.font = cv2.FONT_HERSHEY_SIMPLEX

        self.lock = threading.Lock()

        self.lanes_bond = bondpy.Bond("keepalive/lanes", "lanes")

        # topic names for the node
        self.camera_topic = "/camera/image_mono"

        self.lane_topic = '/localization/lanes'

        # publishers
        self.lane_pub = rospy.Publisher(self.lane_topic, LanesMsg, queue_size=10)

        # subscribers
        rospy.Subscriber(self.camera_topic, sensor_msgs.msg.Image, self.setCenterImageData)

    def setCenterImageData(self, camera_msg):
        with self.lock:
            self.camera_data = camera_msg

    def getCenterImageData(self):
        cv_image = None

        if self.camera_data:
            with self.lock:
                try:
                    cv_image = self.bridge.imgmsg_to_cv2(self.camera_data, 'bgr8')
                except CvBridgeError as e:
                    print(e)

            self.camera_data = None

        return cv_image

    def laneDetection(self):
        image = self.getCenterImageData()
        self.lane_msg = LanesMsg()

        if image is not None:
            leftpointlist = []
            rightpointlist = []

            [leftPoints, rightPoints] = final_processing(image)

            for point_array in leftPoints:
                pt = geometry_msgs.msg.Point()
                pt.x = point_array[0]
                pt.y = point_array[1]
                pt.z = 0
                leftpointlist.append(pt)

            for point_array in rightPoints:
                pt = geometry_msgs.msg.Point()
                pt.x = point_array[0]
                pt.y = point_array[1]
                pt.z = 0
                rightpointlist.append(pt)

            self.lane_msg.right.lane = rightpointlist
            self.lane_msg.left.lane = leftpointlist

    def run(self):
        rospy.init_node('lane_detector')
        rate = rospy.Rate(10)  # 10hz

        self.lanes_bond.start()

        while not rospy.is_shutdown():

            # Publish Lanes
            self.laneDetection()
            self.lane_msg.header = std_msgs.msg.Header()
            self.lane_msg.header.stamp = rospy.Time.now()
            self.lane_pub.publish(self.lane_msg)

            rate.sleep()

if __name__ == '__main__':
    print("Lane Detector Started.")
    LaneDetector().run()
    cv2.destroyAllWindows()


import time
import logging

from core.systems import *
from core.entities import Player
from core.map_generator import DungeonGenerator
from engine.ecs import Manager as ECSManager

class GameState(object):
    def __init__(self, ecs_manager, map):
        self.map = DungeonGenerator(ecs_manager).generate(80, 60) if map is None else map
        self.player = None

class Game:

    def __init__(self):
        self.manager = ECSManager()
        # self.map = Map.from_file('data/level1.txt', self.manager)
        map_generator = DungeonGenerator(self.manager)
        self.map = map_generator.generate(80, 60)
        self.game_state = GameState(self.manager, self.map)
        self.manager.add_system(UIProcessor())
        self.manager.add_system(AIProcessor())
        self.manager.add_system(CombatProcessor())
        self.manager.add_system(MovementProcessor(self.map))

        player_pos = (None, None)
        for ent, (pos, spawn) in self.manager.get_components(Position, PlayerSpawn):
            player_pos = (pos.x, pos.y)

        self.player = Player(*player_pos)
        self.manager.add_entity(self.player)
        self.map.add_entity(self.player)
        self.game_state.player = self.player

        console_log = logging.StreamHandler()
        formatter = logging.Formatter('%(name)-20s: %(levelname)-8s %(message)s')
        console_log.setFormatter(formatter)
        root_logger = logging.getLogger('')
        root_logger.addHandler(console_log)
        root_logger.setLevel(logging.DEBUG)

    def run(self):
        self.running = True
        self.manager.bcaster.add_sub('quit-game', self.quit)
        self.manager.update(self.game_state)
        while self.running:
            self.manager.update(self.game_state)

    def quit(self):
        self.running = False

if __name__ == '__main__':
    game = Game()
    game.run()


from requests import get

class Classroom:
    """Contains help functions to get an overveiw of the viritual classroom"""

    def __init__(self, auth, url_orgs):
        self.auth = auth
        self.url_orgs = url_orgs

    def get_teams(self):
        return self._get(self.url_orgs + "/teams")

    def get_repos(self):
        return self._get(self.url_orgs + "/repos")

    def _get(self, url):
        # Find numer of pages
        r = get(url, auth=self.auth, params={'per_page':100, 'page':1})

        if 'link' not in r.headers.keys():
            return r.json()

        else:
            header = r.headers['link'].split(',')
            for link in header:
                if 'rel="last"' in link:
                    pages = int(link.split(';')[0][-2])

            # Get each page
            teams = r.json()
            for page in range(pages-1):
                r = get(url, auth=self.auth, params={'per_page':100, 'page':page+2})
                teams += r.json()

            return teams


# To add a new cell, type '
# To add a new markdown cell, type  [markdown]'
#from IPython import get_ipython

# 导入需要的包
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import torch.nn.functional as F

import torchvision.datasets as dsets
import torchvision.transforms as transforms

import matplotlib.pyplot as plt
import numpy as np

#get_ipython().run_line_magic('matplotlib', 'inline')

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

image_size = 28
num_classes = 10
num_epochs = 20
batch_size = 64

# 加载MNIST数据
# 用户想调用自己的数据图像时，可以使用torchvision.datasets.ImageFolder
# 或torch.utils.data.TensorDataset
train_dataset = dsets.MNIST(root='./data',
                            train=True,
                            transform=transforms.ToTensor(),
                            download=True)
test_dataset = dsets.MNIST(root='./data',
                           train=False,
                           transform=transforms.ToTensor)

train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

'''校验集和测试集，校验集用来检测模型，测试机用来检测整个模型的工作'''
# indices，相当于对所有test_dataset中的数据编码
# indice_val 校验集数据的下标，indices_test表示测试集的下标
indices = range(len(test_dataset))
indices_val = indices[:5000]
indices_test = indices[5000:]

# 根据下标构造两个数据集的SubsetRandomSampler采样器，它会对下标进行采样
sampler_val = torch.utils.data.sampler.SubsetRandomSampler(indices_val)
sampler_test = torch.utils.data.sampler.SubsetRandomSampler(indices_test)

# 根据两个采样器定义加载器
'''
采样器：为加载器提供了一个每一批抽取数据集中样本的方法。我们可以按照顺序逐个抽取数据集中的数据到加载器中，
也可以完全的随机地抽取，甚至可以依某种概率抽取。
'''
validation_loader = torch.utils.data.DataLoader(dataset = test_dataset,
                                                batch_size = batch_size,
                                                shuffle = False,
                                                sampler = sampler_val)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False,
                                          sampler=sampler_test)

# 随便从数据集中读入一张图片，并绘制出来
idx = 100
# dataset支持下标索引，其中提取出来的元素为features，target格式，即属性和标签。[0]表示索引features
muteimg = train_dataset[idx][0].numpy()
plt.imshow(muteimg[0, ...])
print('label is:', train_dataset[idx][1])

# 构建网络
'''
使用nn.Module类  来构建卷积神经网络
'''
depth = [4, 8]

class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 4, 5, padding=2)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(depth[0], depth[1], 5, padding=2)
        self.fc1 = nn.Linear(image_size//4*image_size//4*depth[1], 512)
        self.fc2 = nn.Linear(512, num_classes)

    def forward(self, x):
        x = self.conv1(x)  # 第一层卷积
        x = F.relu(x)  # 激活函数，用Relu
        x = self.pool(x)  # 第二层池化，将图片变小
        x = self.conv2(x)  # 第三层卷积，窗口为5
        x = F.relu(x)  # 非线性函数
        x = self.pool(x)  # 第四层池化，将图片缩小到原来的1/4

        x = x.view(-1, image_size//4*image_size//4*depth[1])
        x = F.relu(self.fc1(x))

        x = F.dropout(x, training=self.training)
        x = self.fc2(x)

        x = F.log_softmax(x, dim=1)
        return x

    def retrieve_features(self, x):
        feature_map1 = F.relu(self.conv1(x))
        x = self.pool(feature_map1)
        feature_map2 = F.relu(self.conv2(x))
        return (feature_map1, feature_map2)

net = ConvNet().to(device)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001,
                      momentum=0.9)  # 定义优化器，普通的随机梯度下降法

record = []
weights = []

def rightness(output, target):
    preds = output.data.max(dim=1, keepdim=True)[1]
    return preds.eq(target.data.view_as(preds)).cpu().sum(), len(target)

for epoch in range(num_epochs):
    train_rights = []

    for batch_idx, (data, target) in enumerate(train_loader):  # 针对容器中的每一个批进行循环
        # 将Tensor转化为Variable,data为一批图像，target为一批标签
        data, target = Variable(data), Variable(target)
        data = data.to(device)
        target = data.to(device)
    # 给网络模型做标记，标志着模型在训练集上训练
    # 这种区分主要是为了打开关闭net的training标志，从而决定是否运行dropout
        net.train()

        output = net(data)  # 神经网络完成一次前馈的计算过程，得到预测输出output
        loss = criterion(output, target)  # 将output与标签target比较计算误差
        optimizer.zero_grad()  # 清空梯度
        loss.backward()  # 反向传播
        optimizer.step()  # 一步随机梯度下降算法
        right = rightness(output, target)  # 计算准确率所需数值，返回数值为
        train_rights.append(right)  # 将计算结果装到列表容器train_rights中

        if batch_idx % 100 == 0:
            net.eval()
            val_rights = []

            #开始在校验集上做循环，计算校验集上的准确度
            for (data, target) in validation_loader:
                data, target = Variable(data), Variable(target)
                data = data.to(device)
                target = data.to(device)
                # 完成一次前馈计算过程，得到目前训练的模型net在校验集上的表现
                output = net(data)
                # 计算准确率所需数值，返回正确的数值为（正确样例数，总样本数）
                right = rightness(output, target)
                val_rights.append(right)

                train_r = (sum([tup[0] for tup in train_rights]),
                           sum([tup[1] for tup in train_rights]))
                val_r = (sum([tup[0] for tup in val_rights]),
                         sum([tup[1] for tup in val_rights]))

                record.append(
                    (100-100.*train_r[0]/train_r[1], 100-100.*val_r[0]/val_r[1]))
                weights.append([net.conv1.weight.data.clone(), net.conv1.bias.data.clone(),
                                net.conv2.weight.data.clone(), net.conv2.bias.data.clone()])

with torch.no_grad:
    net.eval()
    vals = []
    for data, target in test_loader:
        #data, target = Variable(data, volatile=True), Variable(target)
        output = net(data)
        val = rightness(output, target)
        vals.append(val)

    rights = (sum([tup[0] for tup in vals]), sum([tup[1] for tup in vals]))
    right_rate = 1.0*rights[0]/rights[1]
    right_rate

plt.figure(figsize=(10, 7))
plt.plot(record)  # record记载了每一个打印周期记录的训练集和校验集上的准确度
plt.xlabel('Steps')
plt.ylabel('Error rate')


# © Copyright 2018-2023 Triptera Pty Ltd - https://pythonator.com
import unittest

from triptera_pe_tests import GeneralTestCase, mock_create, mock_building_send, configure_logging
from unittest import mock
from ircbuilder import nodebuilder

def building_pattern(player_z):
    b = {}
    # BUILDING MATERIALS
    air = "air"
    wall = "default:glass"
    floor = "default:stone"
    torch = "default:torch"
    # BUILDING LOCATION
    path_x_min = 105
    floor_y = 9
    # BUILDING SIZE
    arch_height = 7
    arch_width = 5
    # ENGINEERING CALCULATIONS
    wall_z = player_z - arch_width // 2
    range_x_arch = path_x_min
    range_y_ext = range(floor_y, floor_y + arch_height)
    range_z_ext = range(wall_z, wall_z + arch_width)
    range_y_int = range(floor_y + 1, floor_y + arch_height - 1)
    range_z_int = range(wall_z + 1, wall_z + arch_width - 1)
    # BUILD
    b.update(nodebuilder.build(range(path_x_min, path_x_min + 40), range(floor_y + 1, floor_y + 31), range(player_z - 4, player_z + 5), air))
    b.update(nodebuilder.build(range_x_arch, range_y_ext, range_z_ext, wall))
    b.update(nodebuilder.build(range_x_arch, range_y_int, range_z_int, air))
    b.update(nodebuilder.build(range_x_arch, floor_y, range_z_int, floor))
    b.update(nodebuilder.build(range_x_arch, floor_y + 1, player_z + 1, torch))
    return b

class TestCase(GeneralTestCase):
    @mock.patch('ircbuilder.building.Building.send', mock_building_send)
    @mock.patch('ircbuilder.MinetestConnection.create', mock_create)
    def test_building(self):
        from lesson1.task11_arch_floor.task import b
        self.check_building_with_pattern(b, building_pattern)

    def test_by_building_in_minetest(self):
        from lesson1.task11_arch_floor.task import b
        self.check_by_building_in_minetest(b, 11)

if __name__ == '__main__':
    # these steps are not executed when pressing the [Check] button
    configure_logging()
    unittest.main()


import random
from mezzanine.blog.models import BlogPost

__author__ = 'opel'

from django.core.management.base import BaseCommand

## CRON EXAMPLE (every 5 min)
## */5 * * * * python manage.py hottop

class Command(BaseCommand):
    help = "Updates views counter"

    def handle(self, *apps, **options):
        blog_posts = BlogPost.objects.all()
        for bp in blog_posts:
            current = bp.views_count
            old = bp.views_count_delta
            bp.views_count_delta = current
            bp.save()
        self.stdout.write("\nSuccess.\n")


import ctypes
import os

def main():
    path = os.path.join(os.path.abspath("."), "float_input.so")
    lib = ctypes.CDLL(path)
    user_input = float(input("Enter a float: "))
    print("Answer is:")
    print("--------------------")
    lib.float_input(user_input)
    print("--------------------/n/n")

if __name__ == "__main__":
    main()


import urllib.request, tarfile, os, time
from bs4 import BeautifulSoup
import nltk.tokenize
import numpy as np
from collections import Counter

class DataSet:
    """
    A class to represent the entire data from the Reuters-21578 collection. Deals with downloading and unpacking the required files, then reading them in and storing the data for subsequent use by the classifiers. This is all done automatically with instantiation of the class.
    """
    def __init__(self):
        self.train_vectors = []  # will become a np array of binary vectors, each vector representing a document and each bit representing the presence of the term at that index in self.all_features in that particular document

        self.train_labels = []  # will become a np array of strings, representing the label of the document associated with that index in the training data

        self.test_vectors = []  # as self.train_vectors, on the test set
        self.test_labels = []  # as self.train_labels, on the test set

        self.all_features = []  # will become a list containing strings which are all the (normalised) terms occuring in any document in the collection, to be used as features

        self.all_labels = ["acq", "crude", "earn", "grain", "interest", "money-fx", "ship", "trade", "corn", "wheat"]  # these are the 10 most common classes in reuters, only documents in one or more of these classes will be selected into the sets

        self.train_body_raw = []  # list used to contain the raw text extracted from the body of each document in the training set, later tokenised, normalised, then these variables are set to None at the end of training
        self.test_body_raw = []  # as self.train_body_raw for the test set

        # begin creating the data set
        t0 = time.time()

       # get_reuters()

        self.parse_reuters_files()  # read in all the files, fill in train_labels, test_labels, train_body_raw, test_body_raw
        print("Parsed the Reuters files")

        self.extract_vocab()
        print("Extracted vocabulary of", len(self.all_features), "words")
        self.text_to_features()

        # done with these values now, so clear them
        self.train_body_raw = None
        self.test_body_raw = None

        print("Processing the data set took", time.time() - t0, "seconds")
        print("Training set distribution:", Counter(self.train_labels))
        print("Test set distribution:", Counter(self.test_labels))

    def parse_reuters_files(self):
        """
        Parse the reuters files.
        add to training set iff LEWISSPLIT="TRAIN";  TOPICS="YES", test set iff LEWISSPLIT="TEST"; TOPICS="YES"  - modification of the MODAPTE split, taking into account the 10 most common topics only
        """
        for file in os.listdir("./data/"):
            print("processing", file)
            with open("./data/" + file, "r", encoding="latin-1") as fin:
                contents = fin.read()

            soup = BeautifulSoup(contents, "html.parser")  # the data format of the reuters files approximates HTML close enough for this to be the simplest way to parse them and easily extract the relevant information

            docs = soup.find_all("reuters")  # each document in the file is contained within a "reuters" tag

            for doc in docs:
                # filter out the documents I don't  want in my sets: those with no topics at all, or topics that aren't the 10 most common
                if doc["topics"] == "NO":  # the TOPICS attribute in the REUTERS tag
                    continue

                topics_tag = doc.find_next("topics")

                relevant = True
                topics = topics_tag.find_all("d")
                for d in topics:
                    if d.string not in self.all_labels:
                        relevant = False
                        break
                if topics == [] or not relevant:  # There are a few documents which have no topics at all
                    continue

                # only relevant documents will get this far!
                body_tag = doc.find_next("body")
                if body_tag is None:  # there are a couple of instances of docs with no body, which are not useful here
                    continue
                body_text = body_tag.string

                # add the body of the text to the array that is the value for each of its topics in the appropriate dictionary
                if doc["lewissplit"] == "TRAIN":
                    for topic in topics:  # there may be more than one topic associated with each document
                        self.train_labels.append(topic.string)
                        self.train_body_raw.append(body_text)

                elif doc["lewissplit"] == "TEST":
                    for topic in topics:
                        self.test_labels.append(topic.string)
                        self.test_body_raw.append(body_text)

    def extract_vocab(self):
        """
        Tokenize & normalize raw body text extracted from Reuters files, add each of the normalised tokens from the training set to the vocabulary self.all_features
        """
        vocab_set = set()  # for collecting the unique vocabulary items

        # process the training set
        for i in range(len(self.train_body_raw)):
            tokenized = nltk.tokenize.word_tokenize(self.train_body_raw[i], language='english')
            normalized = [w.lower() for w in tokenized if
                          w.isalpha()]  # there are loads of numbers, these are essentially really rare words, so not helpful for classification.
            self.train_body_raw[i] = set(normalized)  # reduce whole body to only unique normalised tokens for efficiency
            for word in self.train_body_raw[i]:
                vocab_set.add(word)  # this is a set, ensures it contains only unique elements by definition

        # process the test set in identical fashion, but don't extract its vocabulary!
        for i in range(len(self.test_body_raw)):
            tokenized = nltk.tokenize.word_tokenize(self.test_body_raw[i], language='english')
            normalized = [w.lower() for w in tokenized if
                          w.isalpha()]
            self.test_body_raw[i] = set(normalized)

        self.all_features = list(vocab_set)

    def text_to_features(self):
        """
        Map from a set of "bag of words" items in a document, to a binary array, conceptually a feature vector, where the value of a bit at a certain index in the array indicates the presence of the item at that index in the vocabulary self.all_features
        """

        # make an zeros nparray of dimensions vocab_size, no_of_training/test_docs in self.train_features, self.test_features, since most of the values in the feature vector will be 0
        self.train_vectors = np.zeros((len(self.train_labels), len(self.all_features)), dtype=np.bool)
        self.test_vectors = np.zeros((len(self.test_labels), len(self.all_features)), dtype=np.bool)

        # process the training set
        for i in range(len(self.train_body_raw)):  # for all the docs
            for word in self.train_body_raw[i]:  # word in the doc bag-of-words
                index = self.all_features.index(word)  # find the index of that word in the features list
                self.train_vectors[i, index] = 1  # set the appropriate position in that doc's feature vector to 1

        # process the test set in the same way
        for i in range(len(self.test_body_raw)):
            for word in self.test_body_raw[i]:
                if word in self.all_features:  # there may be some words in the test set that arent in the training set
                    index = self.all_features.index(word)
                    self.test_vectors[i, index] = 1

def get_reuters():
	"""
	Download the Reuters-21578 data set archive and extract all the files.
	"""
	print("Downloading reuters-21578 from http://kdd.ics.uci.edu/databases/reuters21578/")
	tarfilepath = "reuters.tar.gz"

	# download the archive
	urllib.request.urlretrieve("http://kdd.ics.uci.edu/databases/reuters21578/reuters21578.tar.gz", tarfilepath)

	# extract the files from the downloaded archive # TODO check this works cross-OS
	if tarfile.is_tarfile(tarfilepath):
		tfile = tarfile.open("reuters.tar.gz", "r:gz")
		tfile.extractall("./data/")

	# tidy up the directory - keep only the required sgm files
	os.remove(tarfilepath)
	for root, dirs, files in os.walk("./data/"):
		for file in files:
			if not file.endswith(".sgm"):
				os.remove("./data/" + file)


import datetime, requests, sys, time, webapp2
sys.path.append('lib')
from matplotlib.path import Path
from constants import *
from userVars import *

seenList = []
seenRaidList = []
raidArea = Path(RAID_AREA, [ Path.MOVETO ] + [ Path.LINETO ] * (len(RAID_AREA) - 2) + [ Path.CLOSEPOLY ])

class Scan(Enum):
	RAID = 0
	RARE_MON = 1
	HIGH_MON = 2

def getDiscordString(type, obj):
	discordStrArray = []
	if type == Scan.RAID:
		if obj['pokemon_id'] == 0:
			discordStrArray = [
				'Level',
				obj['level'],
				'raid at',
				getGymName(obj),
				'starting at',
				getTimeString(obj['raid_start']) + '.'
			]
		else:
			discordStrArray = [
				getPokemonNameFromObj(obj),
				'raid at',
				getGymName(obj),
				'until',
				getTimeString(obj['raid_end']) + '.'
			]
			if obj['move1'] != 0 and obj['move2'] != 0:
				discordStrArray.append(getMoveName(obj['move1']) + ', ' + getMoveName(obj['move2']) + '.')
	else:
		discordStrArray.append(getPokemonNameFromObj(obj))
		if validIV(obj):
			discordStrArray.append(getIVPercentage(obj))
		if obj['cp'] != -1:
			str = '({} CP'.format(obj['cp'])
			if validIV(obj):
				# obj['level'] has not been trustworthy so figure it out ourselves
				# don't figure it out just to test other things first
				str += ' L{}'.format(obj['level'])
			discordStrArray.append(str + ')')
		discordStrArray += [
			'until',
			getTimeString(obj['despawn']),
			'at',
			getGoogleMapsURL(obj['lat'], obj['lng'])
		]
	return ' '.join(discordStrArray)

def getGoogleMapsURL(lat, lng):
	return 'http://maps.google.com/maps?q={},{}'.format(lat, lng)

def getIVPercentage(pok):
	return int(round(pok['attack'] + pok['defence'] + pok['stamina']) / 0.45, 0)

def getLocationName(obj):
	key = '{}{}'.format(obj['lat'], obj['lng'])
	if key in GYM_NAMES:
		return GYM_NAMES[key]
	else:
		return '{},{}'.format(obj['lat'], obj['lng'])

def getMoveName(mID):
	return MOVE_NAMES[mID]

def getPokemonName(pID):
	return POKEMON_NAMES[pID]

def getPokemonNameFromObj(pok):
	return getPokemonName(pok['pokemon_id']) + '' if pok['form'] == 0 else ' - {}'.format(pok['form']) # TODO: what is this for unown?

def getTimeString(utcTime):
	return datetime.datetime.fromtimestamp(int(utcTime)).strftime('%H:%M:%S')

def getURL():
	return 'https://sydneypogomap.com/query2.php?since=0&mons={}&bounds={}'.format(','.join(POKEMON.keys()), POKEMON_AREA)

def isHighIV(pok):
	if pok['pokemon_id'] in POKEMON:
		if POKEMON[pok['pokemon_id']] is not None and validIV(pok):
			return getIVPercentage(pok) >= POKEMON[pok['pokemon_id']]
	return False

def isRaidValuable(raid):
	return raid['level'] in RAIDS and (RAIDS[raid['level']] == None or raid['pokemon_id'] in RAIDS[raid['level']])

def isRaidWithinBoundaries(raid):
	return raidArea.contains_points([(raid['lat'], raid['lng'])])[0]

def isRare(pok):
	if pok['pokemon_id'] in POKEMON:
		return True

def postDiscord(type, obj):
	response = requests.post(
		url = WEBHOOKS[type],
		data = 'content=' + getDiscordString(type, obj)
	)

def validIV(pok):
	return pok['attack'] != -1 and pok['defence'] != -1 and pok['stamina'] != -1

class Clear(webapp2.RequestHandler):
	def get(self):
		global seenList
		global seenRaidList
		upperBound = int(time.time()) - 600 # 10 min buffer
		for pok in seenList:
			endTime = int(pok['despawn'])
			if endTime < upperBound:
				seenList.remove(pok)
		for raid in seenRaidList:
			endTime = int(raid['raid_end']) - 600
			if endTime < upperBound:
				seenRaidList.remove(raid)

class Pokemon(webapp2.RequestHandler):
	def get(self):
		global seenList
		response = requests.get(
			url = getUrl(),
			headers = { 'Referer': 'https://sydneypogomap.com/?forcerefresh' }
		)
		monList = response.json()
		if 'pokemons' not in monList:
			return
		for pok in monList['pokemons']:
			if not pok in seenList:
				seenList.append(pok)
				if isRare(pok):
					postDiscord(Scan.RARE_MON, pok)
				if isHighIV(pok):
					postDiscord(Scan.HIGH_MON, pok)

class Raids(webapp2.RequestHandler):
	def get(self):
		global seenRaidList
		response = requests.get(
			url = 'https://sydneypogomap.com/raids.php',
			headers = { 'Referer': 'https://sydneypogomap.com/gym.html' }
		)
		raidList = response.json()
		if 'raids' not in raidList:
			return
		for raid in raidList['raids']:
			if not raid in seenRaidList:
				seenRaidList.append(raid)
				if isRaidValuable(raid) and isRaidWithinBoundaries(raid):
					postDiscord(Scan.RAID, raid)

app = webapp2.WSGIApplication([
	('/clear', Clear),
	('/raids', Raids),
	('/mon', Pokemon)
])


import setuptools
import sys

with open("README.md", "r") as fh:
    long_description = fh.read()

# Default
version = "SNAPSHOT"

if "--version" in sys.argv:
    idx = sys.argv.index("--version")
    sys.argv.pop(idx)
    version = sys.argv.pop(idx)

print("Using version " + version)

setuptools.setup(
    name='py-canary-2',
    version=version,
    author="Brian Marks",
    description="Python API for the Canary App",
    license="MIT",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/bm1549/canary",
    packages=setuptools.find_packages(),
    install_requires=[
        "requests>=2.25.1",
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
     ],
 )


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /thrift/TSCons.py
# Compiled at: 2018-09-11 21:54:05
# Size of source mod 2**32: 1309 bytes
from os import path
from SCons.Builder import Builder
from six.moves import map

def scons_env(env, add=''):
    opath = path.dirname(path.abspath('$TARGET'))
    lstr = 'thrift --gen cpp -o ' + opath + ' ' + add + ' $SOURCE'
    cppbuild = Builder(action=lstr)
    env.Append(BUILDERS={'ThriftCpp': cppbuild})

def gen_cpp(env, dir, file):
    scons_env(env)
    suffixes = ['_types.h', '_types.cpp']
    targets = map(lambda s: 'gen-cpp/' + file + s, suffixes)
    return env.ThriftCpp(targets, dir + file + '.thrift')


import functools
import numpy as np
import regreg.api as rr
import copy
from selection.randomized.M_estimator import restricted_Mest
from selection.api import randomization

class CV(object):

    def __init__(self, loss, folds, lam_seq, objective_randomization=None, epsilon=None):

        (self.loss,
         self.folds,
         self.lam_seq,
         self.objective_randomization,
         self.epsilon) = (loss,
                          folds,
                          lam_seq,
                          objective_randomization,
                          epsilon)

        if self.objective_randomization is not None:
            if self.epsilon is None:
                X, _ = loss.data
                n = X.shape[0]
                self.epsilon = np.true_divide(1, np.sqrt(n))
        self.K = len(np.unique(self.folds))

    def CV_err(self,
               penalty,
               loss = None,
               residual_randomization = None,
               scale = None,
               solve_args = {'min_its':20, 'tol':1.e-1}):
        """
        Computes the non-randomized CV error and the one with added residual randomization
        """
        if loss is None:
            loss = copy.copy(self.loss)
        X, y = loss.data
        n, p = X.shape

        CV_err = 0
        CV_err_squared = 0

        if residual_randomization is not None:
            CV_err_randomized = 0
            CV_err_squared_randomized = 0
            if scale is None:
                scale = 1.

        for fold in np.unique(self.folds):
            test = self.folds == fold
            train = ~test

            loss_train = loss.subsample(train)
            loss_test = loss.subsample(test)
            X_test, y_test = X[test], y[test]
            n_test = y_test.shape[0]

            if self.objective_randomization is not None:
                randomized_train_loss = self.objective_randomization.randomize(loss_train, self.epsilon) # randomized train loss
                problem = rr.simple_problem(randomized_train_loss, penalty)
            else:
                problem = rr.simple_problem(loss_train, penalty)
            beta_train = problem.solve(**solve_args)

            #active = beta_train!=0
            #_beta_unpenalized = restricted_Mest(loss_train, active, solve_args=solve_args)
            #beta_full = np.zeros(p)
            #beta_full[active] = _beta_unpenalized

            _mu = lambda X, beta: loss_test.saturated_loss.mean_function(X.dot(beta))
            resid = y_test - _mu(X_test, beta_train)
            cur = (resid**2).sum() / n_test
            CV_err += cur
            CV_err_squared += (cur**2)

            if residual_randomization is not None:
                random_noise = scale * np.random.standard_normal(n_test)
                cur_randomized = ((resid + random_noise)**2).sum() / n_test
                CV_err_randomized += cur_randomized
                CV_err_squared_randomized += cur_randomized**2

        SD_CV = np.sqrt((CV_err_squared - ((CV_err**2)/self.K)) / float(self.K-1))
        if residual_randomization is not None:
            SD_CV_randomized = np.sqrt((CV_err_squared_randomized - (CV_err_randomized**2/self.K)) / (self.K-1))
            return CV_err, SD_CV, CV_err_randomized, SD_CV_randomized
        else:
            #print(CV_err, SD_CV)
            return CV_err, SD_CV

    def choose_lambda_CVr(self, scale = 1., loss=None):
        """
        Minimizes CV error curve without randomization and the one with residual randomization
        """
        if loss is None:
            loss = self.loss

        if not hasattr(self, 'scale'):
            self.scale = scale

        CV_curve = []
        X, _ = loss.data
        p = X.shape[1]
        for lam in self.lam_seq:
            penalty = rr.l1norm(p, lagrange=lam)
            # CV_curve.append(self.CV_err(penalty, loss) + (lam,))
            CV_curve.append(self.CV_err(penalty, loss, residual_randomization = True, scale = self.scale))

        CV_curve = np.array(CV_curve)
        CV_val = CV_curve[:,0]
        CV_val_randomized = CV_curve[:,2]
        lam_CV = self.lam_seq[np.argmin(CV_val)]
        lam_CV_randomized = self.lam_seq[np.argmin(CV_val_randomized)]

        SD_val = CV_curve[:,1]
        SD_val_randomized = CV_curve[:,3]
        return lam_CV, CV_val, SD_val, lam_CV_randomized, CV_val_randomized, SD_val_randomized

    def bootstrap_CVr_curve(self):
        """
        Bootstrap of CV error curve with residual randomization
        """

        def _boot_CV_curve(indices):
            X, y = self.loss.data
            n, p = X.shape
            folds_star = np.arange(n) % self.K
            np.random.shuffle(folds_star)
            loss_star = self.loss.subsample(indices)
            # loss_star = rr.glm.gaussian(X[indices,:], y[indices])
            result = self.choose_lambda_CVr(scale=self.scale, loss=loss_star)
            CV_val = result[1]
            return np.array(CV_val)

        def _boot_CVr_curve(indices):
            X, y = self.loss.data
            n, p = X.shape
            folds_star = np.arange(n) % self.K
            np.random.shuffle(folds_star)
            loss_star = self.loss.subsample(indices)
            #loss_star = rr.glm.gaussian(X[indices,:], y[indices])
            result = self.choose_lambda_CVr(scale=self.scale, loss=loss_star)
            CV_val_randomized = result[4]
            return np.array(CV_val_randomized)

        return _boot_CV_curve, _boot_CVr_curve

    def choose_lambda_CVR(self,  scale1=None, scale2=None, loss=None):
        """
        Minimizes CV error curve with additive randomization (CVR=CV+R1+R2=CV1+R2)
        """
        if loss is None:
            loss = copy.copy(self.loss)
        CV_curve = []
        X, _ = loss.data
        p = X.shape[1]
        for lam in self.lam_seq:
            penalty = rr.l1norm(p, lagrange=lam)
            #CV_curve.append(self.CV_err(penalty, loss) + (lam,))
            CV_curve.append(self.CV_err(penalty, loss))

        CV_curve = np.array(CV_curve)

        rv1, rv2 = np.zeros(self.lam_seq.shape[0]), np.zeros(self.lam_seq.shape[0])
        if scale1 is not None:
            randomization1 = randomization.isotropic_gaussian((self.lam_seq.shape[0],), scale=scale1)
            rv1 = np.asarray(randomization1._sampler(size=(1,)))
        if scale2 is not None:
            randomization2 = randomization.isotropic_gaussian((self.lam_seq.shape[0],), scale=scale2)
            rv2 = np.asarray(randomization2._sampler(size=(1,)))
        CVR_val = CV_curve[:,0]+rv1.flatten()+rv2.flatten()
        lam_CVR = self.lam_seq[np.argmin(CVR_val)] # lam_CVR minimizes CVR
        CV1_val = CV_curve[:,0]+rv1.flatten()

        SD = CV_curve[:,1]
        return lam_CVR, SD, CVR_val, CV1_val, self.lam_seq

    def bootstrap_CVR_curve(self, scale1=None, scale2=None):
        """
        Bootstrap of CVR=CV+R1+R2 and CV1=CV+R1 curves
        """
        def _bootstrap_CVerr_curve(indices):
            X, y = self.loss.data
            n, p = X.shape
            folds_star = np.arange(n) % self.K
            np.random.shuffle(folds_star)
            loss_star = self.loss.subsample(indices)
            #loss_star = rr.glm.gaussian(X[indices,:], y[indices])
            _, _, CVR_val, CV1_val, _ = self.choose_lambda_CVR(scale1, scale2, loss_star)
            return np.array(CVR_val), np.array(CV1_val)

        def _CVR_boot(indices):
            return _bootstrap_CVerr_curve(indices)[0]
        def _CV1_boot(indices):
            return _bootstrap_CVerr_curve(indices)[1]

        return _CVR_boot, _CV1_boot

if __name__ == '__main__':
    from selection.tests.instance import gaussian_instance
    np.random.seed(1)
    n, p = 3000, 1000
    X, y, beta, nonzero, sigma = gaussian_instance(n=n, p=p, s=30, rho=0., sigma=1)
    loss = rr.glm.gaussian(X,y)
    lam_seq = np.exp(np.linspace(np.log(1.e-6), np.log(1), 30)) * np.fabs(np.dot(X.T,y)).max()
    K = 5
    folds = np.arange(n) % K
    CV_compute = CV(loss, folds, lam_seq)
    lam_CV, CV_val, SD_val, lam_CV_randomized, CV_val_randomized, SD_val_randomized = CV_compute.choose_lambda_CVr()
    #print("CV error curve (nonrandomized):", CV_val)

    minimum_CV = np.min(CV_val)
    lam_idx = list(lam_seq).index(lam_CV)
    SD_min = SD_val[lam_idx]
    lam_1SD = lam_seq[max([i for i in range(lam_seq.shape[0]) if CV_val[i] <= minimum_CV + SD_min])]

    #print(lam_CV, lam_1SD)
    import matplotlib.pyplot as plt
    plt.plot(np.log(lam_seq), CV_val)
    plt.show()


class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        n = len(nums)
        start = 0
        end = n - 1
        ans = -1
        while(ans != n - k):
            ans = self.quick_sort(nums, start, end)
            if ans < n - k:
                start = ans + 1
            elif ans > n - k:
                end = ans - 1
        return nums[ans]

    def quick_sort(self, nums, start, end):
        # 取最后一位作为分解符
        q = nums[end]
        # 小技巧i从-1开始，判断j，则可以定位i+1为第一个大于q，j为第一个小于q
        i = start - 1
        for j in range(start, end):
            if nums[j] < q:
                i += 1
                nums[i], nums[j] = nums[j], nums[i]
        nums[i + 1], nums[end] = nums[end], nums[i + 1]
        return i + 1


from peak.api import *

class StreamProto(binding.Component):

    reactor = binding.Obtain(running.IBasicReactor)
    socket  = binding.Require("Socket to run the protocol on")
    bufsize = binding.Make(lambda: 2048)
    _fileno = binding.Make(lambda self: self.socket.fileno())
    _o_buf  = binding.Make(list)

    _writer_added = False

    def fileno(self):
        return self._fileno

    def doRead(self):
        d = self.socket.recv(self.bufsize)
        if not d:
            self.connectionLost()
        else:
            self.dataReceived(d)

    def connectionLost(self):
        self.socket.close()

    def dataReceived(self, data):
        pass

    def write(self, data):
        ob = self._o_buf
        ob.append(data)
        if ob and not self._writer_added:
            self._writer_added = True
            self.reactor.addWriter(self)

    def doWrite(self):
        ob = self._o_buf
        while ob:
            d = ob.pop(0)
            l = len(d)
            sl = self.socket.send(d)
            if sl < l:
                ob.insert(0, d[sl:])
                return

        if not ob and self._writer_added:
            self.reactor.removeWriter(self)
            self._writer_added = False


#
# snimpy -- Interactive SNMP tool
#
# Copyright (C) Vincent Bernat <bernat@luffy.cx>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""This module is a low-level interface to manipulate and
extract information from MIB files. It is a CFFI_ wrapper around
libsmi_. You may find convenient to use it in other projects but the
wrapper is merely here to serve *Snimpy* use and is therefore
incomplete.

.. _libsmi: http://www.ibr.cs.tu-bs.de/projects/libsmi/
.. _CFFI: http://cffi.readthedocs.org/
"""

from snimpy import cffi_fix  # nopep8
from cffi import FFI

ffi = FFI()
ffi.cdef("""

typedef char *SmiIdentifier;
typedef unsigned long SmiUnsigned32;
typedef long SmiInteger32;
typedef unsigned long long SmiUnsigned64;
typedef long long SmiInteger64;
typedef unsigned int SmiSubid;
typedef float SmiFloat32;
typedef double SmiFloat64;
typedef long double SmiFloat128;

typedef enum SmiBasetype {
    SMI_BASETYPE_INTEGER32,
    SMI_BASETYPE_OCTETSTRING,
    SMI_BASETYPE_OBJECTIDENTIFIER,
    SMI_BASETYPE_UNSIGNED32,
    SMI_BASETYPE_INTEGER64,
    SMI_BASETYPE_UNSIGNED64,
    SMI_BASETYPE_ENUM,
    SMI_BASETYPE_BITS,
    ...
} SmiBasetype;
typedef struct SmiType {
    SmiIdentifier       name;
    SmiBasetype         basetype;
    char                *format;
    ...;
} SmiType;

typedef enum SmiIndexkind {
    SMI_INDEX_INDEX,
    SMI_INDEX_AUGMENT,
    ...
} SmiIndexkind;

typedef unsigned int SmiNodekind;
#define SMI_NODEKIND_NODE         ...
#define SMI_NODEKIND_SCALAR       ...
#define SMI_NODEKIND_TABLE        ...
#define SMI_NODEKIND_ROW          ...
#define SMI_NODEKIND_COLUMN       ...

typedef struct SmiNode {
    SmiIdentifier       name;
    unsigned int        oidlen;
    SmiSubid            *oid;
    char                *format;
    SmiIndexkind        indexkind;
    int                 implied;
    SmiNodekind         nodekind;
    ...;
} SmiNode;

typedef struct SmiValue {
    SmiBasetype             basetype;
    union {
        SmiUnsigned64       unsigned64;
        SmiInteger64        integer64;
        SmiUnsigned32       unsigned32;
        SmiInteger32        integer32;
        SmiFloat32          float32;
        SmiFloat64          float64;
        SmiFloat128         float128;
        SmiSubid            *oid;
        char                *ptr;
    } value;
    ...;
} SmiValue;

typedef struct SmiRange {
    SmiValue            minValue;
    SmiValue            maxValue;
} SmiRange;

typedef struct SmiModule {
    SmiIdentifier       name;
    int                 conformance;
    ...;
} SmiModule;

typedef struct SmiElement {
    ...;
} SmiElement;

typedef struct SmiNamedNumber {
    SmiIdentifier       name;
    SmiValue            value;
} SmiNamedNumber;

int          smiInit(const char *);
void         smiExit(void);
void         smiSetErrorLevel(int);
void         smiSetFlags(int);
char        *smiLoadModule(const char *);
SmiModule   *smiGetModule(const char *);
SmiModule   *smiGetNodeModule(SmiNode *);
SmiType     *smiGetNodeType(SmiNode *);
SmiType     *smiGetParentType(SmiType *);
char        *smiRenderNode(SmiNode *, int);
SmiElement  *smiGetFirstElement(SmiNode *);
SmiElement  *smiGetNextElement(SmiElement *);
SmiNode     *smiGetElementNode(SmiElement *);
SmiRange    *smiGetFirstRange(SmiType *);
SmiRange    *smiGetNextRange(SmiRange *);
SmiNode     *smiGetNode(SmiModule *, const char *);
SmiNode     *smiGetFirstNode(SmiModule *, SmiNodekind);
SmiNode     *smiGetNextNode(SmiNode *, SmiNodekind);
SmiNode     *smiGetParentNode(SmiNode *);
SmiNode     *smiGetRelatedNode(SmiNode *);
SmiNode     *smiGetFirstChildNode(SmiNode *);
SmiNode     *smiGetNextChildNode(SmiNode *);
SmiNamedNumber *smiGetFirstNamedNumber(SmiType *);
SmiNamedNumber *smiGetNextNamedNumber(SmiNamedNumber *);

#define SMI_FLAG_ERRORS ...
#define SMI_FLAG_RECURSIVE ...
#define SMI_RENDER_ALL ...
""")

_smi = ffi.verify("""
#include <smi.h>
""", libraries=["smi"])

class SMIException(Exception):

    """SMI related exception. Any exception thrown in this module is
    inherited from this one."""

class Node(object):

    """MIB node. An instance of this class represents a MIB node. It
    can be specialized by other classes, like :class:`Scalar`,
    :class:`Table`, :class:`Column`, :class:`Node`.
    """

    def __init__(self, node):
        """Create a new MIB node.

        :param node: libsmi node supporting this node.
        """
        self.node = node

    @property
    def type(self):
        """Get the basic type associated with this node.

        :return: The class from :mod:`basictypes` module which can
            represent the node. When retrieving a valid value for
            this node, the returned class can be instanciated to get
            an appropriate representation.
        """
        from snimpy import basictypes
        t = _smi.smiGetNodeType(self.node)
        target = {
            _smi.SMI_BASETYPE_INTEGER32: basictypes.Integer,
            _smi.SMI_BASETYPE_INTEGER64: basictypes.Integer,
            _smi.SMI_BASETYPE_UNSIGNED32: {b"TimeTicks": basictypes.Timeticks,
                                           None: basictypes.Unsigned32},
            _smi.SMI_BASETYPE_UNSIGNED64: basictypes.Unsigned64,
            _smi.SMI_BASETYPE_OCTETSTRING: {b"IpAddress": basictypes.IpAddress,
                                            None: basictypes.OctetString},
            _smi.SMI_BASETYPE_OBJECTIDENTIFIER: basictypes.Oid,
            _smi.SMI_BASETYPE_ENUM: {b"TruthValue": basictypes.Boolean,
                                     None: basictypes.Enum},
            _smi.SMI_BASETYPE_BITS: basictypes.Bits
        }.get(t.basetype, None)
        if isinstance(target, dict):
            tt = _smi.smiGetParentType(t)
            target = target.get((t.name != ffi.NULL and ffi.string(t.name)) or
                                (tt.name != ffi.NULL and ffi.string(
                                    tt.name)) or None,
                                target.get(None, None))

        if target is None:
            raise SMIException("unable to retrieve type of node")
        return target

    @property
    def fmt(self):
        """Get node format. The node format is a string to use to display
        a user-friendly version of the node. This is can be used for
        both octet strings or integers (to make them appear as decimal
        numbers).

        :return: The node format as a string or None if there is no
            format available.

        """
        t = _smi.smiGetNodeType(self.node)
        tt = _smi.smiGetParentType(t)
        f = (t != ffi.NULL and t.format != ffi.NULL and ffi.string(t.format) or
             tt != ffi.NULL and tt.format != ffi.NULL and
             ffi.string(tt.format)) or None
        if f is None:
            return None
        return f.decode("ascii")

    @property
    def oid(self):
        """Get OID for the current node. The OID can then be used to request
        the node from an SNMP agent.

        :return: OID as a tuple
        """
        return tuple([self.node.oid[i] for i in range(self.node.oidlen)])

    @property
    def ranges(self):
        """Get node ranges. An node can be restricted by a set of
        ranges. For example, an integer can only be provided between
        two values. For strings, the restriction is on the length of
        the string.

        The returned value can be `None` if no restriction on range
        exists for the current node, a single value if the range is
        fixed or a list of tuples or fixed values otherwise.

        :return: The valid range for this node.
        """
        t = _smi.smiGetNodeType(self.node)
        if t == ffi.NULL:
            return None

        ranges = []
        range = _smi.smiGetFirstRange(t)
        while range != ffi.NULL:
            m1 = self._convert(range.minValue)
            m2 = self._convert(range.maxValue)
            if m1 == m2:
                ranges.append(m1)
            else:
                ranges.append((m1, m2))
            range = _smi.smiGetNextRange(range)
        if len(ranges) == 0:
            return None
        if len(ranges) == 1:
            return ranges[0]
        return ranges

    @property
    def enum(self):
        """Get possible enum values. When the node can only take a discrete
        number of values, those values are defined in the MIB and can
        be retrieved through this property.

        :return: The dictionary of possible values keyed by the integer value.
        """
        t = _smi.smiGetNodeType(self.node)
        if t == ffi.NULL or t.basetype not in (_smi.SMI_BASETYPE_ENUM,
                                               _smi.SMI_BASETYPE_BITS):
            return None

        result = {}
        element = _smi.smiGetFirstNamedNumber(t)
        while element != ffi.NULL:
            result[self._convert(element.value)] = ffi.string(
                element.name).decode("ascii")
            element = _smi.smiGetNextNamedNumber(element)
        return result

    def __str__(self):
        return ffi.string(self.node.name).decode("ascii")

    def __repr__(self):
        r = _smi.smiRenderNode(self.node, _smi.SMI_RENDER_ALL)
        if r == ffi.NULL:
            return "<uninitialized {0} object at {1}>".format(
                self.__class__.__name__, hex(id(self)))
        module = _smi.smiGetNodeModule(self.node)
        if module == ffi.NULL:
            raise SMIException("unable to get module for {0}".format(
                self.node.name))
        return "<{0} {1} from '{2}'>".format(self.__class__.__name__,
                                             ffi.string(r),
                                             ffi.string(module.name))

    def _convert(self, value):
        attr = {_smi.SMI_BASETYPE_INTEGER32: "integer32",
                _smi.SMI_BASETYPE_UNSIGNED32: "unsigned32",
                _smi.SMI_BASETYPE_INTEGER64: "integer64",
                _smi.SMI_BASETYPE_UNSIGNED64: "unsigned64"}.get(value.basetype,
                                                                None)
        if attr is None:
            raise SMIException("unexpected type found in range")
        return getattr(value.value, attr)

class Scalar(Node):

    """MIB scalar node. This class represents a scalar value in the
    MIB. A scalar value is a value not contained in a table.
    """

class Table(Node):

    """MIB table node. This class represents a table. A table is an
    ordered collection of objects consisting of zero or more
    rows. Each object in the table is identified using an index. An
    index can be a single value or a list of values.
    """

    @property
    def columns(self):
        """Get table columns. The columns are the different kind of objects
        that can be retrieved in a table.

        :return: list of table columns (:class:`Column` instances)

        """
        child = _smi.smiGetFirstChildNode(self.node)
        if child == ffi.NULL:
            return []
        if child.nodekind != _smi.SMI_NODEKIND_ROW:
            raise SMIException("child {0} of {1} is not a row".format(
                ffi.string(child.name),
                ffi.string(self.node.name)))
        columns = []
        child = _smi.smiGetFirstChildNode(child)
        while child != ffi.NULL:
            if child.nodekind != _smi.SMI_NODEKIND_COLUMN:
                raise SMIException("child {0} of {1} is not a column".format(
                    ffi.string(child.name),
                    ffi.string(self.node.name)))
            columns.append(Column(child))
            child = _smi.smiGetNextChildNode(child)
        return columns

    @property
    def _row(self):
        """Get the table row.

        :return: row object (as an opaque object)
        """
        child = _smi.smiGetFirstChildNode(self.node)
        if child != ffi.NULL and child.indexkind == _smi.SMI_INDEX_AUGMENT:
            child = _smi.smiGetRelatedNode(child)
            if child == ffi.NULL:
                raise SMIException("AUGMENT index for {0} but "
                                   "unable to retrieve it".format(
                                       ffi.string(self.node.name)))
        if child == ffi.NULL:
            raise SMIException("{0} does not have a row".format(
                ffi.string(self.node.name)))
        if child.nodekind != _smi.SMI_NODEKIND_ROW:
            raise SMIException("child {0} of {1} is not a row".format(
                ffi.string(child.name),
                ffi.string(self.node.name)))
        if child.indexkind != _smi.SMI_INDEX_INDEX:
            raise SMIException("child {0} of {1} has an unhandled "
                               "kind of index".format(
                                   ffi.string(child.name),
                                   ffi.string(self.node.name)))
        return child

    @property
    def implied(self):
        """Is the last index implied? An implied index is an index whose size
        is not fixed but who is not prefixed by its size because this
        is the last index of a table.

        :return: `True` if and only if the last index is implied.
        """
        child = self._row
        if child.implied:
            return True
        return False

    @property
    def index(self):
        """Get indexes for a table. The indexes are used to locate a precise
        row in a table. They are a subset of the table columns.

        :return: The list of indexes (as :class:`Column` instances) of
            the table.
        """
        child = self._row
        lindex = []
        element = _smi.smiGetFirstElement(child)
        while element != ffi.NULL:
            nelement = _smi.smiGetElementNode(element)
            if nelement == ffi.NULL:
                raise SMIException("cannot get index "
                                   "associated with {0}".format(
                                       ffi.string(self.node.name)))
            if nelement.nodekind != _smi.SMI_NODEKIND_COLUMN:
                raise SMIException("index {0} for {1} is "
                                   "not a column".format(
                                       ffi.string(nelement.name),
                                       ffi.string(self.node.name)))
            lindex.append(Column(nelement))
            element = _smi.smiGetNextElement(element)
        return lindex

class Column(Node):

    """MIB column node. This class represent a column of a table."""

    @property
    def table(self):
        """Get table associated with this column.

        :return: The :class:`Table` instance associated to this
            column.
        """
        parent = _smi.smiGetParentNode(self.node)
        if parent == ffi.NULL:
            raise SMIException("unable to get parent of {0}".format(
                ffi.string(self.node.name)))
        if parent.nodekind != _smi.SMI_NODEKIND_ROW:
            raise SMIException("parent {0} of {1} is not a row".format(
                ffi.string(parent.name),
                ffi.string(self.node.name)))
        parent = _smi.smiGetParentNode(parent)
        if parent == ffi.NULL:
            raise SMIException("unable to get parent of {0}".format(
                ffi.string(self.node.name)))
        if parent.nodekind != _smi.SMI_NODEKIND_TABLE:
            raise SMIException("parent {0} of {1} is not a table".format(
                ffi.string(parent.name),
                ffi.string(self.node.name)))
        t = Table(parent)
        return t

def reset():
    """Reset libsmi to its initial state."""
    _smi.smiExit()
    if _smi.smiInit(b"snimpy") < 0:
            raise SMIException("unable to init libsmi")
    _smi.smiSetErrorLevel(0)
    try:
        _smi.smiSetFlags(_smi.SMI_FLAG_ERRORS | _smi.SMI_FLAG_RECURSIVE)
    except TypeError:
        pass                    # We are being mocked

def _get_module(name):
    """Get the SMI module from its name.

    :param name: The name of the module
    :return: The SMI module or `None` if not found (not loaded)
    """
    if not isinstance(name, bytes):
        name = name.encode("ascii")
    m = _smi.smiGetModule(name)
    if m == ffi.NULL:
        return None
    if m.conformance and m.conformance <= 1:
        return None
    return m

def _kind2object(kind):
    return {
        _smi.SMI_NODEKIND_NODE: Node,
        _smi.SMI_NODEKIND_SCALAR: Scalar,
        _smi.SMI_NODEKIND_TABLE: Table,
        _smi.SMI_NODEKIND_COLUMN: Column
    }.get(kind, Node)

def get(mib, name):
    """Get a node by its name.

    :param mib: The MIB name to query
    :param name: The object name to get from the MIB
    :return: the requested MIB node (:class:`Node`)
    """
    if not isinstance(mib, bytes):
        mib = mib.encode("ascii")
    module = _get_module(mib)
    if module is None:
        raise SMIException("no module named {0}".format(mib))
    node = _smi.smiGetNode(module, name.encode("ascii"))
    if node == ffi.NULL:
        raise SMIException("in {0}, no node named {1}".format(
            mib, name))
    pnode = _kind2object(node.nodekind)
    return pnode(node)

def _get_kind(mib, kind):
    """Get nodes of a given kind from a MIB.

    :param mib: The MIB name to search objects for
    :param kind: The SMI kind of object
    :return: The list of matched MIB nodes for the MIB
    """
    if not isinstance(mib, bytes):
        mib = mib.encode("ascii")
    module = _get_module(mib)
    if module is None:
        raise SMIException("no module named {0}".format(mib))
    lnode = []
    node = _smi.smiGetFirstNode(module, kind)
    while node != ffi.NULL:
        lnode.append(_kind2object(kind)(node))
        node = _smi.smiGetNextNode(node, kind)
    return lnode

def getNodes(mib):
    """Return all nodes from a given MIB.

    :param mib: The MIB name
    :return: The list of all MIB nodes for the MIB
    :rtype: list of :class:`Node` instances
    """
    return _get_kind(mib, _smi.SMI_NODEKIND_NODE)

def getScalars(mib):
    """Return all scalars from a given MIB.

    :param mib: The MIB name
    :return: The list of all scalars for the MIB
    :rtype: list of :class:`Scalar` instances
    """
    return _get_kind(mib, _smi.SMI_NODEKIND_SCALAR)

def getTables(mib):
    """Return all tables from a given MIB.

    :param mib: The MIB name
    :return: The list of all tables for the MIB
    :rtype: list of :class:`Table` instances
    """
    return _get_kind(mib, _smi.SMI_NODEKIND_TABLE)

def getColumns(mib):
    """Return all columns from a givem MIB.

    :param mib: The MIB name
    :return: The list of all columns for the MIB
    :rtype: list of :class:`Column` instances
    """
    return _get_kind(mib, _smi.SMI_NODEKIND_COLUMN)

def load(mib):
    """Load a MIB into the library.

    :param mib: The MIB to load, either a filename or a MIB name.
    :return: The MIB name that has been loaded.
    :except SMIException: The requested MIB cannot be loaded.
    """
    if not isinstance(mib, bytes):
        mib = mib.encode("ascii")
    modulename = _smi.smiLoadModule(mib)
    if modulename == ffi.NULL:
        raise SMIException("unable to load {0}".format(mib))
    modulename = ffi.string(modulename)
    if not _get_module(modulename.decode("ascii")):
        raise SMIException("{0} contains major SMI error "
                           "(check with smilint -s -l1)".format(mib))
    return modulename

reset()


import functools as ft

import tensorflow as tf
import tensorflow_probability as tfp

import src.bnn.bnn as bnn_fn

def pre_train_nn(X_train, y_train, nodes_per_layer, epochs=100):
    """Pre-train NN to get good starting point for HMC.

    Args:
        nodes_per_layer (list): the number of nodes in each dense layer
        X_train (Tensor or np.array): training samples
        y_train (Tensor or np.array): training labels

    Returns:
        Tensor: list of tensors specifying the weights of the trained network
        model: Keras Sequential model
    """
    last_layer = nodes_per_layer.pop(-1)
    model = tf.keras.Sequential()
    model.add(tf.keras.Input(nodes_per_layer.pop(0)))
    for units in nodes_per_layer:
        model.add(tf.keras.layers.Dense(units, activation="relu"))
    model.add(tf.keras.layers.Dense(last_layer, activation="linear"))

    model.compile(loss="mse", optimizer="adam")
    model.fit(X_train, y_train, epochs=epochs, verbose=0)
    return [tf.convert_to_tensor(w) for w in model.get_weights()], model

def trace_fn(current_state, kernel_results, summary_freq=10, callbacks=[]):
    """Can be passed to the HMC kernel to obtain a trace of intermediate
    kernel results and histograms of the network parameters in Tensorboard.
    """
    step = kernel_results.step
    with tf.summary.record_if(tf.equal(step % summary_freq, 0)):
        for idx, tensor in enumerate(current_state, 1):
            count = str(int(idx / 2) + 1)
            name = "weights_" if idx % 2 == 0 else "biases_" + count
            tf.summary.histogram(name, tensor, step=step)
        return kernel_results, [cb(*current_state) for cb in callbacks]

# @tf.function
def sample_chain(*args, **kwargs):
    """Compile static graph for tfp.mcmc.sample_chain.
    Since this is bulk of the computation, using @tf.function here
    significantly improves performance (empirically about ~5x).
    """
    return tfp.mcmc.sample_chain(*args, **kwargs)

def run_hmc(
    target_log_prob_fn,
    step_size=.0001,
    num_leapfrog_steps=1000,
    num_burnin_steps=500,
    num_results=100,
    current_state=None,
    resume=None,
    log_dir="logs/hmc/",
    sampler="hmc",
    step_size_adapter="dual",
    **kwargs,
):
    """Creates an adaptive HMC kernel and generates a Markov chain of length num_results
    by performing gradient-informed Hamiltonian Monte Carlo transitions. Either the new
    or current position in parameter space is appended to the chain after each transition
    depending on a Metropolis accept/reject.

    Args:
        target_log_prob_fn {callable}: Determines the stationary distribution
        the Markov chain should converge to.

    Returns:
        burnin(s), chain(s), trace, final_kernel_result: Discarded samples generated during warm-up,
        the Markov chain(s), the trace generated by trace_fn and the kernel results of the last step
        (in case the computation needs to be resumed).
    """
    err = "Either current_state or resume is required when calling run_hmc"
    assert current_state is not None or resume is not None, err

    summary_writer = tf.summary.create_file_writer(log_dir)

    step_size_adapter = {
        "simple": tfp.mcmc.SimpleStepSizeAdaptation,
        "dual_averaging": tfp.mcmc.DualAveragingStepSizeAdaptation,
    }[step_size_adapter]
    if sampler == "nuts":
        kernel = tfp.mcmc.NoUTurnSampler(target_log_prob_fn, step_size=step_size)
        adaptive_kernel = step_size_adapter(
            kernel,
            num_adaptation_steps=num_burnin_steps,
            step_size_setter_fn=lambda pkr, new_step_size: pkr._replace(
                step_size=new_step_size
            ),
            step_size_getter_fn=lambda pkr: pkr.step_size,
            log_accept_prob_getter_fn=lambda pkr: pkr.log_accept_ratio,
        )
    elif sampler == "hmc":
        kernel = tfp.mcmc.HamiltonianMonteCarlo(
            target_log_prob_fn,
            step_size=step_size,
            num_leapfrog_steps=num_leapfrog_steps,
        )
        adaptive_kernel = step_size_adapter(
            kernel, num_adaptation_steps=num_burnin_steps
        )

    if resume:
        prev_chain, prev_trace, prev_kernel_results = resume
        step = len(prev_chain)
        current_state = tf.nest.map_structure(lambda chain: chain[-1], prev_chain)
    else:
        prev_kernel_results = adaptive_kernel.bootstrap_results(current_state)
        step = 0

    tf.summary.trace_on(graph=True, profiler=False)
    ########
    print('Will sample chain')
    #######
    chain, trace, final_kernel_results = sample_chain(
        kernel=adaptive_kernel,
        current_state=current_state,
        previous_kernel_results=prev_kernel_results,
        num_results=num_results + num_burnin_steps,
        trace_fn=ft.partial(trace_fn, summary_freq=20),
        return_final_kernel_results=True,
        **kwargs,
    )

    with summary_writer.as_default():
        tf.summary.trace_export(name="hmc_trace", step=step)
    summary_writer.close()

    if resume:
        chain = nest_concat(prev_chain, chain)
        trace = nest_concat(prev_trace, trace)
    burnin, samples = zip(*[(t[:-num_results], t[-num_results:]) for t in chain])
    return burnin, samples, trace, final_kernel_results

def predict_from_chain(chain, X_test, uncertainty="aleatoric+epistemic"):
    """Takes a Markov chain of NN configurations and does the actual
    prediction on a test set X_test including aleatoric and optionally
    epistemic uncertainty estimation.
    """
    err = f"unrecognized uncertainty type: {uncertainty}"
    assert uncertainty in ["aleatoric", "aleatoric+epistemic"], err

    if uncertainty == "aleatoric+epistemic":
        restructured_chain = [
            [tensor[i] for tensor in chain] for i in range(len(chain[0]))
        ]

        def predict_from_sample(sample):
            post_weights, post_biases = sample[::2], sample[1::2]
            post_model = bnn_fn.build_network(post_weights, post_biases)
            y_pred = post_model(X_test, training=False)
            return y_pred

        y_pred_mc_samples = [predict_from_sample(sample) for sample in restructured_chain]

        '''y_pred, y_var_epist = tf.nn.moments(
            tf.convert_to_tensor(y_pred_mc_samples), axes=0
        )'''
        #y_var_aleat = tf.reduce_mean(tf.convert_to_tensor(y_var_mc_samples), axis=0)
        #y_var_tot = y_var_epist + y_var_aleat
        return y_pred_mc_samples

def hmc_predict(
    weight_prior, bias_prior, init_state, X_train, y_train, X_test, y_test=None, **kwds
):
    """Top-level function that ties together run_hmc and predict_from_chain by accepting
    a train and test set plus parameter priors to construct the BNN's log probability
    function given the training data X_train, y_train.
    """
    default = dict(
        step_size_adapter="dual_averaging"
    )
    kwds = {**default, **kwds}
    bnn_log_prob_fn = bnn_fn.target_log_prob_fn_factory(
        weight_prior, bias_prior, X_train, y_train
    )
    print('Created log prob fn')
    burnin, samples, trace, final_kernel_results = run_hmc(
        bnn_log_prob_fn, current_state=init_state, **kwds
    )
    print('run_hmc COMPLETE')
    y_pred = predict_from_chain(samples, X_test)
    return y_pred,trace, final_kernel_results,samples

def nest_concat(*args, axis=0):
    """Utility function for concatenating a new Markov chain or trace with
    older ones when resuming a previous calculation.
    """
    return tf.nest.map_structure(lambda *parts: tf.concat(parts, axis=axis), *args)

def ess(chains, **kwargs):
    """Measure effective sample size of Markov chain(s).

    Arguments:
        chains {Tensor or list of Tensors}: If list, first
            dimension should index identically distributed states.
    """
    return tfp.mcmc.effective_sample_size(chains, **kwargs)

def r_hat(tensors):
    """See https://tensorflow.org/probability/api_docs/python/tfp/mcmc/potential_scale_reduction.
    """
    return [tfp.mcmc.diagnostic.potential_scale_reduction(t) for t in tensors]

def get_num_chains(target_log_prob_fn, current_state):
    """Check how many chains your kernel thinks it's dealing
    with. Can help with debugging.
    """
    return tf.size(target_log_prob_fn(*current_state)).numpy()


#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

"""
Exercise 12.3
--------------
Write a function called most_frequent that takes a string and prints the letters
in decreasing order of frequency. Find text samples from several different languages and see
how letter frequency varies between languages.
"""

import string

white_space = string.whitespace

def make_histogram(string):
    hist = {}
    for letter in string:
        if letter not in white_space:
            temp = letter.lower()
            hist[temp] = hist.get(temp, 0) + 1
    return hist

def most_frequent(hist):
    t = sorted(hist.items(), key=lambda item: item[1], reverse=True)
    total_letters = sum(hist.values())
    for letter, freq in t:
        print(letter, ((freq / total_letters) * 100))

def read_text():
    fin = open('words.txt').read()
    h = make_histogram(fin)
    most_frequent(h)

if __name__ == '__main__':
    read_text()


import matplotlib.pyplot as plt
import pandas as pd

population = pd.read_csv("API_SP.POP.TOTL_DS2_en_csv_v2_10576638.csv", skiprows=4, delimiter=",")
wealth = pd.read_csv("7197c8d1-6dea-4393-b1d8-c7b4662d6aab_Data.csv", delimiter=",")

popu2017 = pd.DataFrame(population, columns = ["Country Code","Country Name", "2017"])
gdp2017 = pd.DataFrame(wealth, columns = ["Country Code","Country Name", "2017 [YR2017]"])

gdp2017.rename(columns={"2017 [YR2017]":"GDP_2017"}, inplace=True)

gdp2017 = gdp2017[gdp2017.GDP_2017 != '..']

popugdp2017 = pd.merge(popu2017, gdp2017, on="Country Name")

popugdp2017.drop('Country Code_x', axis=1)

popugdp2017.rename(columns={"Country Code_y":"Code","2017":"Population_2017"}, inplace=True)

GDP = []
for item in popugdp2017["GDP_2017"]:
    GDP.append (float(item))

popugdp2017["GDP_2017"] = GDP

plt.scatter(popugdp2017["Population_2017"],popugdp2017["GDP_2017"]/popugdp2017["Population_2017"], c=popugdp2017["GDP_2017"] ,edgecolor="black", cmap = "Reds", s=popugdp2017["Population_2017"]/1000000, alpha=0.75)
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Population")
plt.ylabel("Per Capita Income")
plt.title ("2017")

plt.show()


from argparse import ArgumentParser
import os
from mmdet.apis import inference_detector, init_detector, show_result_pyplot
import json
import numpy

def main():
    parser = ArgumentParser()
    # parser.add_argument('img', help='Image file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--score-thr', type=float, default=0.000000001, help='bbox score threshold')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    # test a single image
    # import pdb;
    # pdb.set_trace()
    #

    test_img_dir="/home/dell/桌面/tile_round1_testA_20201231/tile_round1_testA_20201231/cam1/crop/"
    imgs_dir=os.listdir(test_img_dir)
    json_result=[]
    for img in imgs_dir:
        img_name=img
        img=test_img_dir+img
        result = inference_detector(model, img)
        # show the results
        show_result_pyplot(model,result,img,img_name,json_result=json_result,score_thr=args.score_thr)

    with open("/home/dell/桌面/guangdong/result_cam1.json", "w") as f:
        json.dump(json_result, f,cls=MyEncoder,indent=6)
    print("加载入文件完成...")

######################转换写入json类型
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)

if __name__ == '__main__':
    main()