# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
import os

version = open(os.path.join("sc", "apyb", "pythonbrasil6", "version.txt")).read().strip()

setup(name='sc.apyb.pythonbrasil6',
      version=version,
      description="Site policy for PythonBrasil[7]",
      long_description=open(os.path.join("sc", "apyb", "pythonbrasil6", "README.txt")).read() + "\n" +
                       open(os.path.join("docs", "HISTORY.txt")).read(),
      # Get more strings from http://www.python.org/pypi?%3Aaction=list_classifiers
      classifiers=[
        "Framework :: Plone",
        "Framework :: Zope2",
        "Framework :: Zope3",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        ],
      keywords='apyb plone pythonbrasil plone3',
      author='Simples Consultoria',
      author_email='products@simplesconsultoria.com.br',
      url='http://www.simplesconsultoria.com.br/',
      license='GPL',
      packages=find_packages(exclude=['ez_setup']),
      namespace_packages=['sc', 'sc.apyb'],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          # -*- Extra requirements: -*-
          'plone.app.blob==1.5',
          'plone.app.imaging==1.0.5',
          'archetypes.schematuning==1.2',
          'beyondskins.pythonbrasil.site==3.0.1',
          'Products.LinguaPlone==3.0.1',
          'Products.CMFContentPanels==2.6a7',
          'Products.PloneFormGen==1.6.0',
          'Products.CacheSetup==1.2.1',
          'Products.ATGoogleVideo==0.8.2',
          'Products.PlonePopoll==2.7.0-beta2',
          'Products.PyConBrasil==2.4.1',
          'Products.Maps==2.0.3',
          'sc.social.bookmarks==0.8',
          'collective.easyslider==0.3.1',
          'sc.kupu.objectsupport==1.0',
          'Products.RedirectionTool==1.2.1',
          'collective.captcha==1.4',
          'experimental.contentcreation==1.0rc1',
          'experimental.catalogqueryplan==3.0',
          'experimental.opaquespeedup==1.0',
          'experimental.daterangeindexoptimisations==1.0dev-r77925',
          'Products.contentmigration==2.0.1',
      ],
      entry_points="""
      # -*- Entry points: -*-

      [z3c.autoinclude.plugin]
      target = plone
      """,
      )


import requests
import random
import time

from flask import Flask, Response, jsonify, send_from_directory
from flask import request as flask_request
from flask_cors import CORS

from bootstrap import create_app
from models import Advertisement, db

app = create_app()
CORS(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

@app.route('/')
def hello():
    app.logger.info("home url for ads called")
    return Response({'Hello from Advertisements!': 'world'}, mimetype='application/json')

@app.route('/banners/<path:banner>')
def banner_image(banner):
    app.logger.info(f"attempting to grab banner at {banner}")
    return send_from_directory('ads', banner)

@app.route('/weighted-banners/<float:weight>')
def weighted_image(weight):
    app.logger.info(f"attempting to grab banner weight of less than {weight}")
    advertisements = Advertisement.query.all()
    for ad in advertisements:
        if ad.weight < weight:
            return jsonify(ad.serialize())

@app.route('/ads', methods=['GET', 'POST'])
def status():
    if flask_request.method == 'GET':

        try:
            advertisements = Advertisement.query.all()
            app.logger.info(f"Total advertisements available: {len(advertisements)}")
            return jsonify([b.serialize() for b in advertisements])

        except:
            app.logger.error("An error occurred while getting ad.")
            err = jsonify({'error': 'Internal Server Error'})
            err.status_code = 500
            return err

    elif flask_request.method == 'POST':

        try:
            # create a new advertisement with random name and value
            advertisements_count = len(Advertisement.query.all())
            new_advertisement = Advertisement('Advertisement ' + str(discounts_count + 1),
                                    '/',
                                    random.randint(10,500))
            app.logger.info(f"Adding advertisement {new_advertisement}")
            db.session.add(new_advertisement)
            db.session.commit()
            advertisements = Advertisement.query.all()

            return jsonify([b.serialize() for b in advertisements])

        except:

            app.logger.error("An error occurred while creating a new ad.")
            err = jsonify({'error': 'Internal Server Error'})
            err.status_code = 500
            return err

    else:
        err = jsonify({'error': 'Invalid request method'})
        err.status_code = 405
        return err


# coding: utf-8

"""
    LiveAgent API

    This page contains complete API documentation for LiveAgent software. To display additional info and examples for specific API method, just click on the method name in the list below.

To be able to make API requests you need to generate an API key in your admin panel first. [See this article for detailed info.](https://support.liveagent.com/741982-API-key)

Additional info about more advanced agent, contact or ticket API filters can be found [in this article](https://support.liveagent.com/513528-APIv3-advanced-filter-examples).

If you have any question or doubts regarding this API, please do not hesitate to contact our support team.  # noqa: E501

    OpenAPI spec version: 3.0.0
    Contact: support@qualityunit.com
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class Device(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'id': 'float',
        'agent_id': 'str',
        'phone_id': 'str',
        'type': 'str',
        'service_type': 'str',
        'online_status': 'str',
        'preset_status': 'str',
        'route_if_offline': 'str'
    }

    attribute_map = {
        'id': 'id',
        'agent_id': 'agent_id',
        'phone_id': 'phone_id',
        'type': 'type',
        'service_type': 'service_type',
        'online_status': 'online_status',
        'preset_status': 'preset_status',
        'route_if_offline': 'route_if_offline'
    }

    def __init__(self, id=None, agent_id=None, phone_id=None, type=None, service_type=None, online_status='N', preset_status=None, route_if_offline='Y'):  # noqa: E501
        """Device - a model defined in Swagger"""  # noqa: E501

        self._id = None
        self._agent_id = None
        self._phone_id = None
        self._type = None
        self._service_type = None
        self._online_status = None
        self._preset_status = None
        self._route_if_offline = None
        self.discriminator = None

        if id is not None:
            self.id = id
        self.agent_id = agent_id
        if phone_id is not None:
            self.phone_id = phone_id
        self.type = type
        self.service_type = service_type
        if online_status is not None:
            self.online_status = online_status
        self.preset_status = preset_status
        if route_if_offline is not None:
            self.route_if_offline = route_if_offline

    @property
    def id(self):
        """Gets the id of this Device.  # noqa: E501

        :return: The id of this Device.  # noqa: E501
        :rtype: float
        """
        return self._id

    @id.setter
    def id(self, id):
        """Sets the id of this Device.

        :param id: The id of this Device.  # noqa: E501
        :type: float
        """

        self._id = id

    @property
    def agent_id(self):
        """Gets the agent_id of this Device.  # noqa: E501

        :return: The agent_id of this Device.  # noqa: E501
        :rtype: str
        """
        return self._agent_id

    @agent_id.setter
    def agent_id(self, agent_id):
        """Sets the agent_id of this Device.

        :param agent_id: The agent_id of this Device.  # noqa: E501
        :type: str
        """
        if agent_id is None:
            raise ValueError("Invalid value for `agent_id`, must not be `None`")  # noqa: E501

        self._agent_id = agent_id

    @property
    def phone_id(self):
        """Gets the phone_id of this Device.  # noqa: E501

        :return: The phone_id of this Device.  # noqa: E501
        :rtype: str
        """
        return self._phone_id

    @phone_id.setter
    def phone_id(self, phone_id):
        """Sets the phone_id of this Device.

        :param phone_id: The phone_id of this Device.  # noqa: E501
        :type: str
        """

        self._phone_id = phone_id

    @property
    def type(self):
        """Gets the type of this Device.  # noqa: E501

        W - WEB A - MOBILE APP S - SIP E - EXTERNAL I - API  # noqa: E501

        :return: The type of this Device.  # noqa: E501
        :rtype: str
        """
        return self._type

    @type.setter
    def type(self, type):
        """Sets the type of this Device.

        W - WEB A - MOBILE APP S - SIP E - EXTERNAL I - API  # noqa: E501

        :param type: The type of this Device.  # noqa: E501
        :type: str
        """
        if type is None:
            raise ValueError("Invalid value for `type`, must not be `None`")  # noqa: E501
        allowed_values = ["W", "A", "S", "E", "I"]  # noqa: E501
        if type not in allowed_values:
            raise ValueError(
                "Invalid value for `type` ({0}), must be one of {1}"  # noqa: E501
                .format(type, allowed_values)
            )

        self._type = type

    @property
    def service_type(self):
        """Gets the service_type of this Device.  # noqa: E501

        M - MESSAGE T - CHAT P - PHONE  # noqa: E501

        :return: The service_type of this Device.  # noqa: E501
        :rtype: str
        """
        return self._service_type

    @service_type.setter
    def service_type(self, service_type):
        """Sets the service_type of this Device.

        M - MESSAGE T - CHAT P - PHONE  # noqa: E501

        :param service_type: The service_type of this Device.  # noqa: E501
        :type: str
        """
        if service_type is None:
            raise ValueError("Invalid value for `service_type`, must not be `None`")  # noqa: E501
        allowed_values = ["M", "T", "P"]  # noqa: E501
        if service_type not in allowed_values:
            raise ValueError(
                "Invalid value for `service_type` ({0}), must be one of {1}"  # noqa: E501
                .format(service_type, allowed_values)
            )

        self._service_type = service_type

    @property
    def online_status(self):
        """Gets the online_status of this Device.  # noqa: E501

        N - ONLINE F - OFFLINE  # noqa: E501

        :return: The online_status of this Device.  # noqa: E501
        :rtype: str
        """
        return self._online_status

    @online_status.setter
    def online_status(self, online_status):
        """Sets the online_status of this Device.

        N - ONLINE F - OFFLINE  # noqa: E501

        :param online_status: The online_status of this Device.  # noqa: E501
        :type: str
        """
        allowed_values = ["N", "F"]  # noqa: E501
        if online_status not in allowed_values:
            raise ValueError(
                "Invalid value for `online_status` ({0}), must be one of {1}"  # noqa: E501
                .format(online_status, allowed_values)
            )

        self._online_status = online_status

    @property
    def preset_status(self):
        """Gets the preset_status of this Device.  # noqa: E501

        N - ONLINE F - OFFLINE  # noqa: E501

        :return: The preset_status of this Device.  # noqa: E501
        :rtype: str
        """
        return self._preset_status

    @preset_status.setter
    def preset_status(self, preset_status):
        """Sets the preset_status of this Device.

        N - ONLINE F - OFFLINE  # noqa: E501

        :param preset_status: The preset_status of this Device.  # noqa: E501
        :type: str
        """
        if preset_status is None:
            raise ValueError("Invalid value for `preset_status`, must not be `None`")  # noqa: E501
        allowed_values = ["N", "F"]  # noqa: E501
        if preset_status not in allowed_values:
            raise ValueError(
                "Invalid value for `preset_status` ({0}), must be one of {1}"  # noqa: E501
                .format(preset_status, allowed_values)
            )

        self._preset_status = preset_status

    @property
    def route_if_offline(self):
        """Gets the route_if_offline of this Device.  # noqa: E501

        Y - Yes N - No  # noqa: E501

        :return: The route_if_offline of this Device.  # noqa: E501
        :rtype: str
        """
        return self._route_if_offline

    @route_if_offline.setter
    def route_if_offline(self, route_if_offline):
        """Sets the route_if_offline of this Device.

        Y - Yes N - No  # noqa: E501

        :param route_if_offline: The route_if_offline of this Device.  # noqa: E501
        :type: str
        """
        allowed_values = ["Y", "N"]  # noqa: E501
        if route_if_offline not in allowed_values:
            raise ValueError(
                "Invalid value for `route_if_offline` ({0}), must be one of {1}"  # noqa: E501
                .format(route_if_offline, allowed_values)
            )

        self._route_if_offline = route_if_offline

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value
        if issubclass(Device, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, Device):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


import random

from scapy.modules.gnuradio import *
from scapy.all import *
from scapy.layers.ZWave import *
from Support import *
from CONSTANTS import *

class Transmitter():

    def __init__(self, configuration, transmitter_conn):
        self.configuration = configuration
        self.conn = transmitter_conn
        self.seqn = long(random.randint(0, SEQN_RANGE))

    def send_frame(self, frame):
        self.seqn += 1
        if self.seqn >= SEQN_RANGE:
            self.seqn = 0
        frame.seqn = self.seqn

        frame[ZWaveReq].crc = calc_crc(frame)

        if not self.conn.send(calc_hash(frame)):
            self.conn.send(calc_hash(frame))
        send(frame, verbose=False)

    def send_test_frame(self, frame, hash):
        self.seqn += 1
        if self.seqn >= SEQN_RANGE:
            self.seqn = 0
        frame.seqn = self.seqn

        frame[ZWaveReq].crc = calc_crc(frame)

        if hash:
            self.conn.send(calc_hash(frame))

        send(frame, verbose=False)

    def send_exit(self):
        self.conn.send(EXIT)


from bs4 import BeautifulSoup as bt
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import urllib.request
import json as js
import time

class catchData(object):
    def __init__(self, **kwargs):
        self.header ={
            r'User-Agent':r'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
            }
        #self.Driver = webdriver.Chrome('chromedriver.exe')
        return super().__init__(**kwargs)

    def GetVocalConcert(self,begin_time="0",end_time="0"):
        self.GetJsonData(begin_time,end_time)

    def GetPageData(self,data_list):
        str='https://piao.damai.cn/'
        for i in data_list:
            Req = urllib.request.Request(str+i+".html",headers=self.header)
            pagedata = urllib.request.urlopen(Req,timeout=5000).read()
            page = bt(pagedata,'lxml')
            showtime = page.find('div',class_='m-sdbox m-showtime').span.string
            venue = page.find('div',class_='m-sdbox m-venue').div.a.string
            title = page.find('h2',class_='tt').span.string
            print(title,venue,showtime,sep='|*-*|')
            time.sleep(2)

    def GetJsonData(self,begin_time='0',end_time='0'):
        req_data={'ctl':'演唱会','st':begin_time,'et':end_time,'tsg':5}
        urlstr='http://search.damai.cn/search.html'
        urlstr1 = 'http://search.damai.cn/searchajax.html'
        data = urllib.parse.urlencode(req_data).encode('ascii')
        Req = urllib.request.Request(urlstr1,data,headers =self.header)
        pagedata = urllib.request.urlopen(Req).read()
        pagedata = pagedata.decode('utf8')
        jsondata = js.loads(pagedata)
        js_data_list=jsondata["ids"].split(",")
        self.GetPageData(js_data_list)

d = catchData()
d.GetVocalConcert(begin_time='2017-06-23',end_time='2017-06-23')


from django.shortcuts import render, get_object_or_404, get_list_or_404
from django.http import HttpResponse, Http404
from .models import CSQuote, StoicQuote
from .forms import SearchForm
from django.http import HttpResponse

def home(request):
    form = SearchForm()
    if form.is_valid():
        print(form.cleaned_data)

    recent_quotes = CSQuote.objects.order_by('-pub_date')
    context = {
        'recent_quotes': recent_quotes,
        'form': form,
    }
    return render(request, 'genquotes/home.html', context)

def displaycs(request):
    randomcs = CSQuote.objects.order_by('?').first()

    context = {
        'randomcs': randomcs,
    }
    return render(request, 'genquotes/displaycs.html', context)

def displaystoic(request):
    randomstoic = StoicQuote.objects.order_by('?').first()
    context = {
        'randomstoic': randomstoic,

    }
    return render(request, 'genquotes/displaystoic.html', context)

def searchcs(request):
    error = ['Author not found.']
    authorquery = request.GET.get("author")
    csquotesby = CSQuote.objects.filter(author__iexact=authorquery)
    stoicquotesby = StoicQuote.objects.filter(author__iexact=authorquery)
    if (csquotesby):
        querylist = csquotesby
    elif (stoicquotesby):
        querylist = stoicquotesby
    if (len(authorquery) > 30):
        error.append('Search term must not exceed 30 characters.')
        error = ' '.join(error)
        return render(request, 'genquotes/home.html', {'error': error})
    if (len(authorquery) < 5):
        error.append('Search term must exceed 5 characters.')
        error = ' '.join(error)
        return render(request, 'genquotes/home.html', {'error': error})
    if not csquotesby and not stoicquotesby:
        #raise Http404("No quotes found")
        error.append('Please search again.')
        error = ' '.join(error)
        return render(request, 'genquotes/home.html', {'error': error})
    context = {
        'results': querylist,
        'author': authorquery,
    }
    return render (request, 'genquotes/searchcs.html', context)


import numpy as np
import matplotlib.pyplot as plt
import sys
import json
import pylab
import os
import math
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

graphs = []

# Select on which graphs to evaluate
graphs.append('../tests/parameter-testing/01-pueblo/steamRolled')
graphs.append('../tests/parameter-testing/02a-great-falls')
graphs.append('../tests/parameter-testing/02-aachen/steamRolled')
graphs.append('../tests/parameter-testing/03-manhattan_new-york/steamRolled')
graphs.append('../tests/parameter-testing/03a-minnesota')
#graphs.append('../tests/parameter-testing/04-oklahoma-city_oklahoma/steamRolled')
graphs.append('../tests/parameter-testing/05-oklahoma-city_oklahoma_big/steamRolled')
#graphs.append('../tests/parameter-testing/06-los-angeles_california_custom/steamRolled')
graphs.append('../tests/parameter-testing/07-texas_texas_custom/steamRolled')

# ATTENTION: ONLY THE DEFAULT - ALWAYS ACCEPT Version EXISTS!
# graphs.append('../grid-finder/results/08-usa/steamRolled/invalidatedNodesOutside')

graphNames = {
	'../tests/parameter-testing/01-pueblo/steamRolled' : 'Pueblo',
	'../tests/parameter-testing/02a-great-falls' : 'Great Falls',
	'../tests/parameter-testing/02-aachen/steamRolled' : 'Aachen',
	'../tests/parameter-testing/03-manhattan_new-york/steamRolled' : 'Manhattan',
	'../tests/parameter-testing/03a-minnesota' : 'Minnesota',
	'../tests/parameter-testing/05-oklahoma-city_oklahoma_big/steamRolled' : 'Oklahoma City',
	'../tests/parameter-testing/06-los-angeles_california_custom/steamRolled' : 'Los Angeles',
	'../tests/parameter-testing/07-texas_texas_custom/steamRolled' : 'Texas',
	'../grid-finder/results/08-usa/steamRolled/invalidatedNodesOutside' : 'USA'

}

def step_range(start, end, func):
  while start <= end:
    yield start
    start = func(start)

def sqDim(dim):
	dims = [int(i) for i in dim.split('x')]
	return min(dims)

def createPlotSimple(xAxis, yAxis, xLabel, yLabel):
	plt.plot(xAxis, yAxis, linestyle="dashed", marker="o", color="blue", label='Computation time of graphs of different sizes')
	axes = plt.gca()
	axes.set_xlim([min(xAxis) - 10000, max(xAxis) + 10000])
	axes.set_ylim([-1000, max(yAxis) + 1000])
	plt.xlabel(xLabel)
	plt.ylabel(yLabel)
	plt.show()

def createPlot(values, xLabel, yLabel):
	maxX = 0
	maxY = 0
	minX = 10000
	minY = 10000

	for val in values:
		# put plot in
		xAxis = val['x']
		yAxis = val['y']
		plt.xlabel(xLabel)
		plt.ylabel(yLabel)
		plt.plot(val['x'], val['y'], label=val['name'])

		# look for new extremes to later adapt the xlim and ylim
		maxX = max(maxX, max(xAxis))
		maxY = max(maxY, max(yAxis))
		minX = min(minX, min(xAxis))
		minY = min(minY, min(yAxis))

	axes = plt.gca()
	axes.set_xlim([minX, maxX])
	axes.set_ylim([0, maxY + 1])
	plt.legend()
	plt.show()

def getAxisValuesStraightLine(g):
	a = g + './alternating/'
	sf = g + './short-first/'
	aDefault = a + './default/'
	aDefaultSl = aDefault + './straight-line/'
	stats = {}
	for oneDir in os.listdir(aDefaultSl):
		stats[oneDir] = json.load(open(aDefaultSl + oneDir + '/properties.json'))

	# get the steps
	xAxis = [float(evalThresh) for evalThresh in stats]
	xAxis.sort()

	# get the dimension of each step
	dims = []
	for x in xAxis:
		dims.append((stats[str(x)])['dimension'])

	# get the min squared dimension of each step
	yAxis = [sqDim(dim) for dim in dims]

	return { 'x' : xAxis, 'y' : yAxis, 'name' : stats[str(xAxis[0])]['name'] }

def getAxisValuesImprovedHeuristic(g):
	a = g + './alternating/'
	sf = g + './short-first/'
	aImpr = a + './improved/'
	stats = {}
	for oneDir in os.listdir(aImpr):
		theDir = oneDir + './always-accept/'
		stats[oneDir] = json.load(open(aImpr + theDir + './properties.json'))

	xAxis = [float(heuristicConst) for heuristicConst in stats]
	xAxis.sort()

	dims = []
	for x in xAxis:
			dims.append((stats[str(x)])['dimension'])

	yAxis = [sqDim(dim) for dim in dims]

	return { 'x': xAxis, 'y' : yAxis, 'name' : stats[str(xAxis[0])]['name'] }

def getAxisValuesFor3DPlot(g):
	a = g + './alternating/'
	aImpr = a + './improved/'

	stats = {}
	xVals = []
	yVals = []
	zVals = []

	for hC in os.listdir(aImpr):
		hCDir = aImpr + '/' + hC + '/' + 'straight-line'
		for thresh in os.listdir(hCDir):
			threshDir = hCDir + '/' + thresh + '/'
			xVals.append(float(hC))
			yVals.append(float(thresh))
			theJson = json.load(open(threshDir + './properties.json'))
			zVals.append(sqDim(theJson['dimension']))

	return { 'x' : xVals, 'y' : yVals, 'z' : zVals, 'name' : 'test'}

def create3DPlot(values):
	xVal = values['x']
	yVal = values['y']
	zVal = values['z']
	fig = plt.figure()
	ax = fig.gca(projection='3d')
	surf = ax.scatter(xVal, yVal, zVal)
	ax.set_zlim(0, 30)
	plt.show()

def getPropsFromDir(dir):
	return json.load(open(dir + './properties.json', 'r'))

# general plt config
plt.rcdefaults()
# TODO do we need this?
#plt.rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
#plt.rc('font',**{'family':'serif','serif':['Times']})

# get grid sizes of default configuration
# alternating = []
# shortFirst = []
# maxSquare = []
# names = []
# nodes = []
# for g in graphs:
# 	names.append(graphNames[g])
# 	props = getPropsFromDir(g + '/alternating/default/always-accept/')
# 	alt = props['dimension']
# 	alternating.append(alt)
# 	props2 = getPropsFromDir(g + '/short-first/default/always-accept/')
# 	sf = props2['dimension']
# 	shortFirst.append(sf)
# 	maxSquare.append(max(sqDim(alt), sqDim(sf)))
# 	nodes.append(props['nodes'])

# print(names)
# print(alternating)
# print(shortFirst)
# print(maxSquare)
# print(nodes)

# Plot vertex number against computing time for default config
# computingTimes = []
# graphSizes = []
# graphNames = []
# for g in graphs:
# 	props = getPropsFromDir(g + '/alternating/default/always-accept/')

# 	computingTime = props['computation-time-ms']
# 	computingTimes.append(computingTime / 1000)

# 	size = props['nodes']

# 	graphSizes.append(size)
# 	graphNames = props['name']

# print(graphSizes)
# print(computingTimes)
# print(graphNames)

# plt.plot(graphSizes, computingTimes, linestyle="dashed", marker="o", color="blue")
# plt.title('Computation time of graphs of different sizes')
# axes = plt.gca()
# axes.set_xlim([min(graphSizes) - 10000, max(graphSizes) + 10000])
# axes.set_ylim([-1, max(computingTimes) + 1])
# plt.xlabel('Number of vertices')
# plt.ylabel('Computing time in seconds')
# plt.show()

# # E.g. like this create a diagram that shows the maximal squared grid for every possible heuristic constant..
allVals = []
print(graphs)
for g in graphs:
	gVals = getAxisValuesImprovedHeuristic(g)
	allVals.append(gVals)
print(allVals)
createPlot(allVals, 'Constant', 'Grid dimension (square)')

# Create plot of different values for improved heuristic
sizes = []
heuristicConsts = [c for c in step_range(-0.1, 1, lambda x: x + 0.1)]

name = []
for g in graphs:
	root = g + '/alternating/improved'
	dirs = [root + '/' + d  + '/always-accept/' for d in os.listdir(root)]
	theSizes = []
	for d in dirs:
		props = getPropsFromDir(d)
		theSizes.append(sqDim(props['dimension']))
	sizes.append(theSizes)
	name.append(graphNames[g])

plt.figure(figsize=(10,5))
axes = plt.gca()
axes.set_ylim([0,28])
for s in sizes:
	plt.plot(s)
	plt.xticks([0,1,2,3,4,5,6,7,8,9,10,11], ['-0.1', '0.0', '0.1', '0,2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0'])

plt.xlabel('constant value for improved heuristic')
plt.ylabel('size of grid')
plt.title('Grid sizes when using the improved heuristic with different values')
plt.legend(name, loc='upper right')
plt.savefig('improved-sizes.pdf')
plt.show()

print(name)
print(sizes)

# create plot of different values for straight-line evaluator threshold
# sizes = []
# name = []

# for g in graphs:
# 	root = g + '/alternating/default/straight-line'
# 	dirs = [root + '/' + d + '/' for d in os.listdir(root)]
# 	theSizes = []
# 	for d in dirs:
# 		props = getPropsFromDir(d)
# 		theSizes.append(sqDim(props['dimension']))
# 	sizes.append(theSizes)
# 	name.append(graphNames[g])

# plt.figure(figsize=(10,5))
# axes = plt.gca()
# axes.set_ylim([0,28])
# for s in sizes:
# 	plt.plot(s)

# plt.xticks([0,1,2,3,4,5], ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5'])
# plt.xlabel('threshold value for the straight-line evaluator')
# plt.ylabel('size of grid')
# plt.title('Grid sizes when using the straight-line path evaluator with different thresholds')
# plt.legend(name, loc='upper right')
# plt.show()

# # create values for table of improved and straight-line
# graph = [g for g in graphs if graphNames[g] is 'Texas'][0]

# root = graph + '/alternating/improved/'
# values = []

# for oneImpr in os.listdir(root):
# 	path = root + '/' + oneImpr + '/straight-line/'
# 	theValue = []
# 	for oneSl in os.listdir(path):
# 		leave = path + oneSl + '/'
# 		props = getPropsFromDir(leave)
# 		theValue.append(sqDim(props['dimension']))
# 	values.append(theValue)

# switchedValues = []

# for i in range (0, 6):
# 	theVal = []
# 	for elem in values:
# 		theVal.append(elem[i])
# 	switchedValues.append(theVal)

# for val in switchedValues:
# 	oneValStr = ''
# 	for theVal in val:
# 		oneValStr = oneValStr + str(theVal) + ' & '
# 	print(oneValStr)


from openerp import models, fields, api, _
from openerp import SUPERUSER_ID
from openerp import http
from openerp.http import request
from openerp.addons.website_sale.controllers.main import website_sale
import logging

_logger = logging.getLogger(__name__)

class website_sale_invoice(website_sale):

    mandatory_invoice_fields = ["name", "phone", "street", "city", "country_id"]
    optional_invoice_fields = ["state_id", "zip"]

    def _get_mandatory_invoice_fields(self):
        return self.mandatory_invoice_fields

    def _get_optional_invoice_fields(self):
        return self.optional_invoice_fields

    def checkout_parse(self, address_type, data, remove_prefix=False):
        """ data is a dict OR a partner browse record
        """
        # set mandatory and optional fields
        assert address_type in ('billing', 'shipping', 'invoice')
        if address_type == 'billing':
            all_fields = self._get_mandatory_billing_fields() + self._get_optional_billing_fields()
            prefix = ''
        elif address_type == 'invoice':
            all_fields = self._get_mandatory_invoice_fields() + self._get_optional_invoice_fields()
            prefix = 'invoice_'
        else:
            all_fields = self._get_mandatory_shipping_fields() + self._get_optional_shipping_fields()
            prefix = 'shipping_'

        # set data
        if isinstance(data, dict):
            query = dict((prefix + field_name, data[prefix + field_name])
                for field_name in all_fields if prefix + field_name in data)
        else:
            query = dict((prefix + field_name, getattr(data, field_name))
                for field_name in all_fields if getattr(data, field_name))
            if address_type == 'billing' and data.parent_id:
                query[prefix + 'street'] = data.parent_id.name

        if query.get(prefix + 'state_id'):
            query[prefix + 'state_id'] = int(query[prefix + 'state_id'])
        if query.get(prefix + 'country_id'):
            query[prefix + 'country_id'] = int(query[prefix + 'country_id'])

        if query.get(prefix + 'vat'):
            query[prefix + 'vat_subjected'] = True

        query = self._post_prepare_query(query, data, address_type)

        if not remove_prefix:
            return query

        return dict((field_name, data[prefix + field_name]) for field_name in all_fields if prefix + field_name in data)

    def _get_invoice_info(self, checkout):
        invoice_info = {}
        invoice_info.update(self.checkout_parse('invoice', checkout, True))
        invoice_info['type'] = 'invoice'
        return invoice_info

    def checkout_form_save(self, checkout):
        cr, uid, context, registry = request.cr, request.uid, request.context, request.registry

        order = request.website.sale_get_order(force_create=1, context=context)

        orm_partner = registry.get('res.partner')
        orm_user = registry.get('res.users')
        order_obj = request.registry.get('sale.order')

        partner_lang = request.lang if request.lang in [lang.code for lang in request.website.language_ids] else None

        billing_info = {'customer': True}
        if partner_lang:
            billing_info['lang'] = partner_lang
        billing_info.update(self.checkout_parse('billing', checkout, True))

        # set partner_id
        partner_id = None
        if request.uid != request.website.user_id.id:
            partner_id = orm_user.browse(cr, SUPERUSER_ID, uid, context=context).partner_id.id
        elif order.partner_id:
            user_ids = request.registry['res.users'].search(cr, SUPERUSER_ID,
                [("partner_id", "=", order.partner_id.id)], context=dict(context or {}, active_test=False))
            if not user_ids or request.website.user_id.id not in user_ids:
                partner_id = order.partner_id.id

        # save partner informations
        if partner_id and request.website.partner_id.id != partner_id:
            orm_partner.write(cr, SUPERUSER_ID, [partner_id], billing_info, context=context)
        else:
            # create partner
            partner_id = orm_partner.create(cr, SUPERUSER_ID, billing_info, context=context)

        # create a new shipping partner
        if checkout.get('shipping_id') == -1:
            shipping_info = self._get_shipping_info(checkout)
            if partner_lang:
                shipping_info['lang'] = partner_lang
            shipping_info['parent_id'] = partner_id
            checkout['shipping_id'] = orm_partner.create(cr, SUPERUSER_ID, shipping_info, context)

        # create a new invoice partner
        if checkout.get('invoice_id') == -1:
            invoice_info = self._get_invoice_info(checkout)
            if partner_lang:
                invoice_info['lang'] = partner_lang
            invoice_info['parent_id'] = partner_id
            checkout['invoice_id'] = orm_partner.create(cr, SUPERUSER_ID, invoice_info, context)

        order_info = {
            'partner_id': partner_id,
            'message_follower_ids': [(4, partner_id), (3, request.website.partner_id.id)],
            'partner_invoice_id': partner_id,
        }
        order_info.update(order_obj.onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
        address_change = order_obj.onchange_delivery_id(cr, SUPERUSER_ID, [], order.company_id.id, partner_id,
                                                        checkout.get('shipping_id'), None, context=context)['value']
        order_info.update(address_change)
        if address_change.get('fiscal_position'):
            fiscal_update = order_obj.onchange_fiscal_position(cr, SUPERUSER_ID, [], address_change['fiscal_position'],
                                                               [(4, l.id) for l in order.order_line], context=None)['value']
            order_info.update(fiscal_update)

        order_info.pop('user_id')
        order_info.update(partner_shipping_id=checkout.get('shipping_id') or partner_id)

        order_obj.write(cr, SUPERUSER_ID, [order.id], order_info, context=context)


# Create your views here.
import subprocess
from xml.dom import minidom
import MySQLdb

#def scanNet():
subprocess.call("nmap -oA NmapResults  146.64.204.48-109")

db = MySQLdb.connect("localhost", "root", "", "detect")
cursor = db.cursor()

IP_Address = ''
MAC_Address = ''

xmldoc = minidom.parse('NmapResults.xml')
itemlist = xmldoc.getElementsByTagName('address')

for device in itemlist:
    if device.getAttribute('addrtype') == "ipv4":
        IP_Address = device.attributes['addr'].value
        print(device.attributes['addr'].value)

        sql = "INSERT INTO details(IP_Address, MAC_Address, PC_Name, Status) VALUES ( '%s', '%s', '%s', '%s' )" % (
            IP_Address, '', '', '')

        try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
        except:
            # Rollback in case there is any error
            db.rollback()

    elif device.getAttribute('addrtype') == "mac":
        MAC_Address = device.attributes['addr'].value
        print(device.attributes['addr'].value)

        sql = 'UPDATE details SET MAC_Address = "%s" WHERE IP_Address = "%s"' % ( MAC_Address,IP_Address)

        try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
        except:
            # Rollback in case there is any error
            db.rollback()


"""Compute rough emission assuming radiative equillibrium."""
import warnings
import numpy as np
import xarray as xr
import roughness.helpers as rh
import roughness.roughness as rn
from roughness.config import SB, SC

def rough_emission_eq(
    geom, wls, rms, albedo, emiss, solar_dist, rerad=False, flookup=None
):
    """
    Return emission spectrum corrected for subpixel roughness assuming
    radiative equillibrium. Uses ray-casting generated shadow_lookup table at
    sl_path.

    Parameters
    ----------
    geom (list of float): View geometry (sun_theta, sun_az, sc_theta, sc_az)
    wls (arr): Wavelengths [microns]
    rms (arr): RMS roughness [degrees]
    albedo (num): Hemispherical broadband albedo
    emiss (num): Emissivity
    solar_dist (num): Solar distance (au)
    rerad (bool): If True, include re-radiation from adjacent facets
    flookup (str): Los lookup path

    Returns
    -------
    rough_emission (arr): Emission spectrum vs. wls [W m^-2]
    """
    sun_theta, sun_az, sc_theta, sc_az = geom
    # If no roughness, return isothermal emission
    if rms == 0:
        return bb_emission_eq(sun_theta, wls, albedo, emiss, solar_dist)

    # Select shadow table based on rms, solar incidence and az
    shadow_table = rn.get_shadow_table(rms, sun_theta, sun_az, flookup)

    # Produce sloped facets at same resolution as shadow_table
    f_theta, f_az = rh.facet_grids(shadow_table)
    cinc = cos_facet_sun_inc(f_theta, f_az, sun_theta, sun_az)

    # Compute total radiance of sunlit facets assuming radiative eq
    facet_inc = np.degrees(np.arccos(cinc))
    albedo_array = get_albedo_scaling(facet_inc, albedo)
    rad_illum = get_emission_eq(cinc, albedo_array, emiss, solar_dist)
    if rerad:
        rad0 = rad_illum[0, 0]  # Radiance of level surface facet
        alb0 = albedo_array[0, 0]  # Albedo of level surface facet
        rad_illum += get_reradiation(
            cinc, f_theta, albedo, emiss, solar_dist, rad0, alb0
        )

    # Convert to temperature with Stefan-Boltzmann law
    temp_illum = get_temp_sb(rad_illum)

    # Compute temperatures for shadowed facets (JB approximation)
    temp0 = temp_illum[0, 0]  # Temperature of level surface facet
    temp_shade = get_shadow_temp(sun_theta, sun_az, temp0)

    # Compute 2 component emission of illuminated and shadowed facets
    emission_table = emission_2component(
        wls, temp_illum, temp_shade, shadow_table
    )

    # Weight each facet by its probability of occurrance
    facet_weights = rn.get_facet_weights(rms, sun_theta, flookup)

    # Weight each facet by its probability of being observed (sc theta, az)
    view_weights = rn.get_view_table(rms, sc_theta, sc_az, flookup)
    weight_table = facet_weights * view_weights

    rough_emission = emission_spectrum(emission_table, weight_table)
    return rough_emission

def bb_emission_eq(sun_theta, wls, albedo, emiss, solar_dist):
    """
    Return isothermal blackbody emission spectrum assuming radiative
    equilibrium.

    Parameters
    ----------
    sun_theta (num): Solar incidence angle [deg]
    wls (arr): Wavelengths [microns]
    albedo (num): Hemispherical broadband albedo
    emiss (num): Emissivity
    solar_dist (num): Solar distance (au)

    Return
    ------
    bb_emission (arr): Blackbody emission spectrum vs. wls [W m^-2]
    """
    cinc = np.cos(np.deg2rad(sun_theta))
    albedo_array = get_albedo_scaling(sun_theta, albedo)
    rad_isot = get_emission_eq(cinc, albedo_array, emiss, solar_dist)
    temp_isot = get_temp_sb(rad_isot)
    bb_emission = bbrw(wls, temp_isot)
    return bb_emission

def cos_facet_sun_inc(surf_theta, surf_az, sun_theta, sun_az):
    """
    Return cos solar incidence of surface slopes (surf_theta, surf_az) given
    sun location (sun_theta, sun_az) using the dot product.

    If cinc < 0, angle b/t surf and sun is > 90 degrees. Limit these values to
    cinc = 0.

    Parameters
    ----------
    surf_theta (num or arr): Surface slope(s) [deg]
    surf_az (num or arr): Surface azimuth(s) [deg]
    sun_theta (num): Solar incidence angle [deg]
    sun_az (num): Solar azimuth from N [deg]
    """
    surface_vec = rh.sph2cart(np.radians(surf_theta), np.radians(surf_az))
    sun_vec = rh.sph2cart(np.radians(sun_theta), np.radians(sun_az))
    cinc = (sun_vec * surface_vec).sum(axis=2)
    cinc[cinc < 0] = 0
    if isinstance(surf_theta, xr.DataArray):
        cinc = xr.ones_like(surf_theta) * cinc
        cinc.name = "cos(inc)"
    return cinc

def get_albedo_scaling(inc, albedo=0.12, a=None, b=None, mode="vasavada"):
    """
    Return albedo scaled with solar incidence angle (eq A5; Keihm, 1984).

    Parameters a and b have been measured since Keihm (1984) and may differ for
    non-lunar bodies. Optionally supply a and b or select a mode:
        modes:
            'king': Use lunar a, b from King et al. (2020)
            'hayne': Use lunar a, b from Hayne et al. (2017)
            'vasavada': Use lunar a, b from Vasavada et al. (2012)
            else: Use original a, b from Keihm (1984)
    Note: if a or b is supplied, both must be supplied and supercede mode

    Parameters
    ----------
    albedo (num): Lunar average bond albedo (at inc=0)
    inc (num or arr): Solar incidence angle [deg]
    mode (str): Scale albedo based on published values of a, b
    """
    if a is not None and b is not None:
        pass
    elif mode == "king":
        # King et al. (2020)
        a = 0.0165
        b = -0.03625
    elif mode == "hayne":
        # Hayne et al. (2017)
        a = 0.06
        b = 0.25
    elif mode == "vasavada":
        # Vasavada et al. (2012)
        a = 0.045
        b = 0.14
    else:
        # Keihm et al. (1984)
        a = 0.03
        b = 0.14
    alb_scaled = albedo + a * (inc / 45) ** 3 + b * (inc / 90) ** 8
    return alb_scaled

def emission_spectrum(rad_table, weight_table=1):
    """
    Return emission spectrum as weighted sum of facets in rad_table.

    Parameters
    ----------
    rad_table (xr.DataArray): Table of radiance at each facet
    flookup (str): Table of facet weights
    """
    weighted = rad_table * weight_table
    if isinstance(rad_table, xr.DataArray):
        spectrum = weighted.sum(dim=("az", "theta"))
    else:
        spectrum = np.sum(weighted, axis=(0, 1))
    return spectrum

def get_emission_eq(cinc, albedo, emissivity, solar_dist):
    """
    Return radiated emission assuming radiative equillibrium.

    Parameters
    ----------
    cinc (num): Cosine of solar incidence
    albedo (num): Hemispherical broadband albedo
    emissivity (num): Hemispherical broadband emissivity
    solar_dist (num): Solar distance (au)
    """
    f_sun = SC / solar_dist ** 2
    rad_eq = f_sun * cinc * (1 - albedo) / emissivity
    if isinstance(cinc, xr.DataArray):
        rad_eq.name = "Radiance [W m^-2]"
    return rad_eq

def get_reradiation(
    cinc, surf_theta, albedo, emissivity, solar_dist, rad0, alb0=0.12
):
    """
    Return emission incident on surf_theta from re-radiating adjacent level
    surfaces. Approximation from JB modified from downwelling radiance.

    Parameters
    ----------
    cinc (num or arr): Cosine of solar incidence
    surf_theta (num or arr): Surface slope(s) [deg]
    surf_rad (num or arr): Surface radiance; e.g., from get_emission_eq()
    albedo (num or arr): Albedo (hemispherical if num, scaled by cinc if array)
    emissivity (num): Hemispherical broadband emissivity
    solar_dist (num): Solar distance (au)
    rad0 (num): Radiance of level surface facet
    alb0 (num): Albedo of level surface facet
    """
    # Albedo scaled by angle b/t facet and level surface (e.g. surf_theta)
    # TODO: Why scale by alb_level / 0.12?
    level_albedo = get_albedo_scaling(surf_theta, albedo) * alb0 / 0.12
    level_rad = get_emission_eq(cinc, level_albedo, emissivity, solar_dist)

    # Assuming infinite level plane visible from facet slope, the factor
    #  surf_theta / 180 gives incident reradiation
    rerad = (surf_theta / 180) * emissivity * (rad0 + alb0 * level_rad)

    if isinstance(rerad, xr.DataArray):
        rerad = rerad.clip(min=0)
    else:
        rerad[rerad < 0] = 0
    return rerad

def get_temp_sb(rad):
    """
    Return temperature assuming the Stefan-Boltzmann Law given radiance.

    Parameters
    ----------
    rad (num or arr): Radiance [W m^-2]
    """
    temp = (rad / SB) ** 0.25
    if isinstance(rad, xr.DataArray):
        temp.name = "Temperature [K]"
    return temp

def get_shadow_temp(sun_theta, sun_az, temp0):
    """
    Return temperature of shadows. Nominally 100 K less than the rad_eq
    surf_temp. At high solar incidence, scale shadow temperature by inc angle.
    Evening shadows are warmer than morning shadows. Tuned for lunar eqautor
    by Bandfield et al. (2015, 2018).

    Parameters
    ----------
    sun_theta (num): Solar incidence angle [deg]
    sun_az (num): Solar azimuth from N [deg]
    temp0 (num): Temperature of illuminated level surface facet [K]
    """
    shadow_temp_factor = 1
    if sun_theta >= 60 and 0 <= sun_az < 180:
        shadow_temp_factor = 1 - 0.6 * (sun_theta % 60) / 30
    elif sun_theta >= 60 and 180 <= sun_az < 360:
        shadow_temp_factor = 1 - 0.75 * (sun_theta % 60) / 30
    shadow_temp = temp0 - 100 * shadow_temp_factor
    return float(shadow_temp)

def emission_2component(wavelength, temp_illum, temp_shadow, shadow_table):
    """
    Return roughness emission spectrum at given wls given the temperature
    of illuminated and shadowed surfaces and fraction of the surface in shadow.

    Parameters
    ----------
    wavelength (num or arr): Wavelength(s) [microns]
    temp_illum (num or arr): Temperature(s) of illuminated fraction of surface
    temp_shade (num or arr): Temperature(s) of shadowed fraction of surface
    shadow_table (num or arr): Proportion(s) of surface in shadow
    """
    rad_illum = bbrw(wavelength, temp_illum)
    rad_shade = bbrw(wavelength, temp_shadow)

    rad_total = (1 - shadow_table) * rad_illum + shadow_table * rad_shade
    return rad_total

def get_solar_irradiance(solar_spec, solar_dist):
    """
    Return solar irradiance from solar spectrum and solar dist
    assuming lambertian surface.

    Parameters
    ----------
    solar_spec (arr): Solar spectrum
    solar_dist (num): Solar distance (au)
    """
    return solar_spec / (solar_dist ** 2 * np.pi)

def get_rad_factor(rad, emission, solar_irr, emissivity=None):
    """
    Return radiance factor (I/F) given observed radiance, modeled emission,
    solar irradiance and emissivity. If no emissivity is supplied,
    assume Kirchoff's Law (eq. 6, Bandfield et al., 2018).

    Parameters
    ----------
    rad (num or arr): Observed radiance [W m^-2]
    emission (num or arr): Emission to remove from observed radiance
    solar_irr (num or arr): Solar irradiance
    emissivity (num or arr): Emissivity (if None, assume Kirchoff)
    """
    if emissivity is None:
        # Assume Kirchoff's Law to compute emissivity
        emissivity = (rad - solar_irr) / (emission - solar_irr)
    return (rad - emissivity * emission) / solar_irr

def bbrw(wavelength, temp):
    """
    Return blackbody radiance at given wavelength(s) and temp(s).

    Parameters
    ----------
    wavelength (num or arr): Wavelength(s) to compute radiance at [microns]
    temp (num or arr): Temperatue(s) to compute radiance at [K]
    """
    return bbr(wl2wn(wavelength), temp, "wl")

def bbr(wavenumber, temp, radunits="wn"):
    """
    Return blackbody radiance at given wavenumber(s) and temp(s).

    Units='wn' for wavenumber: W/(cm^2 sr cm^-1)
          'wl' for wavelength: W/(cm^2 sr um)

    Translated from ff_bbr.c in davinci_2.22.

    Parameters
    ----------
    wavenumber (num or arr): Wavenumber(s) to compute radiance at [cm^-1]
    temp (num or arr): Temperatue(s) to compute radiance at [K]
    radunits (str): Return units in terms of wn or wl (wn: cm^-1; wl: um)
    """
    a = 2 * 6.6260755e-34 * 2.99792458e10 * 2.99792458e10
    b = 1.43879

    if isinstance(temp, (float, int)):
        if temp <= 0:
            temp = 1
    elif isinstance(temp, xr.DataArray):
        temp = temp.clip(min=1)
    else:
        temp[temp < 0] = 1
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        rad = (a * wavenumber ** 3) / (np.exp(b * wavenumber / temp) - 1.0)
        if radunits == "wl":
            rad = wnrad2wlrad(wavenumber, rad)
    return rad

def btemp(wavenumber, radiance, radunits="wn"):
    """
    Return brightness temperature from wavenumber and radiance.

    Translated from ff_bbr.c in davinci_2.22.
    """
    c1 = 2.0 * 6.6260755e-34 * 2.99792458e10 * 2.99792458e10
    c2 = 1.43879

    if radunits == "wl":
        radiance = wlrad2wnrad(wn2wl(wavenumber), radiance)

    T = (c2 * wavenumber) / np.log(1.0 + (c1 * wavenumber ** 3 / radiance))
    return T

def btempw(wavelength, radiance, radunits="wl"):
    """
    Return brightness temperature from wavelength and radiance.

    Translated from thermis_science.dvrc in davinci_2.22.
    """
    c1 = 1.1911e4  # W cm^-2 micron^4 str^-1
    c2 = 1.43879e4  # micron K

    if radunits == "wn":
        radiance = wnrad2wlrad(wl2wn(wavelength), radiance)

    T = c2 / (wavelength * np.log((c1 / ((wavelength ** 5) * radiance)) + 1))
    return T

def wnrad2wlrad(wavenumber, rad):
    """
    Convert radiance from units W/(cm2 sr cm-1) to W/(cm2 sr um).

    Parameters
    ----------
    wn (num or arr): Wavenumber(s) [cm^-1]
    wnrad (num or arr): Radiance in terms of wn [W/(cm^2 sr cm^-1)]
    """
    wavenumber_microns = wavenumber * 1e-4  # [cm-1] -> [um-1]
    rad_microns = rad * 1e4  # [1/cm-1] -> [1/um-1]
    return rad_microns * wavenumber_microns ** 2  # [1/um-1] -> [1/um]

def wlrad2wnrad(wl, wlrad):
    """
    Convert radiance from units W/(cm2 sr cm-1) to W/(cm2 sr um).
    """
    wn = 1 / wl  # [um] -> [um-1]
    wnrad = wlrad / wn ** 2  # [1/um] -> [1/um-1]
    return wnrad * 1e-4  # [1/um-1] -> [1/cm-1]

def wn2wl(wavenumber):
    """Convert wavenumber (cm-1) to wavelength (um)."""
    return 10000 / wavenumber

def wl2wn(wavelength):
    """Convert wavelength (um) to wavenumber (cm-1)."""
    return 10000 / wavelength


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

COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'

def GlobalComputeUrl(project, collection, name):
  """Generate global compute URL."""
  return ''.join([COMPUTE_URL_BASE, 'projects/', project, '/global/', collection, '/', name])

def RegionalComputeUrl(project, region, collection, name):
  """Generate regional compute URL."""
  return ''.join([COMPUTE_URL_BASE, 'projects/', project, '/regions/', region, '/', collection, '/', name])

def GenerateConfig(context):
  """Generates config."""

  project_id = context.env['project']
  project_number = str(context.env['project_number'])
  environment = str(context.properties.get('environment'))

  sa_hana_compute = 'sa-' + project_number + '-' + environment + '-hana-vm'
  sa_hana_compute_full = sa_hana_compute + '@' + project_id + '.iam.gserviceaccount.com'
  sa_compute_default_full = project_number + '-compute' + '@developer.gserviceaccount.com'
  sa_dm_default_full = project_number + '@cloudservices.gserviceaccount.com'

  resources = []

  # create service account for VM
  resources.append({
    'name': sa_hana_compute,
    'type': 'iam.v1.serviceAccount',
    'properties': {
      'accountId': sa_hana_compute,
      'displayName': sa_hana_compute,
      'projectId': project_id
    }
  })

  # update permissions for service account
  resources.extend([{
    # Get the IAM policy first so that we do not remove any existing bindings.
    'name': project_id + '-get-iam-policy',
    'action': 'gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.getIamPolicy',
    'properties': {
      'resource': project_id,
    },
    'metadata': {
      'runtimePolicy': ['UPDATE_ALWAYS']
    }
  }, {
    # Set the IAM policy patching the existing policy with what ever is currently in the
    # config.
    'name': project_id + '-patch-iam-policy',
    'action': 'gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.setIamPolicy',
    'properties': {
      'resource': project_id,
      'policy': '$(ref.' + project_id + '-get-iam-policy)',
      'gcpIamPolicyPatch': {
        'add': [
          {
            'role': 'roles/owner',
            'members': [
              'serviceAccount:' + sa_compute_default_full,
              'serviceAccount:' + sa_hana_compute_full
            ]
          },
          {
            'role': 'roles/compute.serviceAgent',
            'members': [
              # default agent for DM
              'serviceAccount:' + sa_dm_default_full
            ]
          }
        ]
      }
    },
    'metadata': {
      'dependsOn': [sa_hana_compute, project_id + '-get-iam-policy']
    }
  }])

  # update firewalls
  resources.append({
    'name': 'hana-fw-rule',
    'type': 'compute.v1.firewall',
    'properties': {
      #'network': '$(ref.{{' + context.env['deployment'] +'}}-network.selfLink)',
      'network': GlobalComputeUrl(project_id, 'networks', 'default'),
      'direction': 'INGRESS',
      'sourceRanges': [
        '0.0.0.0/0'
      ],
      'allowed': [{
        'IPProtocol': 'all'
      }],
      'targetServiceAccounts': [
        '149382556458-compute@developer.gserviceaccount.com'
      ]
    }
  })

  return {'resources': resources}


import os
import tarfile

from typing import Any, List, Tuple

from hestia.paths import check_or_create_path
from polystores.exceptions import PolyaxonStoresException
from rest_framework.exceptions import ValidationError

import conf
import stores

from options.registry.archives import ARCHIVES_ROOT_ARTIFACTS, ARCHIVES_ROOT_REPOS
from options.registry.downloads import DOWNLOADS_ROOT_ARTIFACTS, DOWNLOADS_ROOT_LOGS
from stores.exceptions import StoreNotFoundError  # pylint:disable=ungrouped-imports

def create_tarfile(files: List[str], tar_path: str) -> None:
    """Create a tar file based on the list of files passed"""
    with tarfile.open(tar_path, "w:gz") as tar:
        for f in files:
            tar.add(f)

def get_files_in_path(path: str) -> List[str]:
    result_files = []
    for root, _, files in os.walk(path):
        for file_name in files:
            result_files.append(os.path.join(root, file_name))
    return result_files

def archive_repo(repo_git: Any, repo_name: str, commit: str = None) -> Tuple[str, str]:
    archive_root = conf.get(ARCHIVES_ROOT_REPOS)
    check_or_create_path(archive_root)
    archive_name = '{}-{}.tar.gz'.format(repo_name, commit or 'master')
    with open(os.path.join(archive_root, archive_name), 'wb') as fp:
        repo_git.archive(fp, format='tgz', treeish=commit)

    return archive_root, archive_name

def archive_outputs(outputs_path: str, namepath: str, persistence_outputs: str) -> Tuple[str, str]:
    archive_root = conf.get(ARCHIVES_ROOT_ARTIFACTS)
    check_or_create_path(archive_root)

    check_or_create_path(conf.get(DOWNLOADS_ROOT_ARTIFACTS))
    download_path = os.path.join(conf.get(DOWNLOADS_ROOT_ARTIFACTS), namepath.replace('.', '/'))
    download_dir = '/'.join(download_path.split('/'))
    check_or_create_path(download_dir)

    try:
        store_manager = stores.get_outputs_store(persistence_outputs=persistence_outputs)
        store_manager.download_dir(outputs_path, download_path)
    except (PolyaxonStoresException, StoreNotFoundError) as e:
        raise ValidationError(e)

    if store_manager.store.is_local_store:
        outputs_files = get_files_in_path(outputs_path)
    else:
        outputs_files = get_files_in_path(download_path)
    tar_name = "{}.tar.gz".format(namepath.replace('.', '_'))
    create_tarfile(files=outputs_files, tar_path=os.path.join(archive_root, tar_name))
    return archive_root, tar_name

def archive_outputs_file(outputs_path: str,
                         namepath: str,
                         filepath: str,
                         persistence_outputs: str) -> str:
    check_or_create_path(conf.get(DOWNLOADS_ROOT_ARTIFACTS))
    download_filepath = os.path.join(conf.get(DOWNLOADS_ROOT_ARTIFACTS),
                                     namepath.replace('.', '/'),
                                     filepath)
    download_dir = '/'.join(download_filepath.split('/')[:-1])
    check_or_create_path(download_dir)
    try:
        store_manager = stores.get_outputs_store(persistence_outputs=persistence_outputs)
        outputs_filepath = os.path.join(outputs_path, filepath)
        store_manager.download_file(outputs_filepath, download_filepath)
    except (PolyaxonStoresException, StoreNotFoundError) as e:
        raise ValidationError(e)
    if store_manager.store.is_local_store:
        return outputs_filepath
    return download_filepath

def archive_logs_file(log_path: str, namepath: str, persistence_logs: str = 'default') -> str:
    check_or_create_path(conf.get(DOWNLOADS_ROOT_LOGS))
    namepath = namepath.replace('.', '/')
    download_filepath = os.path.join(conf.get(DOWNLOADS_ROOT_LOGS), namepath)
    download_dir = '/'.join(download_filepath.split('/')[:-1])
    check_or_create_path(download_dir)
    try:
        store_manager = stores.get_logs_store(persistence_logs=persistence_logs)
        store_manager.download_file(log_path, download_filepath)
    except (PolyaxonStoresException, StoreNotFoundError) as e:
        raise ValidationError(e)
    if store_manager.store.is_local_store:
        return log_path
    return download_filepath


from django.shortcuts import render
import datetime
from spidercontrolpanel import models
from django.http import HttpResponse
# Create your views here.

def index(request):
    return render(request,'index.html')

def filter(request):
    requesta = request.POST
    if 'year_from' and 'month_from' and 'day_from' and\
        'year_to' and 'month_to' and 'day_to' in request.GET:
        y = requesta.GET['year_from']
        m = requesta.GET['month_from']
        d = requesta.GET['day_from']
        date_from = datetime.datetime(int(y), int(m), int(d), 0, 0)
        y = requesta.GET['year_to']
        m = requesta.GET['month_to']
        d = requesta.GET['day_to']
        date_to = datetime.datetime(int(y), int(m), int(d), 0, 0)
        spider_lists = models.spider_list.objects.filter(date_range=(date_from, date_to))
        print(spider_lists)
    else:
        print("error time range!")

def search_handle(request):
    post = request.POST
    month = post.get('month')
    year = post.get('year')
    day = post.get('day')
    if day:
        sql = year+"-"+month+"-"+day
    else:
        sql = year+"-"+month
    text = models.spider_list.objects.filter(day__contains=sql)
    print(text)
    return render(request,'controlpanel.html',{'text':text})

def search_page(request):
    return render(request, 'controlpanel.html')

def spider_page(request):
    return render(request,'crawlsystem.html')

def spider_handle(request):
    return render(request,'crawlsystem.html')


# -*- coding: utf-8 -*-
# try something like
# def index(): return dict(message="hello from chat_cont.py")
import chat
from datetime import datetime

l = db.executesql("SELECT * FROM user WHERE id="+str(auth.user.id)+";")
sender_id = int(l[0][0])
#print sender_id

def download():
    return response.download(request, db)

def real_time_initial():
    return dict()

def create_message():
    return dict()

def send_message():
    message = request.vars.message
    recepient_username = request.vars.recepient
    q3 = db.executesql("SELECT id FROM user WHERE Username='"+str(recepient_username)+"';")
    recepient_id = int(q3[0][0])
    #recepient_id = 15
    #message = 'good day :)'
    q4 = db.executesql("INSERT INTO message VALUES('',"+str(sender_id)+","+str(recepient_id)+", '"+str(message)+"' , '"+str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"', '') ;")
    return "sent! :)"

def printMsg(i):
    #:) response.write("message ID: " + str(i[0]) + "\n") :)
    q7 = db.executesql("SELECT username FROM user WHERE id="+str(i[1])+";")
    name_from = q7[0][0]
    q8 = db.executesql("SELECT username FROM user WHERE id="+str(i[2])+";")
    name_to = q8[0][0]
    response.write("message from: "  + " " + str(name_from) + "| \n") # id = str(i[1])
    response.write("message to: "  + " " + str(name_to) + "| \n")     # id = str(i[2])
    response.write("message Content: " + i[3] + "| \n")
    response.write("message Sent On: " + i[4] + "| \n")
    response.write("message Read? " + "| \n")
    if (i[5]==0):
        response.write("No!")
    else:
        response.write("Yes!")

def view_sent_messages():
    #response.write( "Sent messages\n" )
    q4 = db.executesql("SELECT * FROM message WHERE FromID="+str(sender_id)+";")
    #printMsg(q4)
    return dict(printMsg=printMsg,q4=q4)

def mark_as_read():
    if(type(request.vars.MsgID) is str):
        id1 = request.vars.MsgID
    else:
        id1 = request.vars.MsgID[0]
    q145 = db.executesql("UPDATE message SET SEEN=1 WHERE id="+str(id1)+"; ")

def view_received_messages():
    # UN-READ MESSAGAES :)
    #response.write( "Un-Read messages\n" )
    q3 = db.executesql("SELECT * FROM message WHERE ToID="+str(sender_id)+" AND SEEN=0 ORDER BY datetime DESC;")
    q5 = db.executesql("SELECT * FROM message WHERE ToID="+str(sender_id)+" AND SEEN=1 ORDER BY datetime DESC")
    #printMsg(q3)
    for i in q3:
        message_id = int(i[0])
        #q4 = db.executesql("UPDATE message SET SEEN=1 WHERE id="+str(message_id)+"; ")
    HTML('<br>')

    # Count of messages :)
    q9 = db.executesql("SELECT count(id) FROM message WHERE ToID="+str(sender_id)+" AND SEEN=0;")
    msg_count= int(q9[0][0])
    #response.write( "Read messages\n" )
    #printMsg(q5)
    q_love = db.executesql("SELECT * FROM message WHERE FromID="+str(sender_id)+" ORDER BY datetime DESC;")
    return dict(printMsg=printMsg,q3=q3, q5=q5, msg_count=msg_count,sender_id=sender_id,q_love=q_love)

def view_chat_profile():
    response.write("hi")
    response.write("Hello!")
    x = db(db.user.id==sender_id).select(db.user.ALL)
    q3 = db.executesql("SELECT count(id) FROM message WHERE ToID="+str(sender_id)+" AND SEEN=0;")
    msg_count= int(q3[0][0])
    # 	print "\tYou have "+str(count[0])+" Unread messages :D\n\n"
    return dict(x=list(x),msg_count=msg_count)


#c2 = 1
#c3 = 8
#c4 = 15
#c5 = 22
#an increase by one integer = movement up by one note name

import turtle
import time
import random
from collections import namedtuple
#from midiutil import MIDIFile

#create midi file with four voices
#mf = MIDIFile(4)
#soprano = 0
#alto = 1
#tenor = 2
#bass = 3

passFail = "pass"
noteList = ''
beatNotes = []
moreNotes = []
chords = ['00']
chordNotes = ["024", "135", "246", "350", "461", "502", "613"]
s = ["s", 21]
a = ["a", 16]
t = ["t", 11]
b = ["b", 7]

def findInterval(voice1, voice2):
    interval = (voice1 - voice2) % 7
    return(interval)

#determine if two notes violate parallel fourths
def parallelFourths(voice1, voice2, beat):
    parallelFourths = "n"
    if findInterval(voice1[beat], voice2[beat]) == 3:
        if findInterval(voice1[beat - 1], voice2[beat - 1]) == 3:
            if voice1[beat - 1] != voice1[beat]:
                if (voice1[beat - 1] > voice1[beat] and voice2[beat - 1] > voice2[beat]) or (voice1[beat - 1] < voice1[beat] and voice2[beat - 1] < voice2[beat]):
                    parallelFourths = "y"
                    print("fourths error")
    return(parallelFourths)

# check for parallel octaves
def parallelOctaves(voice1, voice2, beat):
    parallelOctaves = "n"
    if findInterval(voice1[beat], voice2[beat]) == 0:
        if findInterval(voice1[beat - 1], voice2[beat - 1]) == 0:
            if voice1[beat - 1] != voice1[beat]:
                if (voice1[beat - 1] > voice1[beat] and voice2[beat - 1] > voice2[beat]) or (voice1[beat - 1] < voice1[beat] and voice2[beat - 1] < voice2[beat]):
                    parallelOctaves = "y"
                    print("octaves error")
    return(parallelOctaves)

# check for parallel fifths
def parallelFifths(voice1, voice2, beat):
    parallelFifths = "n"
    if findInterval(voice1[beat], voice2[beat]) == 4:
        if findInterval(voice1[beat - 1], voice2[beat - 1]) == 4:
            if voice1[beat - 1] != voice1[beat]:
                if (voice1[beat - 1] > voice1[beat] and voice2[beat - 1] > voice2[beat]) or (voice1[beat - 1] < voice1[beat] and voice2[beat - 1] < voice2[beat]):
                    parallelFifths = "y"
                    print("fifths error")
    return(parallelFifths)

# check if the voice is within the correct range
def inRange(voice, note):
    inRange = "n"
    if voice == "s":
        if note > 26 or note < 14:
            inRange = "y"
    elif voice == "a":
        if note > 22 or note < 11:
            inRange = "y"
    elif voice == "t":
        if note > 18 or note < 7:
            inRange = "y"
    else:
        if note > 16 or note < 2:
            inRange = "y"
    return(inRange)

# see if a voice is too far away from another voice
def spacing(voice1, voice2, beat):
    spacing = "n"
    if voice1[0] == 't':
        if abs(voice1[beat] - voice2[beat]) > 9:
            spacing = "y"
    elif voice1[0] == "s" and voice2[0] == "b":
        if abs(voice1[beat] - voice2[beat]) > 23:
            spacing = "y"
    else:
        if abs(beat - voice2[beat]) > 7:
            spacing = "y"
    return(spacing)

# check for voice crossing
def voiceCrossing(voice1, voice2):
    crossing = "n"
    voice1a = voice1[beat - 1]
    voice2a = voice1[beat - 1]
    voice1b = voice1[beat]
    voice2b = voice2[beat]
    if voice1a < voice2b or voice2a > voice1b:
        crossing = "y"
    return(crossing)

#two leaps in a row

#dont double leading tone (b)

#try to weight nearest chord tones, yikes thats gonna suck

#similar motion -> perfect w/o step in soprano

#create random chord progression
def randChord():
    inversion = str(random.randrange(0,3))
    chord = str(random.randrange(0,7))
    chord = chord + inversion
    return(chord)

#determine what notes are available on each beat based on chord
def findNotes(beat):
    chord = str(chords[beat])
    root = int(chord[0])
    notes = chordNotes[root]
    return(notes)

#create bass line from bass notes in chords
def bassNote(beat):
    chord = chords[beat]
    root = int(chord[0])
    inversion = chord[1]
    if inversion == "0":
        bNote = root
    elif inversion == "1":
        bNote = root + 2
    else:
        bNote = root + 4
    return(bNote)

#choose a note for the soprano voice on a specific beat
def closestNote(voice1, beat):
    #print(beatNotes, beatNotes[beat], beat)
    notes = list(map(int, list(beatNotes[beat])))
    for x in notes:
        moreNotes.append(x)
    for x in notes:
        moreNotes.append(x + 7)
    closestNote = moreNotes[0]
    voice1 = voice1 % 7
    for x in moreNotes:
        if findInterval(voice1, x) <= findInterval(closestNote, voice1):
            closestNote = x
    del moreNotes[:]
    return(closestNote)

def bassVoice(beat):
    chord = chords[beat]
    root = int(chord[0])
    inversion = chord[1]
    if inversion == "0":
        bNote = root
    elif inversion == "1":
        bNote = root + 2
    else:
        bNote = root + 4
    if inRange(b, bNote) == "y":
        bNote = bNote + 7
    if bNote <= 4:
        bNote = bNote + 7
    return(bNote)

#creates chord progression
def chord():
    for beat in range(1,14):
        chord = randChord()
        chords.append(chord)
    chords.append('40')
    chords.append('00')

chord()

#available notes on each beat
for beat in range(0,16):
    beatNotes.append(findNotes(beat))

#bass voice
def bass():
    for beat in range(1, 16):
        b.append(bassVoice(beat))

bass()

#soprano voice
def soprano():
    for beat in range(0, 16):
        beatNotes[beat] = findNotes(beat)
    for beat in range(2, 17):
        sNote = closestNote(s[beat - 1], beat - 2) + 14
        s.append(sNote)
        while parallelFourths(s, b, beat) == "y" or parallelFifths(s, b, beat) == "y" or parallelOctaves(s, b, beat) == "y" or inRange("s", s[beat]) == "y" or spacing(s, b, beat) == "y":
            notes = list(beatNotes[beat - 2])
            #print(notes, sNote % 7, "sNotes")
            notes.remove(str(sNote % 7))
            print(sNote)
            noteList = ''
            for note in notes:
                noteList = noteList + note
            beatNotes[beat - 2] = noteList
            del s[-1]
            if len(beatNotes[beat - 2]) == 0:
                chords[beat - 3] = randChord()
                print("chord", chords[beat - 3])
                beatNotes[beat - 2] = findNotes(beat)
                print("notes", beatNotes[beat - 2])
                b[beat - 2] = bassVoice(beat)
                print("bass", b[beat - 2])
            sNote = closestNote(s[beat - 1], beat - 2) + 14
            s.append(sNote)

#tenor voice
def tenor():
    for beat in range(0, 16):
        beatNotes[beat] = findNotes(beat)
    for beat in range(2, 17):
        tNote = closestNote(t[beat - 1], beat - 2)
        t.append(tNote)
        while parallelFifths(t, b, beat) == "y" or parallelOctaves(t, b, beat) == "y" or inRange("t", t[beat]) == "y" or spacing(t, b, beat) == "y":
            notes = list(beatNotes[beat - 2])
            print(notes, tNote % 7, "tnotes")
            notes.remove(str(tNote % 7))
            noteList = ''
            for note in notes:
                noteList = noteList + note
            beatNotes[beat - 2] = noteList
            if len(beatNotes[beat - 2]) == 0:
               print("t fail")
               return("fail")
            del t[-1]
            tNote = closestNote(t[beat - 1], beat - 2)
            t.append(tNote)

#alto voice
def alto():
    for beat in range(0, 16):
        beatNotes[beat] = findNotes(beat)
    for beat in range(2, 16):
        aNote = closestNote(a[beat - 1], beat - 2) + 7
        a.append(aNote)
        while parallelFifths(a, t, beat) == "y" or parallelOctaves(a, t, beat) == "y" or inRange("a", a[beat]) == "y" or spacing(a, t, beat) == "y":
            notes = list(beatNotes[beat - 2])
            print(notes, aNote % 7, "aNotes")
            notes.remove(str(aNote % 7))
            noteList = ''
            for note in notes:
                noteList = noteList + note
            beatNotes[beat - 2] = noteList
            if len(beatNotes[beat - 2]) == 0:
               print("a fail")
               return("fail")
            del a[-1]
            aNote = closestNote(a[beat - 1], beat - 2)
            a.append(aNote)

soprano()

while tenor() == "fail":
    s = s[:-15]
    del t[2:-1]
    t = ['t', 11]
    b = b[:-15]
    chords = chords[:-14]
    chord()
    bass()
    soprano()

#while alto() == "fail":
#    s = s[:2]
#    a = a[:2]
#    t = t[:2]
#    b = b[:2]
#    chords = chords[:1]
#    chord()
#    bass()
#    soprano()
#    tenor()

#        if parallelFourths(s, b, beat) == "y":
 #           print("fourths")
  #      elif parallelFifths(s, b, beat) == "y":
   #         print("fifths")
    #    elif parallelOctaves(s, b, beat) == "y":
     #       print("octaves")
      #  elif inRange("s", s[beat]) == "y":
       #     print("range", sNote)
        #elif spacing(s, b, beat) == "y":
     #       print("spacing")
      #  else:
        #    print("good")

turtle.shape("circle")
turtle.speed(10.5)
turtle.penup()
turtle.forward(-400)
turtle.right(-90)
turtle.forward(100)
turtle.right(90)
turtle.pendown()
for x in range(5):
    turtle.forward(800)
    turtle.penup()
    turtle.right(90)
    turtle.forward(20)
    turtle.right(-90)
    turtle.forward(-800)
    turtle.pendown()
turtle.penup()
turtle.right(90)
turtle.forward(50)
turtle.right(-90)
turtle.pendown()
for x in range(5):
    turtle.forward(800)
    turtle.penup()
    turtle.right(90)
    turtle.forward(20)
    turtle.right(-90)
    turtle.forward(-800)
    turtle.pendown()
turtle.penup()
turtle.right(-90)
turtle.forward(20)
for x in range(5):
    turtle.pendown()
    turtle.forward(230)
    turtle.penup()
    turtle.forward(-230)
    turtle.right(90)
    turtle.forward(200)
    turtle.right(-90)
turtle.right(-90)
turtle.forward(980)
turtle.right(90)
turtle.forward(-40)
def upperTurtleNote(voice, beat):
    print(voice[0], voice[beat] % 7)
    turtle.forward(30)
    turtle.forward(10 * voice[beat])
    turtle.stamp()
    turtle.right(90)
    if voice == s or voice == t:
        turtle.forward(10)
    else:
        turtle.forward(-10)
    turtle.right(-90)
    if voice == s or voice == t:
        turtle.forward(70)
    else:
        turtle.forward(-70)
    turtle.pendown()
    if voice == s or voice == t:
        turtle.forward(-70)
    else:
        turtle.forward(70)
    turtle.penup()
    turtle.right(90)
    if voice == s or voice == t:
        turtle.forward(-10)
    else:
        turtle.forward(10)
    turtle.right(-90)
    turtle.forward(-10 * voice[beat])
    turtle.forward(-30)
def lowerTurtleNote(voice, beat):
    print(voice[0], voice[beat] % 7)
    turtle.forward(10 * voice[beat])
    turtle.stamp()
    turtle.right(90)
    if voice == s or voice == t:
        turtle.forward(10)
    else:
        turtle.forward(-10)
    turtle.right(-90)
    if voice == s or voice == t:
        turtle.forward(70)
    else:
        turtle.forward(-70)
    turtle.pendown()
    if voice == s or voice == t:
        turtle.forward(-70)
    else:
        turtle.forward(70)
    turtle.penup()
    turtle.right(90)
    if voice == s or voice == t:
        turtle.forward(-10)
    else:
        turtle.forward(10)
    turtle.right(-90)
    turtle.forward(-10 * voice[beat])
def turtleChord(beat):
    chord = chords[beat]
    root = int(chord[0])
    inversion = chord[1]
    if inversion == "0":
        number = ""
    elif inversion == "1":
        number = "6"
    else:
        number = "64"
    turtle.write(str(root + 1) + " " + number, font = ('Times New Roman', 18, 'bold'))

for beat in range(1, 17):
    print("beat: " + str(beat))
    print(beatNotes[beat - 1])
    turtleChord(beat - 1)
    upperTurtleNote(s, beat)
#    upperTurtleNote(a, beat)
    lowerTurtleNote(t, beat)
    lowerTurtleNote(b, beat)
    turtle.right(90)
    turtle.forward(50)
    turtle.right(-90)
time.sleep(5)
turtle.forward(10000)

turtle.mainloop()


import bpy
from mathutils import Vector
from ... utils.blender_ui import get_dpi, get_dpi_factor
from ... graphics.drawing2d import draw_horizontal_line, draw_text, set_drawing_dpi, draw_box
from ... preferences import Hops_text_color, Hops_text2_color, Hops_border_color, Hops_border2_color

class TwoDBevelOperator(bpy.types.Operator):
    bl_idname = "hops.2d_bevel"
    bl_label = "2 Dimensional Bevel"
    bl_options = {"REGISTER", "UNDO", "GRAB_CURSOR", "BLOCKING"}
    bl_description = "Change the bevel modifier of the active object via vertex"

    @classmethod
    def poll(cls, context):
        return getattr(context.active_object, "type", "") == "MESH"

    def invoke(self, context, event):
        self.object = context.active_object
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.view3d.clean_mesh()
        self.bevel = self.get_bevel_modifier()
        self.start_mouse_position = Vector((event.mouse_region_x, event.mouse_region_y))
        self.start_bevel_width = self.bevel.width
        self.start_bevel_profile = self.bevel.profile
        self.start_bevel_segments = self.bevel.segments
        self.bevel_offset = 0
        self.profile_offset = 0
        self.last_mouse_x = event.mouse_region_x

        args = (context, )
        self.draw_handler = bpy.types.SpaceView3D.draw_handler_add(self.draw, args, "WINDOW", "POST_PIXEL")
        context.window_manager.modal_handler_add(self)
        return {"RUNNING_MODAL"}

    def get_bevel_modifier(self):
        object = bpy.context.active_object
        bevel_modifier = None
        for modifier in object.modifiers:
            if modifier.type == "BEVEL":
                bevel_modifier = modifier
                self.created_bevel_modifier = False
        if bevel_modifier is None:
            bevel_modifier = object.modifiers.new("Bevel", "BEVEL")
            bevel_modifier.limit_method = "NONE"
            bevel_modifier.width = 0.200
            bevel_modifier.profile = 0.70
            bevel_modifier.segments = 6
            bevel_modifier.use_only_vertices = True
            bevel_modifier.use_clamp_overlap = False
            self.created_bevel_modifier = True
        return bevel_modifier

    def modal(self, context, event):
        divisor = 10000 if event.shift else 10000000 if event.ctrl else 1000
        divisor_profile = 500 if event.ctrl else 100000000000
        offset_x = event.mouse_region_x - self.last_mouse_x
        self.bevel_offset += offset_x / divisor / get_dpi_factor()
        self.bevel.width = self.start_bevel_width - self.bevel_offset
        self.profile_offset += offset_x / divisor_profile / get_dpi_factor()

        if event.ctrl:
            self.bevel.profile = self.start_bevel_profile - self.profile_offset

        if event.type == "WHEELUPMOUSE" or event.type == 'NUMPAD_PLUS' and event.value=='PRESS':
            self.bevel.segments += 1
        if event.type == "WHEELDOWNMOUSE" or event.type == 'NUMPAD_MINUS' and event.value=='PRESS':
            self.bevel.segments -= 1

        if event.type in ("ESC", "RIGHTMOUSE"):
            self.reset_object()
            return self.finish()

        if event.type in ("SPACE", "LEFTMOUSE"):
            return self.finish()

        if event.type == 'W' and event.value =='PRESS':
            ofset_list =['OFFSET','WIDTH', 'DEPTH', 'PERCENT']
            if self.bevel.offset_type == ofset_list[0]:
                self.bevel.offset_type = ofset_list[1]
            elif self.bevel.offset_type == ofset_list[1]:
                self.bevel.offset_type = ofset_list[2]
            elif self.bevel.offset_type == ofset_list[2]:
                self.bevel.offset_type = ofset_list[3]
            elif self.bevel.offset_type == ofset_list[3]:
                self.bevel.offset_type = ofset_list[0]

        self.last_mouse_x = event.mouse_region_x
        context.area.tag_redraw()
        return {"RUNNING_MODAL"}

    def reset_object(self):
        self.bevel.width = self.start_bevel_width
        self.bevel.segments = self.start_bevel_segments
        self.bevel.profile = self.start_bevel_profile
        if self.created_bevel_modifier:
            self.object.modifiers.remove(self.bevel)

    def finish(self):
        bpy.types.SpaceView3D.draw_handler_remove(self.draw_handler, "WINDOW")
        return {"FINISHED"}

    def draw(self, context):
        x, y = self.start_mouse_position
        bevel = self.bevel

        set_drawing_dpi(get_dpi())
        factor = get_dpi_factor()
        color_text1 = Hops_text_color()
        color_text2 = Hops_text2_color()
        color_border = Hops_border_color()
        color_border2 = Hops_border2_color()

        draw_box(x -8 * factor, y + 8 * factor, 214 * factor , 34* factor, color = color_border2)

        draw_box(x + 24 * factor, y + 8 * factor, 180 * factor , 30* factor, color = color_border)

        if bevel.segments >= 10 :
            draw_text(str(bevel.segments), x -8 * factor, y, size = 23, color = color_text1)
        else:
            draw_text(str(bevel.segments), x + 3 * factor, y, size = 23, color = color_text1)

        draw_text("2d Bevel - {:.3f} // (W) - {}".format(bevel.width, bevel.offset_type),
                  x + 27 * factor, y + 9 * factor , size = 12, color = color_text2)

        draw_text("Profile- {:.2f} ".format(bevel.profile),
                  x + 27 * factor, y - 4 * factor, size = 12, color = color_text2)

        #this never worked anyway, do we need it ?
        '''draw_text(self.get_description_text(), x + 24 * factor, y - 28 * factor,
                                          size = 12, color = color)'''

    def get_description_text(self):
        if self.object.hops.status == "CSHARP":
            return "CSsharp / Ssharp"
        if self.object.hops.status == "CSTEP":
            return "CStep / Sstep - Warning: Bevels could not be showing due to bevel baking."
        if self.object.hops.is_pending_boolean:
            return "Pending Boolean - Warning: Bevels could not be showing due to boolean pending."
        return "Standard Mesh"


# -*-coding:Utf-8 -*

import unittest
import sys
sys.path.insert(0, "/home/pce/workspace/python/Labyrinth Game 2.0/roboc/")

from labyrinth import *
from robot import *
from obstacle import *
from helpers import *

class LabyrinthTest(unittest.TestCase):
    """This test case tests the features associated with the creation of the
    labyrinth and the associated objects"""

    def setUp(self):
        path = ("maps/easy.txt")
        with open(path, "r") as file:
            self.content = file.read().strip()
        self.labyrinth = Labyrinth(mapTextToGrid(self.content))
        self.player1 = "player"
        self.robot1 = Robot(self.labyrinth.getEmptySpaces(), self.player1)
        self.robot2 = Robot(self.labyrinth.getEmptySpaces(), self.player1)
        self.robot3 = Robot(self.labyrinth.getEmptySpaces(), self.player1)

    def test_labyrinthDisplay(self):

        labyrinthFromFile = self.content
        labyrinthFromObject = self.labyrinth.__str__()
        self.assertEqual(labyrinthFromFile, labyrinthFromObject)

    def test_robotCreation(self):

        self.labyrinth.addRobot(self.robot1)
        self.labyrinth.addRobot(self.robot2)
        self.labyrinth.addRobot(self.robot3)
        self.assertIn(self.robot1, self.labyrinth.robots)

unittest.main(exit=False)


import subprocess
from vcomp import util

def get_callers():
    return {
        "freebayes": call_variant_fb,
        "samtools": call_variant_mp_bcf,
        "platypus": call_variant_platypus,
        "varscan": call_variant_varscan,
        #platypus-asm": call_variant_platypus_asm,
        #"rtg": call_variant_rtg,
        "gatk-hc": call_variant_gatk_hc,
        #"wecall": call_wecall,
        "gatk-ug": call_variant_gatk_ug
        #"wecall": call_wecall
        # "freebayes-mre": call_variant_fb_minrepeatentropy,
    }

def call_variant_platypus_asm(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-platypus.vcf"
    cmd= "python " + conf.get('main', 'platypus_path') + " callVariants --assemble=1 --assembleBadReads=1 --refFile " + orig_genome_path + " --bamFiles " + bam + " --regions " + bed + " -o " + vcfoutput
    subprocess.check_call(cmd, shell=True)
    return util.compress_vcf(vcfoutput, conf)

def call_variant_fb(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-fb.vcf"
    cmd=[conf.get('main', 'freebayes_path'), "-f", orig_genome_path, "-t", bed, "-b", bam, "-v", vcfoutput]
    subprocess.check_output(cmd)
    return util.sort_vcf(vcfoutput, conf)

def call_variant_fb_minrepeatentropy(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-fb.vcf"
    cmd=[conf.get('main', 'freebayes_path'), "-f", orig_genome_path, "--min-repeat-entropy", "1", "-t", bed, "-b", bam, "-v", vcfoutput]
    subprocess.check_output(cmd)
    return util.compress_vcf(vcfoutput, conf)

def call_variant_platypus(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-platypus.vcf"
    cmd= "python " + conf.get('main', 'platypus_path') + " callVariants --refFile " + orig_genome_path + " --bamFiles " + bam + " --regions " + bed + " -o " + vcfoutput
    subprocess.check_call(cmd, shell=True)
    return util.compress_vcf(vcfoutput, conf)

def call_wecall(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-wc.vcf"
    cmd=conf.get('main', 'wecall_path') + " --refFile " + orig_genome_path + " --inputs " + bam + " --regions " + bed + " --output " + vcfoutput
    subprocess.check_call(cmd, shell=True)
    return util.compress_vcf(vcfoutput, conf)

def call_variant_gatk_hc(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-hc.vcf"
    err = open("/dev/null")
    no_et = ""
    try:
        no_et = " -et NO_ET -K " + conf.get('main', 'gatk_no_et')
    except:
        pass
    cmd="java -Xmx1g -Djava.io.tmpdir=. -jar " + conf.get('main', 'gatk_path') + " -T HaplotypeCaller " + no_et + " -R " + orig_genome_path +" -I " + bam + " -L " + bed + " -o " + vcfoutput
    subprocess.check_output(cmd, shell=True, stderr=err)
    err.close()
    return util.compress_vcf(vcfoutput, conf)

def call_variant_gatk_ug(bam, orig_genome_path, bed, conf=None):
    vcfoutput = "output-ug.vcf"
    err = open("/dev/null")
    no_et = ""
    try:
        no_et = " -et NO_ET -K " + conf.get('main', 'gatk_no_et')
    except:
        pass
    cmd="java -Xmx1g -Djava.io.tmpdir=. -jar " + conf.get('main', 'gatk_path') + " -T UnifiedGenotyper -glm BOTH " + no_et + " -R " + orig_genome_path +" -I " + bam + " -L " + bed + " -o " + vcfoutput
    subprocess.check_output(cmd, shell=True, stderr=err)
    err.close()
    return util.compress_vcf(vcfoutput, conf)

def call_variant_rtg(bam, orig_genome_path, bed, conf):
    output_dir = "rtg-output-" + util.randstr()
    vcfoutput = output_dir + "/snps.vcf.gz"
    cmd=["java", "-Djava.io.tmpdir=.", "-jar", conf.get('main', 'rtg_jar'), "snp", "-t", conf.get('main', 'rtg_ref_sdf'), "--bed-regions", bed, "-o", output_dir, bam]
    subprocess.check_output(cmd)
    return vcfoutput

def call_variant_varscan(bam, orig_genome_path, bed, conf):
    pre_output = "varscan." + util.randstr() + ".mpileup"
    vcfoutput = "output-vs." + util.randstr() + ".vcf"
    bedarg = ""
    if bed is not None:
        bedarg = " -l " + bed
    cmd = conf.get('main','samtools_path') + ' mpileup ' + ' -f ' + orig_genome_path + " -o " + pre_output + " " + bedarg + " " + bam
    subprocess.check_call(cmd, shell=True)
    cmd2 = "java -Xmx2g -jar " + conf.get('main', 'varscan_path') + ' mpileup2cns ' + pre_output + ' --variants --output-vcf 1 --output-file ' + vcfoutput
    output = subprocess.check_output(cmd2, shell=True)
    with open(vcfoutput, "w") as fh:
        fh.write(output)
    return util.bgz_tabix(vcfoutput, conf)

def call_variant_mp_bcf(bam, orig_genome_path, bed, conf):
    pre_output = "mpileup." + util.randstr() + ".vcf"
    vcfoutput = "output-mp." + util.randstr() + ".vcf"
    bedarg = ""
    if bed is not None:
        bedarg = " -l " + bed
    cmd = conf.get('main','samtools_path') + ' mpileup ' + ' -f ' + orig_genome_path + " -uv " + " -o " + pre_output + " " + bedarg + " " + bam
    subprocess.check_call(cmd, shell=True)
    cmd2 = conf.get('main', 'bcftools_path') + ' call ' + ' -mv ' + ' -o ' + vcfoutput + " " + pre_output
    subprocess.check_call(cmd2, shell=True)
    return util.bgz_tabix(vcfoutput, conf)


from django.contrib import admin
from django.urls import path,include
from .views import *

urlpatterns = [
    path('user/<int:user_id>/advisor',getAdvisors),
    path('admin/advisor',addAdvisor),
    path('user/register',RegisterUser.as_view()),
    path('user/<int:user_id>/advisor/<int:advisor_id>',BookAdvisor.as_view()),
    path('user/<int:user_id>/advisor/booking',GetBookedCalls.as_view())
]


import numpy as np
import scipy.optimize as opt
from ruspy.estimation.estimation_cost_parameters import create_transition_matrix
from ruspy.estimation.estimation_cost_parameters import calc_fixp
from ruspy.estimation.estimation_cost_parameters import myopic_costs
from ruspy.estimation.estimation_cost_parameters import lin_cost

def get_worst_trans(init_dict, roh, num_states, max_it=1000, min_state=0):
    beta = init_dict["beta"]
    x_0 = np.array(init_dict["known probs"])
    dim = x_0.shape[0]
    params = np.array(init_dict["params"])
    costs = myopic_costs(num_states, lin_cost, params)
    eq_constr = {"type": "eq", "fun": lambda x: 1 - np.sum(x)}
    ineq_constr = {
        "type": "ineq",
        "fun": lambda x: roh - np.sum(np.multiply(x, np.log(np.divide(x, x_0)))),
    }
    res = opt.minimize(
        select_fixp,
        args=(min_state, num_states, costs, beta, max_it),
        x0=x_0,
        bounds=[(1e-6, 1)] * dim,
        method="SLSQP",
        constraints=[eq_constr, ineq_constr],
    )
    worst_trans = np.array(res["x"])
    return worst_trans

def select_fixp(trans_probs, state, num_states, costs, beta, max_it):
    trans_mat = create_transition_matrix(num_states, trans_probs)
    fixp = calc_fixp(num_states, trans_mat, costs, beta, max_it=max_it)
    return fixp[state]


# coding:utf-8

from flask import Blueprint, render_template, request, jsonify
from flask_security import login_required

from ..service import locationService

bp = Blueprint("location_admin", __name__, url_prefix="/admin/location")

@bp.route("/", methods=["GET"])
@login_required
def mgr():
    return render_template("backend/addressMgr.html")

@bp.route("/create", methods=["GET"])
@bp.route("/<int:location_id>/update", methods=["GET"])
def form(location_id=None):
    location = None
    if location_id:
        location = locationService.get_location_by_id(location_id)
    location = {} if location is None else location
    return render_template("backend/addressMgr.html", location=location)

@bp.route("/create_or_update", methods=['POST'])
@login_required
def create_or_update():
    location_id = request.form.get("location_id", None)
    name = request.form.get("name")
    province = request.form.get("province")
    city = request.form.get("city")
    description = request.form.get("description")
    longitude = float(request.form.get("longitude")) if request.form.get("longitude", None) else None
    latitude = float(request.form.get("latitude")) if request.form.get("latitude", None) else None

    if location_id:
        locationService.update_location(int(location_id), name, province, city, description, longitude, latitude)
    else:
        locationService.add_location(name, province, city, description, longitude, latitude)

    return jsonify(data=dict(success=True))

@bp.route("/<int:location_id>/delete", methods=["POST"])
@login_required
def delete(location_id):
    locationService.remove_location(location_id)
    return jsonify(data=dict(success=True))

@bp.route("/list", methods=["GET"])
@login_required
def data():
    limit = int(request.args.get("iDisplayLength", "10"))
    offset = int(request.args.get("iDisplayStart", "0"))
    sEcho = request.args.get("sEcho")
    search_content = request.args.get("search_content")
    count, locations = locationService.paginate(search_content, offset, limit)
    return jsonify(data=dict(success=True, sEcho=sEcho, iTotalRecords=count, iTotalDisplayRecords=count, aaData=locations))


"""
A given series 3, 5, 9, ........11, 15, ...........21,...... and so on. You need to identify the pattern and determine whether the input number  ( n ) is a part of the given series or not.

Input:
The first line of the input contains a single integer T denoting the number of test cases.
The first line of each test case contains n.

Output:
For each test case, Print Yes if n is a part of the given series or else No.

Constraints:
1 <= T <= 200
1 <= n <= 1000

Example:
Input:
6
917
101
901
51
102
893
Output:
Yes
Yes
No
Yes
No
No
"""

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return "No"
            break
    else:
        return "Yes"

if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        n = int(input())
        n = n + 2
        print(is_prime(n))


import requests
import re
import os
from bs4 import BeautifulSoup
class test_get() :
    def start(self) :
        n = 0
        while n <= self.max_n :
            try :
                req = requests.get(self.u,headers=self.headers,data=self.data,timeout=self.timeout)
            except Exception :
                print('请求时发生蜜汁的错误重新尝试已尝试%d次' % (n))
                req = False
            else :
                break
            n+=1
        if req == False :
            return
        self.text = req.text
        self.req = req
        self.req.encoding = self.encoding
        self.text = req.text
    def __init__(self,u,max_n=10,data={},timeout=20,encoding='utf-8'):#最大尝试次数
        self.encoding = encoding
        self.data = data
        self.timeout = timeout
        self.max_n = max_n
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'}
        self.u = u
    def __str__(self) :
        return str(self.text)
#hh = test_get('https://www.w3cschool.cn/') #例子必须给定一个url可选参数为最大请求次数其他参数随意
#hh.start() #定义好后不会立马请求然后调用start()方法才能启动在此之前你可以修改headers
#print(hh)#req为请求的requests对象下载文件用content打印网页为text亦可start完之后直接打印网页默认的__str__就是网页内容
class get_url_bottom() :
    def __init__(self,u) :
        b = re.findall('(?<=\/)[^\/]{1,}',u)
        self.bottom = b.pop()
        self.bottom = re.sub('\?','_',self.bottom)
    def __str__(self) :
        return self.bottom
#hh = get_url_bottom('https://wadadwwdasdafaadaffasb.ssdac/adaf.sdaf/ssfadadh')
#print(hh)这个类用于提取文件的名字用法直接给一个url就会自动匹配了
class dw_file() :#下载一些图片的类
    def dw(self) :
        n = 0
        while 10 >= n :
            try :
                req = requests.get(self.u)
            except Exception :
                req = False
                print('蜜汁错误重新尝试')
            else :
                break
                pass
            n+=1
        if req != None :
            print('req存在')
            f_path = self.dir +'%s' % (self.name)
            if os.path.exists(self.dir) == False :
                try :
                    os.makedirs(self.dir)
                except Exception :
                    print('创建文件夹时发生蜜汁的错误')
                    return
                else :
                    pass
            with open(f_path,'wb+') as f :
                f.write(req.content)
    def __init__(self,u,dir = None,name = None) :
        self.u = u
        if dir == None :
            self.dir = './'
        else :
            self.dir = dir
        if name == None :
            self.name = get_url_bottom(self.u)
        else :
            self.name = name
#dw = dw_file('https://www.baidu.com/img/bd_logo1.png',dir = './ssd/')
#dw.dw()#用法一个必须的参数下载文件的url可选参数文件的名字name和文件存储的dir
#定义完不会立即下载要用dw()方法这样写方便在下载之前修改
class test_tag() :#尝试一个html元素存在不存在的类
    def __init__(self,bs4_obj,tag) :
        self.bs4_obj = bs4_obj
        self.tag = self.bs4_obj.select(tag)
        if len(self.tag) > 0 :
            self.bool = True
        else :
            self.bool = False
    def __repr__(self) :
        if self.bool == True :
            return 'True'
        else :
            return "False"
#req = test_get('http://96xxnet.com',encoding='gbk')
#req.start()
#html = req
#html = str(html)
#bs4_obj = BeautifulSoup(html, "html.parser")
#hh = test_tag(bs4_obj,'.next_page')
#if hh.bool == True :
#    print('这个元素存在')
#else :
#    print('不存在')
#print(hh.tag)


from vgg_net import *
from helper import *
import torch.utils.data as Data
import csv

class TestSolver:
    def __init__(self, model, device=None):
        self.device = device
        self.model = model
        self.cfg = Config

    def test(self):
        self.model.eval()
        x, names = get_test_data()
        test_dataset = TestDataset(x, names)
        loader = Data.DataLoader(test_dataset, batch_size=cfg.BATCH_SIZE, shuffle=False, num_workers=4)
        csv_data = []
        header = ['filename', 'type']
        filename = os.path.join('result.csv')
        res = []

        for i, (x, _) in enumerate(loader):
            x = to_tensor(x)
            pre = self.model(x)
            pre = pre.cpu().detach().numpy()
            for j in range(pre.shape[0]):
                p = np.argmax(pre[j])
                res.append(np.add(p, 1))

        for name, label in zip(names, res):
            csv_data.append([name, label])

        with open(filename, 'w+', encoding='utf-8') as f:
            f_csv = csv.writer(f)
            f_csv.writerow(header)
            f_csv.writerows(csv_data)


from preprocess import methods

def retrieve_extractor(filename):
    if 'json' in filename: return methods.json_processor
    else: return methods.plaintext_processor

def preprocess_corpus(corpus, treatments, offset=None, run_number=None):
    if corpus == '':
        raise AttributeError('Invalid empty corpus name')
    if corpus is None:
        raise AttributeError('Invalid corpus NoneType')
    if offset is not None and offset <= 0:
        raise AttributeError('Invalid offset value must be positive')
    if run_number is not None and run_number < 0:
        raise AttributeError('Invalid run_number value must be nonnegative')

    from preprocess import argmanager

    import os
    import gzip

    corpus_file = argmanager.retrieve_corpus_file(corpus, treatments)
    if os.path.isfile(corpus_file):
        return gzip.open(corpus_file, 'rb')

    if offset is None or run_number is None:
        raise AttributeError('Insufficient data to retrieve corpus')

    new_corpus_file = argmanager.retrieve_corpus_file(corpus, treatments, run_number)
    base_corpus_file = argmanager.retrieve_corpus_file(corpus, [])
    text_dict = argmanager.corpus_text_dict

    preprocessor = methods.create_preprocessor(treatments)
    extractor = retrieve_extractor(new_corpus_file)(preprocessor, tag=text_dict[corpus])

    with gzip.open(new_corpus_file, 'wb') as f, gzip.open(base_corpus_file, 'rb') as g:
        start_line = run_number * offset
        end_line = start_line + offset

        for i, line in enumerate(g):
            if i < start_line: continue
            elif i >= end_line: break
            elif i >= start_line and i < end_line:
                line = line.decode('utf-8')
                f.write(f'{extractor(line)}\n'.encode('utf-8'))