from flask import Flask, render_template, request, redirect, url_for,flash,session
from flask.ext.session import Session
from flask.ext.bootstrap import Bootstrap
from flask.ext.wtf import Form
from flask_wtf.csrf import CsrfProtect
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from suitor_python import SubReddit_Postings

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
bootstrap = Bootstrap(app)
CsrfProtect(app)
suitor_flask_session = Session

class SubRedditForm(Form):

	form_name = StringField('What is the subreddit name?', validators=[Required()])
	form_phone_number = StringField('What is the recipients phone number?', \
	validators=[Required()])

	form_twilio_number = StringField('Your Twilio Tel Number - optional')
	form_account_sid = StringField('Your account_sid - optional')
	form_auth_token = StringField('Your auth_token - optional')
	form_submit = SubmitField('Submit')

@app.errorhandler(403)
def page_forbidden(e):
	return render_template('403.html'), 403

@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
	return render_template('500.html'), 500

@app.route('/', methods=['GET', 'POST'])
def subreddit_post():
	form = SubRedditForm()
	if form.validate_on_submit():

		name=form.form_name.data
		phone_number = form.form_phone_number.data

		twilio_number = form.form_twilio_number.data
		account_sid = form.form_account_sid.data
		auth_token = form.form_auth_token.data

		subreddit = SubReddit_Postings(name,phone_number)
		if twilio_number:
			subreddit.twilio_setup(account_sid,auth_token,twilio_number)

		subreddit.subreddit_extract_post()
		valid = subreddit.sms_subreddit_post()
		if valid:
			flash("SMS successfully sent!!!")
		else:
			flash("SMS not sent, error with number or Twilio account !!!")

		return redirect(url_for('subreddit_post'))

	return render_template('suitor_flask.html', form=form)

if __name__ == '__main__':

	app.debug = True
	app.run()


#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import os
import sys
import argparse as ap
import re
from matplotlib.lines import Line2D
import matplotlib.patches as patches

def printf(format, *args):
        sys.stdout.write(format % args)

dest = "../figs/"

indir = "../data/bursty-5Hz"

files = [ f for f in os.listdir( indir )
                if os.path.isfile( os.path.join( indir, f ) ) ]

mydata = map( lambda x: os.path.join( indir, x ), files )

apps = [ "cth-st", "hpcg", "lmps-lj", "lmps-snap", "lmps-crack", "miniFE",
                "sparc", "milc" ]

while mydata:

        el = mydata.pop( 0 )

        try:
                config = re.search(r"(([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)Hz_([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)nodes)\.delta", el ).group( 1 )
        except AttributeError:
                continue

        printf( "Generating scaling plot for %s\n", el )

        data = np.genfromtxt( el, names=True, dtype=None,
                        deletechars=set("""~!@#$%^&*()=+~\|]}[{';: /?.>,<"""),
                        autostrip=True, missing_values='-', )

        fig, ax = plt.subplots()
        ax.set_xscale( 'log', basex = 10 )

        for i, a in enumerate( apps ):

                mask = np.isfinite( data[ a ] )
                the_min = min( data[ a ][ mask ] )

                if  not the_min:
                        printf( "Min not found: %s\n", a )
                        continue

                percent = [ ( x - the_min ) / the_min * 100.0
                                for x in data[ a ][ mask ] ]
                plt.plot( data[ "Delta" ][ mask ],
                        percent,
                        lw = 1,
                        label = a )
        ymin, ymax = plt.ylim()

        plt.legend( loc = 'best' )
        plt.xlabel( "Duration of Error (sec)" )
        plt.ylabel( "Pecent Slowdown" )
        plt.savefig( dest + config + "-apps-delta.pdf" );
        plt.close()


import cherrypy
import mako.template
import mako.lookup
import os
from datetime import datetime
from PIL import Image

from lib.tool import web
from conf.system_config import tmpl_lookup

__all__ = ["Gallery"]

class Gallery(object):

    _cp_config = {
        "error_page.404": web.handle_404_error
        }

    @cherrypy.expose
    def index(self):
        photos = [f for f in os.listdir("public/images/gallery/")
                  if (f.endswith(".jpg") or
                      f.endswith(".JPG") or
                      f.endswith(".jpeg"))]
        tmpl = mako.template.Template(
            filename="template/gallery/index.html",
            lookup=tmpl_lookup
            )
        return tmpl.render(
                current_user=web.get_current_user(),
                photos=photos
                )

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def upload(self, files=None):
        if files:
            filename = datetime.utcnow().strftime("%Y%m%d%H%M%S")
            tmp_file = "public/images/gallery/" + filename + ".orig"
            thumb_file = "public/images/gallery/thumbnails/%s.jpg" % filename
            file_up = "public/images/gallery/" + filename + ".jpg"
            with open(tmp_file, "wb") as f:
                f.write(files.file.read())
            im = Image.open(tmp_file)
            im.thumbnail((1080, 720), Image.ANTIALIAS)
            if im.mode != "RGB":
                im = im.convert("RGB")
            im.save(file_up, "jpeg")
            im.thumbnail((240, 144), Image.ANTIALIAS)
            im.save(thumb_file, "jpeg")
            os.remove(tmp_file)
            return {"files": [{
                        "name": filename + ".jpg",
                        "size": "640x310",
                        "thumbnailUrl": \
                            "/static/images/gallery/thumbnails/%s.jpg" %(
                                        filename)
                        }]
                    }


from flask import Flask, render_template, redirect, url_for
from flask_pymongo import PyMongo
import scraping

#initialize flask app
app = Flask(__name__)

#use flask_pymonogo to set up mongo connection
app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app"
mongo = PyMongo(app)

##Set Up App Routes
@app.route("/")
def index():
    mars = mongo.db.mars.find_one()
    return render_template("index (1).html", mars=mars)

@app.route("/scrape")
def scrape():
   mars = mongo.db.mars
   mars_data = scraping.scrape_all() ## access database
   mars.update({}, mars_data, upsert=True) ##update database  .update(query_parameter, data, options)
   return redirect('/', code=302)

if __name__ == "__main__":
   app.run()

## if run in to error that flask_pymong is not installed. Make sure to delete all terminals and start new terminal


# -*- coding: utf-8 -*-
"""
Created on Thu Oct 10 19:16:35 2019

@author: Luis Rodriguez
"""

# Binary Classification Model

# Training and Testing Dataset

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import KFold

data_set=pd.read_csv('model_dataset_vf.csv')
print(data_set.describe())

#print(data_set)

X = data_set.drop(columns='class')
Y = data_set['class']

# We will use the 80% of the data for the training set and 20% for the testing set
# Since the accuracy of the model increased

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2,random_state = 0)

X_train.fillna(0)
X_test.fillna(0)
Y_train.fillna(0)
X_test.fillna(0)

# Logistic Regression for Binary Classification

logistic_regresion_model = linear_model.LogisticRegression()
logistic_regresion_model.fit(X_train,Y_train)

# Testing the model

Y_pred = logistic_regresion_model.predict(X_test)
lrm_score=logistic_regresion_model.score(X_test, Y_test)*100

print('The accuracy of the logistic regression classifier model on test set is: ',lrm_score,'%')

confusion_matrix = confusion_matrix(Y_test, Y_pred)
print(confusion_matrix)


from direct.showbase.ShowBase import ShowBase
base = ShowBase()
from Graph_bus_stop import Graph_bus_stop
from Graph_bus import Graph_bus

from panda3d.core import TextNode
from panda3d.core import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import DirectObject
import logging
import threading
import time
import sys
import random

class MyApp(DirectObject):

  def __init__(self):
    #ShowBase.__init__(self)

    base.setBackgroundColor(0, 0, 0)  # Set the background to black
    #base.disableMouse()  # disable mouse control of the camera
    #camera.setPos(0, 0, 45)  # Set the camera position (X, Y, Z)
    #camera.setHpr(0, -90, 0)  # Set the camera orientation
    base.trackball.node().setPos(-70, 200, -24)
    base.trackball.node().setHpr(0, 40, 0)

    # Create Ambient Light
    ambientLight = AmbientLight('ambientLight')
    ambientLight.setColor((0.01, 0.01, 0.01, 1))
    ambientLightNP = render.attachNewNode(ambientLight)
    render.setLight(ambientLightNP)

    # Directional light 01
    directionalLight = DirectionalLight('directionalLight')
    directionalLight.setColor((2,  2, 2, 1))
    directionalLightNP = render.attachNewNode(directionalLight)
    # This light is facing backwards, towards the camera.
    directionalLightNP.setHpr(-45, -45, 0)
    render.setLight(directionalLightNP)

    # Load the cartesian plane
    self.plane = loader.loadModel("models/plane")
    self.plane.setPos(100,50,0)
    self.plane.reparentTo(render)

    self.bus_stop_list = []
    for i in range (0, 10):
      for j in range (0, 9):
        bus_stop = Graph_bus_stop(100, 10 + i*20, 10 + j*10)
        self.bus_stop_list.append(bus_stop)

    self.bus = Graph_bus(50, 10, 11)

    self.accept("escape", sys.exit)
    self.accept('spam',self.OnSpam)

  def OnSpam(self, h, pos):
    #print h
    self.bus_stop_list[0].set_pass(h)
    self.bus.set_pos(pos, 11)
    self.bus.set_pass(h)
    #print base.trackball.node().getPos()
    #print base.trackball.node().getHpr()

def thread_function(name):
  h = 1
  inc = 1
  pos = 10
  while True:
    messenger.send('spam',[h, pos])
    time.sleep(0.01)
    h += inc
    pos += 0.01
    if(h >= 100):
      inc = -1
    if(h <= 0):
      inc = 1

format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
                    datefmt="%H:%M:%S")

logging.info("Main    : before creating thread")
x = threading.Thread(target=thread_function, args=(1,))
logging.info("Main    : before running thread")
x.start()
logging.info("Main    : wait for the thread to finish")
# x.join()
logging.info("Main    : all done")

w, h = 1440, 810
props = WindowProperties()
props.setSize(w, h)
base.win.requestProperties(props)

print("d1")
app = MyApp()
#messenger.send('spam',['foo','bar'])
print("d2")

base.run()


#  python-holidays
#  ---------------
#  A fast, efficient Python library for generating country, province and state
#  specific sets of holidays on the fly. It aims to make determining whether a
#  specific date is a holiday as fast and flexible as possible.
#
#  Authors: dr-prodigy <dr.prodigy.github@gmail.com> (c) 2017-2023
#           ryanss <ryanssdev@icloud.com> (c) 2014-2017
#  Website: https://github.com/dr-prodigy/python-holidays
#  License: MIT (see LICENSE file)

import warnings
from datetime import date

from holidays.countries.south_korea import SouthKorea, KR, KOR, Korea
from tests.common import TestCase

class TestSouthKorea(TestCase):
    @classmethod
    def setUpClass(self):
        super().setUpClass(SouthKorea)

    def test_country_aliases(self):
        self.assertCountryAliases(SouthKorea, KR, KOR)

    def test_common(self):
        self.assertNonObservedHolidaysName("New Year's Day", "2019-01-01")

        self.assertNotIn(date(1582, 10, 2), self.holidays)
        self.assertNotIn(date(1582, 11, 14), self.holidays)

    def test_first_day_of_january(self):
        for year in range(2006, 2021):
            self.assertIn(self.holidays[date(year, 1, 1)], "New Year's Day")

    def test_lunar_new_year(self):
        for year, month, day in [
            (2006, 1, 28),
            (2007, 2, 17),
            (2010, 2, 13),
            (2014, 1, 30),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "The day preceding of Lunar New Year's Day",
            )

        for year, month, day in [
            (1916, 2, 4),
            (1944, 1, 26),
            (1954, 2, 4),
            (1958, 2, 19),
            (1966, 1, 22),
            (1988, 2, 18),
            (1997, 2, 8),
            (2008, 2, 7),
            (2009, 1, 26),
            (2011, 2, 3),
            (2012, 1, 23),
            (2014, 1, 31),
            (2015, 2, 19),
            (2016, 2, 8),
            (2017, 1, 28),
            (2018, 2, 16),
            (2019, 2, 5),
            (2020, 1, 25),
            (2027, 2, 7),
            (2028, 1, 27),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Lunar New Year's Day",
            )

        for year, month, day in [
            (2006, 1, 30),
            (2007, 2, 19),
            (2008, 2, 8),
            (2009, 1, 27),
            (2010, 2, 15),
            (2011, 2, 4),
            (2012, 1, 24),
            (2013, 2, 11),
            (2014, 2, 1),
            (2015, 2, 20),
            (2016, 2, 9),
            (2018, 2, 17),
            (2019, 2, 6),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "The second day of Lunar New Year's Day",
            )

        for year, month, day in [(2016, 2, 10), (2017, 1, 30), (2020, 1, 27)]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of Lunar New Year's Day",
            )

        self.assertNotIn(date(2015, 2, 21), self.holidays)
        self.assertNotIn(date(2015, 2, 7), self.holidays)

    def test_independence_movement_day(self):
        for year, month, day in [
            (2006, 3, 1),
            (2022, 3, 1),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Independence Movement Day",
            )

        for year, month, day in [(2025, 3, 3)]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of Independence Movement Day",
            )

    def test_tree_planting_day(self):
        for year, month, day in [
            (1949, 4, 5),
            (2005, 4, 5),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Tree Planting Day",
            )

        for year, month, day in [
            (1960, 4, 5),
            (2006, 4, 5),
        ]:
            self.assertNotIn(date(year, month, day), self.holidays)

    def test_childrens_day(self):
        for year, month, day in [
            (2015, 5, 5),
            (2018, 5, 5),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Children's Day",
            )

        for year, month, day in [(2018, 5, 7), (2019, 5, 6)]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of Children's Day",
            )
        for year, month, day in [(2025, 5, 6), (2044, 5, 6)]:
            self.assertIn(
                "Alternative holiday of Children's Day",
                self.holidays[date(year, month, day)],
            )

    def test_birthday_of_buddha(self):
        name = "Birthday of the Buddha"
        for year, month, day in [
            (1931, 5, 25),
            (1968, 5, 5),
            (2010, 5, 21),
            (2011, 5, 10),
            (2012, 5, 28),
            (2013, 5, 17),
            (2014, 5, 6),
            (2015, 5, 25),
            (2016, 5, 14),
            (2017, 5, 3),
            (2018, 5, 22),
            (2019, 5, 12),
            (2020, 4, 30),
            (2021, 5, 19),
            (2022, 5, 8),
            (2023, 5, 27),
            (2024, 5, 15),
            (2026, 5, 24),
            (2027, 5, 13),
            (2028, 5, 2),
            (2029, 5, 20),
            (2030, 5, 9),
        ]:
            self.assertEqual(self.holidays[date(year, month, day)], name)

        for year, month, day in [(2001, 5, 1)]:
            self.assertIn(
                "Birthday of the Buddha",
                self.holidays[date(year, month, day)],
            )

    def test_labour_day(self):
        for year in [1948, 1993]:
            self.assertIn("Labour Day", self.holidays[date(year, 3, 10)])

        for year in [1994, 2023]:
            self.assertIn("Labour Day", self.holidays[date(year, 5, 1)])

        self.assertNotIn(date(1993, 5, 1), self.holidays)
        self.assertNotIn(date(1994, 3, 10), self.holidays)

    def test_memorial_day(self):
        for year in [2006, 2022]:
            self.assertEqual(self.holidays[date(year, 6, 6)], "Memorial Day")

    def test_constitution_day(self):
        for year in [1948, 2007]:
            self.assertEqual(
                self.holidays[date(year, 7, 17)], "Constitution Day"
            )
        self.assertNotIn(date(1947, 7, 17), self.holidays)
        self.assertNotIn(date(2008, 7, 17), self.holidays)

    def test_liberation_day(self):
        for year in [1945, 2020]:
            self.assertEqual(
                self.holidays[date(year, 8, 15)], "Liberation Day"
            )
        self.assertEqual(
            self.holidays[date(2021, 8, 16)],
            "Alternative holiday of Liberation Day",
        )

    def test_chuseok(self):
        for year, month, day in [
            (2010, 9, 21),
            (2011, 9, 11),
            (2012, 9, 29),
            (2013, 9, 18),
            (2014, 9, 7),
            (2015, 9, 26),
            (2016, 9, 14),
            (2018, 9, 23),
            (2019, 9, 12),
            (2020, 9, 30),
            (2021, 9, 20),
            (2022, 9, 9),
            (2023, 9, 28),
            (2024, 9, 16),
            (2025, 10, 5),
            (2026, 9, 24),
            (2027, 9, 14),
            (2028, 10, 2),
            (2029, 9, 21),
            (2030, 9, 11),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "The day preceding of Chuseok",
            )

        for year, month, day in [(2017, 10, 3)]:
            self.assertIn(
                "The day preceding of Chuseok",
                self.holidays[date(year, month, day)],
            )

        for year, month, day in [
            (1942, 9, 25),
            (1978, 9, 17),
            (2010, 9, 22),
            (2011, 9, 12),
            (2012, 9, 30),
            (2013, 9, 19),
            (2014, 9, 8),
            (2015, 9, 27),
            (2016, 9, 15),
            (2017, 10, 4),
            (2018, 9, 24),
            (2019, 9, 13),
            (2020, 10, 1),
            (2021, 9, 21),
            (2022, 9, 10),
            (2023, 9, 29),
            (2024, 9, 17),
            (2025, 10, 6),
            (2026, 9, 25),
            (2027, 9, 15),
            (2029, 9, 22),
            (2030, 9, 12),
            (2040, 9, 21),
        ]:
            self.assertEqual(self.holidays[date(year, month, day)], "Chuseok")

        for year, month, day in [(2028, 10, 3)]:
            self.assertIn("Chuseok", self.holidays[date(year, month, day)])

        for year, month, day in [
            (2010, 9, 23),
            (2011, 9, 13),
            (2012, 10, 1),
            (2013, 9, 20),
            (2014, 9, 9),
            (2015, 9, 28),
            (2016, 9, 16),
            (2017, 10, 5),
            (2018, 9, 25),
            (2019, 9, 14),
            (2020, 10, 2),
            (2021, 9, 22),
            (2022, 9, 11),
            (2023, 9, 30),
            (2024, 9, 18),
            (2025, 10, 7),
            (2026, 9, 26),
            (2027, 9, 16),
            (2028, 10, 4),
            (2029, 9, 23),
            (2030, 9, 13),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "The second day of Chuseok",
            )

        for year, month, day in [
            (2014, 9, 10),
            (2015, 9, 29),
            (2018, 9, 26),
            (2022, 9, 12),
            (2025, 10, 8),
            (2029, 9, 24),
        ]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of Chuseok",
            )

    def test_national_foundation_day(self):
        for year in [1951, 2049]:
            self.assertIn(
                "National Foundation Day",
                self.holidays[date(year, 10, 3)],
            )

        for year, month, day in [(2021, 10, 4)]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of National Foundation Day",
            )

    def test_hangeul_day(self):
        for year in [1948, 1990]:
            self.assertEqual(self.holidays[date(year, 10, 9)], "Hangeul Day")

        for year in [2013, 2023]:
            self.assertEqual(self.holidays[date(year, 10, 9)], "Hangeul Day")

        for year in [1991, 2012]:
            self.assertNotIn(date(year, 10, 9), self.holidays)

        for year, month, day in [(2021, 10, 11)]:
            self.assertEqual(
                self.holidays[date(year, month, day)],
                "Alternative holiday of Hangeul Day",
            )

    def test_christmas_day(self):
        for year in [1948, 2050]:
            self.assertEqual(
                self.holidays[date(year, 12, 25)], "Christmas Day"
            )

    def test_years_range(self):
        for year in [2006, 2021]:
            self.assertIn(self.holidays[date(year, 1, 1)], "New Year's Day")

    def test_special_holidays(self):
        self.assertIn("2020-08-17", self.holidays)

    def test_korea_deprecation_warning(self):
        warnings.simplefilter("default")
        with self.assertWarns(Warning):
            Korea()

        warnings.simplefilter("error")
        with self.assertRaises(Warning):
            Korea()


from django.http import HttpResponse, QueryDict
from django.views.generic import View, TemplateView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers
from ..system.models import UnitOfMeasurement
from ..things.models import Thing, ThingSensorCrossRef, SensorDataType, SensorData, Sensor, SensorLocationCrossRef
from ..events.models import EVENT_STATUS_CODES, EVENT_TIME_TYPES, Event, EventType, EventPersonCrossRef, EventLocationCrossRef, EventSensorDataCrossRef, DataDebitEventCrossRef
from ..locations.models import Location, LocationType
from ..users.models import Person
from django_extensions.db.fields import UUIDField
from ..users.models import Recipient
from datetime import datetime
import json
from pprint import pformat
import logging
from views import ApiException
import re

from ..locations.models import LocationPersonCrossRef, LocationThingCrossRef, LocationSensorCrossRef, LocationToLocationCrossRef

from ..bundles.models import Bundle_Filter, Bundle_Union, Bundle_Join
from django.db import connection

class NavigationViewBase(View):
	def add_error(self, error):
		self.response_data['errors'].append(error)
	# Check to see if the current request has any errors
	def has_errors(self):
		return len(self.response_data['errors']) > 0

	def __init__(self, **kwargs):
		# Set blank response data
		self.response_data = {
			'response': {},
			'errors': [
			]
		}

	def response(self, request, *args, **kwargs):
		# Set the response result based on errors
		# if not self.has_errors():
		# 	self.response_data['response'] = 'success'
		# 	self.response_data.pop("errors", None)
		# else:
		# 	self.response_data['response'] = 'error'

		# Return json response
		return HttpResponse(json.dumps(self.response_data), content_type="application/json")

class GetNavigationTreeView(NavigationViewBase):
	"""
		Navigation Tree View

		Retrieves information from all cross reference tables to build up the Navigation Tree for data views:
		(not a strict hierarchy, but a DAG)

		- Person
		|
		|-- Related Persons
		|  |
		|  |- Person
		|
		|-- Locations (LocationPersonCrossRef)
		|  |
		|  |
		|  |-- Location (LocationToLocationCrossRef; relationship_type = "Private Residence")
		|     |
		|     |-- Location (LocationToLocationCrossRef; relationship_type = "Room in a House")
		|     |  |
		|     |  |- Location (LocationToLocationCrossRef; relationship_type = "Room in a Room")
		|     |
		|     |-- Thing (LocationThingCrossRef; Room to Thing)
		|     |  |
		|     |  |- Property (Thing Property)
		|     |  |- Sensor (Thing Sensor)
		|     |
		|     |- Sensor (LocationSensorCrossRef)
		|
		|-- Things (ThingPersonCrossRef)
		|  |
		|  |- Thing
		|
		|-- Interactions
		   |
		   |- Emails
		   |- Messages
		   |- Social Media

	"""

	def construct_info_dictionary(self, data, dataType):
		r_info = {}
		for item in data:
			r_info[dataType+str(item.id)] = {
				"properties": {
					"id": item.id,
					"name": (item.first_name + " " + item.last_name if dataType == "person" else item.name),
					"type": dataType
				},
				"id": item.id,
				"children": [],
				"parents": []
			}
		return r_info

	def get_persons(self):
		try:
			persons = Person.objects.all()
			return self.construct_info_dictionary(persons, "person")
		except Person.DoesNotExist:
			return ApiException("No person exists")

	def get_locations(self):
		try:
			locations = Location.objects.all()
			return self.construct_info_dictionary(locations, "location")
		except Location.DoesNotExist:
			return ApiException("No location exists")

	def get_things(self):
		try:
			things = Thing.objects.all()
			return self.construct_info_dictionary(things, "thing")
		except Thing.DoesNotExist:
			return ApiException("No thing exists")

	def get_sensors(self):
		try:
			sensors = Sensor.objects.all()
			return self.construct_info_dictionary(sensors, "sensor")
		except Sensor.DoesNotExist:
			return ApiException("No sensor exists")

	def get_personLocationCrossrefs(self):
		try:
			relationships = LocationPersonCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "person"+str(rel.person.id)
				destination = "location"+str(rel.location.id)
				links.append({"source": source, "destination": destination})
			return links
		except LocationPersonCrossRef.DoesNotExist:
			raise ApiException("No Locations could be found in the Location - Person cross references")

	def get_locationThingCrossrefs(self):
		try:
			relationships = LocationThingCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "location"+str(rel.location.id)
				destination = "thing"+str(rel.thing.id)
				links.append({"source": source, "destination": destination})
			return links
		except LocationThingCrossRef.DoesNotExist:
			raise ApiException("No Things could be found in the Location")

	def get_locationSensorCrossrefs(self):
		try:
			relationships = LocationSensorCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "location"+str(rel.location.id)
				destination = "sensor"+str(rel.sensor.id)
				links.append({"source": source, "destination": destination})
			return links
		except LocationSensorCrossRef.DoesNotExist:
			raise ApiException("No Things could be found in the Location")

	def get_sensorLocationCrossrefs(self):
		try:
			relationships = SensorLocationCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "location"+str(rel.location.id)
				destination = "sensor"+str(rel.sensor.id)
				links.append({"source": source, "destination": destination})
			return links
		except SensorLocationCrossRef.DoesNotExist:
			raise ApiException("No Things could be found in the Location")

	def get_locationLocationCrossrefs(self):
		try:
			relationships = LocationToLocationCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "location"+str(rel.loc_one.id)
				destination = "location"+str(rel.loc_two.id)
				links.append({"source": source, "destination": destination})
			return links
		except LocationToLocationCrossRef.DoesNotExist:
			raise ApiException("No Location crossrefs found")

	def get_thingSensorCrossrefs(self):
		try:
			relationships = ThingSensorCrossRef.objects.all()
			links = []
			for rel in relationships:
				source = "thing"+str(rel.thing.id)
				destination = "sensor"+str(rel.sensor.id)
				links.append({"source": source, "destination": destination})
			return links
		except ThingSensorCrossRef.DoesNotExist:
			raise ApiException("No Sensors could be found in Things")

	def get_nodes(self):
		persons = self.get_persons()
		locations = self.get_locations()
		things = self.get_things()
		sensors = self.get_sensors()

		nodes = {}
		nodes.update(persons)
		nodes.update(locations)
		nodes.update(things)
		nodes.update(sensors)
		return nodes

	def get_links(self):
		links = []
		links = links + self.get_personLocationCrossrefs()
		links = links + self.get_locationThingCrossrefs()
		links = links + self.get_thingSensorCrossrefs()
		links = links + self.get_locationSensorCrossrefs()
		links = links + self.get_sensorLocationCrossrefs()
		links = links + self.get_locationLocationCrossrefs()
		return links

	@method_decorator(csrf_exempt)
	def dispatch(self, *args, **kwargs):
		return super(GetNavigationTreeView, self).dispatch(*args, **kwargs)

	def get(self, request, *args, **kwargs):
		logger = logging.getLogger("navbar")
		try:
			nodes = self.get_nodes()
			links = self.get_links()
			self.response_data['response'] = {"nodes": nodes, "links": links}
		except ApiException as error:
			error_string = str(error) if str(error) != "" else "Unknown error has occured."
			self.add_error(error_string)

		return self.response(request, *args, **kwargs)

class FilterSensorData(NavigationViewBase):

	@method_decorator(csrf_exempt)
	def dispatch(self, *args, **kwargs):
		return super(FilterSensorData, self).dispatch(*args, **kwargs)

	def get_allSensorData(self, sensors, limit=200, page=0):
		data = list(SensorData.objects.filter(sensor__id__in=sensors).values("source_description", "value", "sensor__name", "sensor_id", "type__name", "unit__name", "date_created")[page*limit:(page+1)*limit])
		for record in data:
			record['date_created'] = str(record['date_created'])
		return data

	def post(self, request, *args, **kwargs):
		logger = logging.getLogger("navbar")
		try:
			filter = json.loads(request.body)
			sensors = filter['sensors']
			logger.debug(sensors)
			self.response_data['response'] = self.get_allSensorData(sensors, filter['limit'], filter['page'])
		except ApiException as error:
			error_string = str(error) if str(error) != "" else "Unknown error has occured."
			self.add_error(error_string)

		self.add_error('Not implemented')
		return self.response(request, *args, **kwargs)

class GetBundles(NavigationViewBase):

	@method_decorator(csrf_exempt)
	def dispatch(self, *args, **kwargs):
		return super(GetBundles, self).dispatch(*args, **kwargs)

	def get_bundles(self):
		filters = list(Bundle_Filter.objects.all().values('id', 'name', 'description'))
		unions = list(Bundle_Union.objects.all().values('id', 'name', 'description'))
		joins = list(Bundle_Join.objects.all().values('id', 'name', 'description'))

		return {
			"filter": filters,
			"union": unions,
			"join": joins
		}

	def get(self, request, *args, **kwargs):
		logger = logging.getLogger("navbar")
		try:
			bundles = self.get_bundles()
			self.response_data['response']['data'] = bundles
		except ApiException as error:
			error_string = str(error) if str(error) != "" else "Unknown error has occured."
			self.add_error(error_string)

		return self.response(request, *args, **kwargs)

class GetBundleData(NavigationViewBase):

	@method_decorator(csrf_exempt)
	def dispatch(self, *args, **kwargs):
		return super(GetBundleData, self).dispatch(*args, **kwargs)

	def dictfetchall(self, cursor):
		"""Returns all rows from a cursor as a dict"""
		desc = cursor.description
		return [
			dict(zip([col[0] for col in desc], map(str, row) )) for row in cursor.fetchall()
		]

	def dictfetchall(self, cursor, columns):
		return [
			dict(zip(columns, map(str, row) )) for row in cursor.fetchall()
		]

	date_regex = re.compile(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\+\d{2}:\d{2})')
	like_regex = re.compile(r'LIKE %(.*)%')
	columns = ["source_description", "value", "sensor__name", "sensor_id", "type__name", "unit__name", "date_created"]

	def get_querysetData(self, bundle):
		""" Handle the logic of extracting data from database using the bundle's queryset """

		qs = bundle.querySet().values(*self.columns)

		strQuery = str(qs.query)
		strQuery = re.sub(self.date_regex, r"'\1'", strQuery)
		strQuery = re.sub(self.like_regex, r"""LIKE '%\1%'""", strQuery)

		cursor = connection.cursor()
		cursor.execute(strQuery)
		data = self.dictfetchall(cursor, self.columns)

		return (data, qs.query.__str__())

	def get_data(self, bundle_type, bundle_id):
		""" Get data for the given bundle type and id """

		logger = logging.getLogger("navbar")
		logger.debug(bundle_id)
		bundle = None
		if (bundle_type == "filter"):
			bundle = Bundle_Filter.objects.get(id=int(bundle_id))
			return self.get_querysetData(bundle)
		elif (bundle_type == "union"):
			bundle = Bundle_Union.objects.get(id=int(bundle_id))
			return self.get_querysetData(bundle)
		elif (bundle_type == "join"):
			bundle = Bundle_Join.objects.get(id=int(bundle_id))
			query = str(bundle.rawQuery())
			strQuery = str(query)
			strQuery = re.sub(self.date_regex, r"'\1'", strQuery)
			strQuery = re.sub(self.like_regex, r"""LIKE '%\1%'""", strQuery)

			cursor = connection.cursor()
			cursor.execute(strQuery)
			dbdata = cursor.fetchall()

			rdata = {}
			bundleNames = bundle.serializeBundleNames()
			for bundleName in bundleNames:
				rdata[bundleName] = []
			for row in dbdata:
				chunks=[row[x:x+len(self.columns)] for x in xrange(0, len(row), len(self.columns))]
				for (c, bundleName) in zip(chunks, bundleNames):
					rdata[bundleName].append( dict(zip(self.columns, map(str, c) )) )

			return (rdata, query)
		else:
			raise(ApiException("Invalid parameters"))

	def post(self, request, *args, **kwargs):
		logger = logging.getLogger("navbar")
		try:
			filter = json.loads(request.body)
			bundle_type = filter['bundle_type']
			logger.debug(bundle_type)
			bundle = filter['bundle']
			logger.debug(bundle)
			(self.response_data['response']['data'], self.response_data['response']['query']) = self.get_data(bundle_type, bundle)
		except ApiException as error:
			error_string = str(error) if str(error) != "" else "Unknown error has occured."
			self.add_error(error_string)
		except KeyError as error:
			error_string = "Incomplete parameters."
			self.add_error(error_string)

		self.add_error('Not implemented')
		return self.response(request, *args, **kwargs)


import random
from numpy import mean, var, std
import matplotlib.pyplot as plt
'''
  ****************  新股申购模拟程序     *****************
  **                Ver: 0.1                            **
  **                Author: Vincent                     **
  **                Data:   2017-Dec-23                 **
  ********************************************************
'''
class CNewStockInvest:
    def __init__(self, initCapital):
        self.week = 0   #周数
        self.issuePrice = 0     #发行价
        self.lotRate = 0        #中签率
        self.subscribeNum = 0   #申购数量
        self.ticketNum = 0      #中签数量
        self.V = 0              #V
        self.marketPrice = 0    #上市价
        self.WeeklyStockIncome = [] #每周打中新股的收益（5周后才能抛出）
        if initCapital>0:
            self.InitialCapital = initCapital
        else:
            self.InitialCapital = 1000000
        self.CurrentCapital = self.InitialCapital
        self.Profit = 0     #利润
        self.ProfitRate = 0 #利润率

    def reset(self):
        self.week = 0;
        self.WeeklyStockIncome.clear()

    def setInitCapital(self, initCapital):
        if initCapital>0:
            self.InitialCapital = initCapital
        else:
            self.InitialCapital = 1000000
        self.CurrentCapital = self.InitialCapital

    def genRandIssuePrice():  #产生随机的新股发行价(5~10元)
        return 5+5*random.random()

    def genRandLotRate():#产生随机的新股中签率(0,001~0.025)
        return 0.001+0.024*random.random()

    def refreshWeeklyData(self): #每周进行打新
        self.issuePrice = CNewStockInvest.genRandIssuePrice()
        self.lotRate = CNewStockInvest.genRandLotRate()
        if 3<self.week< 52:  #第5周到52周可以抛出5周前的股票回笼资金
            self.CurrentCapital += self.WeeklyStockIncome[self.week-4]
        self.subscribeNum = self.CurrentCapital/self.issuePrice
        self.ticketNum = round((self.subscribeNum * self.lotRate)/1000)*1000  #一手1000股
        self.V = 0.5*random.random()
        self.marketPrice = (0.8+0.5/(self.lotRate*100) + self.V)*self.issuePrice
        self.WeeklyStockIncome.append(self.ticketNum*self.marketPrice)
        self.CurrentCapital -= self.issuePrice * self.ticketNum #扣去打新费用
        weeklyData = (1 + self.week,self.issuePrice,self.lotRate,self.subscribeNum,self.ticketNum,self.V,self.marketPrice,self.CurrentCapital)
        self.week += 1
        #print(weeklyData)

    def getLastFourWeekIncome(self):#最后四周的新股收益要一年后才能到账
        sum = 0
        for x in range(48,52):
            sum += self.WeeklyStockIncome[x]
        return sum

    def runOneYearTest(self):#测试一年的数据
        #print(" 周         发行价             中签率            申购数         中签数         V            上市价               剩余资金")
        for x in range(0,52):
            self.refreshWeeklyData()
        self.CurrentCapital += self.getLastFourWeekIncome()
        self.Profit = self.CurrentCapital - self.InitialCapital
        self.ProfitRate = self.Profit/self.InitialCapital
        #print("【年初资金】：%d 【年末资金】： %d 【利润】：%d 【利润率】：%0.2f%%" % (self.InitialCapital,self.CurrentCapital,self.Profit, self.ProfitRate*100))
        return self.ProfitRate*100

#主函数
def main():

    invest1 = CNewStockInvest(0)
    X =[] #利润率-投入图 X轴
    Y =[] #利润率-投入图 Y轴
    #测试初始资金10W 到 300W
    profitRateResults = []
    for x in range(0,60):#投入5W~250W,增幅为5W
        initCapital = 50000*(x+1)
        X.append(initCapital)
        for t in range(0,1000): #每组测试1000次取平均值
            invest1.reset()
            invest1.setInitCapital(initCapital)
            profitRateResults.append(invest1.runOneYearTest())
        meanProfitRate = mean(profitRateResults)
        Y.append(meanProfitRate)
        print("投入资金:%d 利润率均值:%0.2f%% 方差:%0.2f 标准差:%0.2f" % (initCapital,meanProfitRate,var(profitRateResults),std(profitRateResults)))
        print("------------------------------------------------------------");
        profitRateResults.clear()

    plt.figure()
    plt.plot(X,Y)
    plt.title("New Stock Investment")
    plt.xlabel("Invested Capital(Yuan)")
    plt.ylabel("Rate of Profit(%)")
    plt.show()
main()


# import requests
# from bs4 import BeautifulSoup
# import re
#
# url = 'https://sou.zhaopin.com/?jl=854&kw=devops&kt=3'
# headers = {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"}
#
# response = requests.get(url).text
#
# soup = BeautifulSoup(response,"html.parser")
#
# for item1 in soup.findAll('div',{"class":"jobName"}):
#     for i in item1.attrs['title']:
#         print(i)

from datetime import timedelta
from datetime import datetime
class CarMessage(object):
    def __init__(self, num, owner, color, type, connect, money, endtime):
        # 汽车属性
        self.num = num
        self.color = color
        self.type = type
        self.owner = owner
        self.connect = connect
        self.money = money
        self.endtime = endtime

    def __str__(self):
        print('车牌号：<%s> 车主：<%s> 颜色:<%s> 车型:<%s> 联系方式：<%s> 余额：<%s> 停车时间：<%s> '
              % (self.num, self.owner, self.color, self.type, self.connect, self.money, self.endtime))

class Park(object):
    def init(self):  # 对停车场初始化车辆
        self.car_list.append(CarMessage('001', 'python', '黑', '大卡', '123456789', 34, datetime.now()))
        self.car_list.append(CarMessage('002', 'hello', '黑', '小汽车', '123456789', 87, timedelta(minutes=10)))
        self.car_list.append(CarMessage('003', 'java', '白', '小汽车', '123456789', 55, timedelta(hours=1)))
        self.car_list.append(CarMessage('004', 'westos', '黑', '小卡', '123456789', 60, timedelta(days=2)))
        self.car_list.append(CarMessage('005', 'root', '白', '中卡', '123456789', 24, timedelta(minutes=60)))

    def __init__(self):
        self.max_car = 200
        self.car_list = []
        self.cur_car = len(self.car_list)

    def pay(self):
        print(self.car_list)
            # money = car.endtime
            # print("当前余额：%s" %(money))
        # while True:
        #     if car.money >= money:  # 判断余额是否够支付
        #         car.money -= money
        #         print('自动付款%s成功' % (money))
        #         break
        #     else:
        #         print('余额不足请充值')
        #         car.money += int(input('充值金额：'))
        #         print('充值成功')

p = Park()

p.pay()


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

from globals import *
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC
import pyinotify
import time, os

def wait_for_file_creation(filename, userId, songId, songUrl, msgrId, type, lang):
    global CURRENT_LOOP_RESULT
    global CURRENT_USER_ID
    global CURRENT_SONG_ID
    global CURRENT_SONG_URL
    global CURRENT_MSGR_ID
    global CURRENT_TYPE_ID
    global CURRENT_LANG_ID

    CURRENT_USER_ID = userId
    CURRENT_SONG_ID = songId
    CURRENT_SONG_URL = songUrl
    CURRENT_MSGR_ID = msgrId
    CURRENT_TYPE_ID = type
    CURRENT_LANG_ID = lang
    CURRENT_LOOP_RESULT = 0

    wm = pyinotify.WatchManager()
    eh = MyEventHandler()
    wm.add_watch(filename, pyinotify.ALL_EVENTS, rec=True)
    notifier = pyinotify.Notifier(wm, eh)
    if(DEBUG==1):
        print(str(userId) + " waits for " + filename + "...")
    while (CURRENT_LOOP_RESULT!=1):
        try:
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
        except:
            e = sys.exc_info()
            if(DEBUG==1):
                print(str(e))
            break
    if(DEBUG==1):
        print("Stopping watcher for: "+filename)
    notifier.stop()

class MyEventHandler(pyinotify.ProcessEvent):
    def process_IN_CLOSE_WRITE(self, event):
        global CURRENT_LOOP_RESULT
        global CURRENT_USER_ID
        global CURRENT_SONG_ID
        global CURRENT_SONG_URL
        global CURRENT_MSGR_ID
        global CURRENT_LANG_ID

        CURRENT_LOOP_RESULT = 1
        if(DEBUG==1):
            print("Got it! Checking...")
        checkFileAndReturn(CURRENT_USER_ID, CURRENT_SONG_ID, CURRENT_SONG_URL, CURRENT_MSGR_ID, "audio", CURRENT_LANG_ID)

def checkFileAndReturn(userId, songId, songUrl, msgrId, filetype, lang):
    if(filetype == "audio"):
        file = "./mp3/"+songId+".mp3"
    elif(filetype == "video"):
        file = "./mp3/"+songId+".mp4"

    imgfile = "./mp3/"+songId+".jpg"

    #add id3 tags
    if(filetype == "audio"):
        if(DEBUG==1):
            print("Writing IDv3 tag...")
        audio = EasyID3(file)
        audio['title'] = songId.replace('_',' ')
        audio['artist'] = "DJ Tuner"
        audio['album'] = "Tunes2u.com"
        audio.save()

        #add id3 image
        try:
            a = MP3(file, ID3=ID3)
            with open(imgfile, 'br') as f:
                contents = f.read()
            a.tags.add(APIC(encoding=3, mime=u'image/jpg', type=3, desc=u'Albumcover', data=contents))
            a.save()
        except:
            if(DEBUG==1):
                print("IMG for ID3 not found...")

    if(filetype=="audio"):
        if(DEBUG==1):
            print("Returning audio file...")

        if(msgrId=="telegram"):
            data={'chat_id': userId, 'text': 'https://tunes2u.com/'+songId+'.mp3'}
            sendTelegramRaw(userId,data)
        elif(msgrId=="facebook"):
            if("de" in lang):
                data={'recipient':{'id': userId}, 'message':{'attachment':{'type':'template','payload':{'template_type':'generic','elements':[{'title':songId.replace('_',' '),'image_url':'https://tunes2u.com/'+songId+'.jpg','default_action':{'type':'web_url','url':songUrl,'messenger_extensions':'true','webview_height_ratio':'compact'},'buttons':[{'type':'web_url','url':'https://tunes2u.com/tune4u.php?file='+songId+'.mp3','title':'Download MP3'},{'type':'postback','title':'Video suchen...','payload':'video '+songId.replace('_',' ')},{'type':'element_share'}]}]}}}}
            else:
                data={'recipient':{'id': userId}, 'message':{'attachment':{'type':'template','payload':{'template_type':'generic','elements':[{'title':songId.replace('_',' '),'image_url':'https://tunes2u.com/'+songId+'.jpg','default_action':{'type':'web_url','url':songUrl,'messenger_extensions':'true','webview_height_ratio':'compact'},'buttons':[{'type':'web_url','url':'https://tunes2u.com/tune4u.php?file='+songId+'.mp3','title':'Download MP3'},{'type':'postback','title':'Search video...','payload':'video '+songId.replace('_',' ')},{'type':'element_share'}]}]}}}}

            sendFacebookRaw(userId,data)

    if(filetype=="video"):
        if(DEBUG==1):
            print("Returning video file...")

        if(msgrId=="telegram"):
            data={'chat_id': userId, 'text': 'https://tunes2u.com/'+songId+'.mp4'}
            sendTelegramRaw(userId,data)
        elif(msgrId=="facebook"):
            if("de" in lang):
                data={'recipient':{'id': userId}, 'message':{'attachment':{'type':'template','payload':{'template_type':'generic','elements':[{'title':songId.replace('_',' '),'image_url':'https://tunes2u.com/'+songId+'.jpg','default_action':{'type':'web_url','url':songUrl,'messenger_extensions':'true','webview_height_ratio':'compact'},'buttons':[{'type':'web_url','url':'https://tunes2u.com/tune4u.php?file='+songId+'.mp4','title':'Download MP4'},{'type':'postback','title':'MP3 suchen...','payload':songId.replace('_',' ')},{'type':'element_share'}]}]}}}}
            else:
                data={'recipient':{'id': userId}, 'message':{'attachment':{'type':'template','payload':{'template_type':'generic','elements':[{'title':songId.replace('_',' '),'image_url':'https://tunes2u.com/'+songId+'.jpg','default_action':{'type':'web_url','url':songUrl,'messenger_extensions':'true','webview_height_ratio':'compact'},'buttons':[{'type':'web_url','url':'https://tunes2u.com/tune4u.php?file='+songId+'.mp4','title':'Download MP4'},{'type':'postback','title':'Search MP3...','payload':songId.replace('_',' ')},{'type':'element_share'}]}]}}}}

            sendFacebookRaw(userId,data)

    #check total searches and send tips or ads depending on it
    tip = check_total_searches(userId, lang)

    if(tip != "notip"):
        if(msgrId=="telegram"):
            sendTelegramMessage(userId,tip)
        elif(msgrId=="facebook"):
            sendFacebookMessage(userId,tip)
    else:
        if(DEBUG==1):
            print("No tip to be returned...")

    #check if old files should be removed
    clear_cache()

def check_total_searches(userid, lang):
    totalSearches = get_user_searches_count(userid)

    try:
        if("de" in lang):
            return TIP_LINKS_DE[totalSearches]
        else:
            return TIP_LINKS_EN[totalSearches]
    except:
        return "notip"

def clear_cache():
    #search and clear mp3's older than 5mins
    now = time.time()
    cutoff = now - (300)
    files = os.listdir("mp3/")
    for xfile in files:
        if "1.png" not in xfile and "tune4u.php" not in xfile and "index.php" not in xfile and "getfile.php" not in xfile:
            if os.path.isfile( "mp3/" + xfile ):
                t = os.stat( "mp3/" + xfile )
                c = t.st_ctime
                if c < cutoff:
                    os.remove("mp3/" + xfile)


""" uri resolver """
import uuid
from ptah import config

RESOLVER_ID = 'ptah:uri-resolver'

def resolve(uri):
    """ Resolve uri, return resolved object.

    Uri contains two parts, `schema` and `uuid`. `schema` is used for
    resolver selection. `uuid` is resolver specific data. By default
    uuid is a uuid.uuid4 string.
    """
    if not uri:
        return

    try:
        schema, data = uri.split(':', 1)
    except ValueError:
        return None

    try:
        return config.get_cfg_storage(RESOLVER_ID)[schema](uri)
    except KeyError:
        pass

    return None

def extract_uri_schema(uri):
    """ Extract schema of given uri """
    if uri:
        try:
            schema, data = uri.split(':', 1)
            return schema
        except:
            pass

    return None

def resolver(schema):
    """ Register resolver for given schema
    """
    info = config.DirectiveInfo()

    def wrapper(func):
        info.attach(
            config.Action(
                _register_uri_resolver, (schema, func),
                discriminator=(RESOLVER_ID, schema))
            )

        return func

    return wrapper

def register_uri_resolver(schema, resolver, depth=1):
    """ Register resolver for given schema

    :param schema: uri schema
    :type schema: string
    :param resolver: Callable object that accept one parameter.

    Resolver interface::

      class Resolver(object):

         def __call__(self, uri):
             return content
    """
    info = config.DirectiveInfo(depth=depth)
    info.attach(
        config.Action(
            _register_uri_resolver, (schema, resolver),
            discriminator=(RESOLVER_ID, schema))
        )

def _register_uri_resolver(cfg, schema, resolver):
    cfg.get_cfg_storage(RESOLVER_ID)[schema] = resolver

class UriFactory(object):
    """ Uri Generator

    Example::

       >> uri = UriFactory('cms-content')

       >> uri()
       'cms-content:f73f3266fa15438e94cca3621a3f2dbc'

    """

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

    def __call__(self):
        """ Generate new uri using supplied schema """
        return '%s:%s' % (self.schema, uuid.uuid4().get_hex())


from collections import defaultdict
from typing import Dict

directions = {
    'R': 1,
    'L': -1,
    'U': 1j,
    'D': -1j
}

if __name__ == '__main__':
    grid: Dict[complex, Dict[int, int]] = defaultdict(dict)

    with open('data/day_03.txt', 'r') as f:
        for idx, wire in enumerate(f):
            steps = 0
            position = 0

            for move in wire.split(','):
                direction = directions[move[0]]
                distance = int(move[1:])

                for _ in range(distance):
                    steps += 1
                    position += direction
                    grid[position].setdefault(idx, steps)

    part1 = int(min(abs(p.real) + abs(p.imag) for p, v in grid.items() if len(v) == 2))
    print(f'Part 01: {part1}')

    part2 = min(sum(v.values()) for v in grid.values() if len(v) == 2)
    print(f'Part 02: {part2}')


import torch

def hebb_values_transform(tensor, on_zero):
    # create a copy of the original tensor,
    # because of the way we are replacing them.
    res = tensor.clone().data
    res[res == 0] = on_zero
    return res

def hebb_array_transform(tensor, on_zero):
    # create a copy of the original tensor,
    # because of the way we are replacing them.
    res = tensor.clone().data
    for i in range(len(on_zero)):
        res[i][res[i] == 0] = on_zero[i]
    return res

def balance_relu(x, overall_mean=False):
    if overall_mean:
        mu_x = torch.mean(x)
        x = [x_i if x_i > 0. else -mu_x for x_i in x]
    else:
        mu_x = torch.mean(x, 1)
        for i, x_i in enumerate(x):
            x_i = x[i]
            x_i[x_i == 0.] = -mu_x[i]
            x[i] = x_i

    return x

def hebb_values_transform_input(tensor, on_zero):
    # create a copy of the original tensor,
    # because of the way we are replacing them.
    res = tensor.clone().data
    res[res == 0] = on_zero
    return res

def hebb_values_transform_input2(tensor, on_zero):
    # create a copy of the original tensor,
    # because of the way we are replacing them.
    res = tensor.clone().data
    res[res == 0] = on_zero
    return res

def hebb_values_transform_conv(tensor, on_zero):
    # create a copy of the original tensor,
    # because of the way we are replacing them.
    res = tensor.clone().data
    res[res == 0] = on_zero

    res = torch.sum(res, dim=2)
    res = torch.sum(res, dim=2)
    return res

def hebb_values_negative_transform(tensor, multiplier=0.1):
    # create a copy of the original tensor,
    # because of the way they are replaced.
    res = tensor.clone().data
    res[res < 0] = multiplier * res[res < 0]
    return res

def indices_h(array):
    indices1 = []
    for ind, x in enumerate(array):
        if int(x) == 1:
            indices1.append(ind)
    return indices1

def indices_matrix(matrix):
    matrix1 = []
    for a, array in enumerate(matrix):
        indices1 = []
        for ind, x in enumerate(array):
            if int(x) == 1:
                indices1.append(ind)
        matrix1.append(indices1)
    return indices1

def indices_h_conv(array):
    indices1 = []
    for ind, x in enumerate(array):
        if x == 1:
            indices1.append(ind)
    return indices1


# import pickle
# d = dict(name='bob',age=20,score=88)
# f = open('dump.txt','wb')               #当前目录生成一个dump.txt
# pickle.dump(d,f)                        #把d写入文件f
# f = open('dump.txt','rb')
# d = pickle.load(f)   #把文件f中的内容反序列化到d
#
# print(d)                                #print line1
#
# import json
# f = open('dump_json.txt','wb')
# j = json.dumps(d)
# pickle.dump(j,f)

#
import json
class Student:
    def __init__(self,name,age,score):
        self.name = name
        self.age = age
        self.score = score

s = Student('Bob', 20, 88)

def student2dict(std):
    return {
        'name': std.name,
        'age': std.age,
        'score': std.score
    }
# #
f = open('dump_json.txt','w')                                       # 打开txt
f.write(json.dumps(s,default = student2dict))

f = open('dump_json.txt','r')
a = f.read()                                                        # 令a为文件内容，即json字符串
def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

b = json.loads(a, object_hook=dict2student)
print(b)


#!/usr/bin/env python3

import json
import csv
import yaml
import pprint

#read csv input file (created manually) as dictionary

input_dict = csv.DictReader(open("io_input.csv"))

#create list from dictreader

dict_list=[]

for row in input_dict:
	dict_list.append(row)

#dump to JSON

input_json="input.json"

with open(input_json, 'w') as file_json:
	json.dump(dict_list,file_json)

#dump to YAML

input_yaml="input.yaml"

with open(input_yaml, 'w') as file_yaml:
	output = yaml.dump(dict_list, default_flow_style=True, explicit_start=True)
	yaml.dump(output,file_yaml)

#read csv file as a whole string

with open("io_input.csv") as csv_file:
    data_csv = csv_file.read()

#read json file as a whole string

with open("input.json", 'r') as json_file:
	data_json = json.load(json_file)

#read yaml file as a whole string

with open("input.yaml", 'r') as yaml_file:
	data_yaml = yaml.load(yaml_file)

#create a super dictionary

super_dict={'csv':data_csv, 'json': data_json, 'yaml':data_yaml}

print(super_dict)


"""
Object for evaluating a model for super-resolution
"""
import logging
import os
from typing import List, Tuple, Iterator, NamedTuple

import pandas as pd

from modules.image import image_utils, image
from modules.model import models

LOG = logging.getLogger(__name__)

class ImagePairDescriptor(NamedTuple):
    """Describes an image pair for querying"""
    dataset: str
    num: int
    scale: int

class ModelEvaluatorError(Exception):
    """Error in using the ModelEvaluator"""

class ModelEvaluator:

    def __init__(self, model_config_fpath: str):
        """Initialize a model evaluator from a model configuration

        :param model_config: configuration dictionary for a model
        """
        self._model = \
            models.load_model_from_config_fpath(model_config_fpath)

    @staticmethod
    def _get_evaluation_image_paths() -> List[str]:
        """Gets all the images paths from the evaluation image directory

        Assumes this method is run from neural-image-enhance, raises an exception otherwise

        :return: List of file_paths
        """
        # TODO: either raise an exception or make this handle the filepath stuff
        evaluation_path = os.path.join('modules', 'evaluation', 'images')
        return image_utils.get_image_paths_from_dir(evaluation_path)

    @staticmethod
    def _paired_images(image_paths: List[str]) -> Iterator[Tuple[str, str]]:
        """Pairs all the images from evaluation directory to their input, output pairs

        :param image_paths: List of file_paths
        :return: A List of tuples, representing input, output pairs for each image
        """
        sorted_images = list(sorted(image_paths))
        hr_images, lr_images = sorted_images[::2], sorted_images[1::2]
        return zip(lr_images, hr_images)

    @staticmethod
    def _categorize_image_fpath_pair(
            lr_hr_pair: Tuple[str, str]) -> ImagePairDescriptor:
        """Creates an ImagePairDescriptor from a pair of image fpaths

        Raises an ModelEvaluatorError if the paths are not a valid image pair

        :param lr_hr_pair: pair of a low-res, high_res image file paths
        :return: ImagePairDescriptor of the lr_hr_pair
        """
        img1, img2 = lr_hr_pair
        img1_directory, img1_fname = os.path.split(img1)
        img2_directory, img2_fname = os.path.split(img2)

        # Handle incorrect file pair directories errors here
        if img1_directory != img2_directory:
            raise ModelEvaluatorError(
                f'Directory for image fpaths must be the same: {lr_hr_pair}'
            )

        # break up the image fnames into the descriptors
        prefix1, num1, srf1, scale1, lr = img1_fname.split('_')
        prefix2, num2, srf2, scale2, hr = img2_fname.split('_')

        # Handle the image naming convention stuff here
        errors = []
        if not (prefix1 == prefix2 == 'img'):
            errors.append(
                f'All image fnames must begin with \'img\'.'
            )

        if not (num1 == num2):
            errors.append(
                f'Image fname numbers must match.'
            )

        if not (srf1 == srf2 == 'SRF'):
            errors.append(
                f'All image fnames must contain \'SRF\'.'
            )

        if not (scale1 == scale2):
            errors.append(
                f'Image scales must match.'
            )

        if not (lr == 'LR.png' and hr == 'HR.png'):
            errors.append(
                f'Image fname ending do not properly label resolution type.'
            )

        # raise all caught errors
        if errors:
            raise (
                f'{lr_hr_pair} does not follow proper naming convention. '
                f'Errors: {errors}'
            )

        # now get the descriptor attributes
        dataset = str(img1_directory.split(os.path.sep)[-2])
        num = int(num1)
        scale = int(scale1)
        return ImagePairDescriptor(dataset=dataset, num=num, scale=scale)

    def evaluate(self) -> pd.DataFrame:
        """Evaluates an SR model

        Saves and returns a pd.dataframe formatted in the following way.

        index, dataset, num, scale, baseline, psnr

        dataset: Dataset in the evaluation folder
        num: Number of the image pair in the dataset
        scale: Scale of the enhancement
        baseline: PSNR of a naive resize
        psnr: PSNR via the model's enhance method

        :return: pd.Dataframe with complete results
        """
        evaluation_image_paths = self._get_evaluation_image_paths()
        paired_evaluation_images = self._paired_images(evaluation_image_paths)
        evaluation_results = []
        LOG.info(f'Evaluating {len(evaluation_image_paths)//2} image pairs')
        for lr_hr_pair in paired_evaluation_images:
            image_descriptor = self._categorize_image_fpath_pair(lr_hr_pair)
            lr_img = image.PILImage.open(lr_hr_pair[0]).convert_to_rgb()
            hr_img = image.PILImage.open(lr_hr_pair[1]).convert_to_rgb()

            # compute the baseline PSNR
            baseline = \
                image_utils.psnr(lr_img.resize(hr_img.size), hr_img)

            # compute the model's PSNR from enhancement
            pair_psnr = \
                image_utils.psnr(self._model.enhance(lr_img, hr_img.size), hr_img)

            # make the results row
            pair_results = [
                image_descriptor.dataset,
                image_descriptor.num,
                image_descriptor.scale,
                baseline,
                pair_psnr
            ]

            LOG.info(pair_results)

            evaluation_results.append(
                pair_results
            )

        pd.DataFrame()
        results = pd.DataFrame(
            data=evaluation_results,
            index=0,
            columns=['dataset', 'num', 'scale', 'baseline', 'psnr']
        )

        results_dir = os.path.join(
            'modules', 'evaluation', 'results', self._model.version
        )
        os.mkdir(results_dir)
        results_save_path_override = os.path.join(results_dir, 'results.csv')

        results.to_csv(results_save_path_override)

        return results

    def analyze_results(self, results: pd.DataFrame) -> pd.DataFrame:
        """Outputs results statistics

        :param results: pd.Dataframe results from evaluate (can also be fpath)
        """

        datasets = ['All'] + list(results['dataset'].unique())
        # use 0 for all scales
        scales = [0] + list(results['scale'].unique())
        analyses = []
        for dataset in datasets:
            for scale in scales:
                # get results by dataset
                results_by_dataset = results if dataset == 'All' else \
                    results[results['dataset'] == dataset]

                # get results by scale
                results_by_dataset_and_scale = \
                    results_by_dataset if not scale else \
                        results_by_dataset[results_by_dataset['scale'] == scale]

                if not results_by_dataset_and_scale.empty:
                    psnr_mean = results_by_dataset_and_scale['psnr'].mean()
                    # get the improvement over the baseline
                    psnr_improvement = \
                        results_by_dataset_and_scale['psnr'] - \
                        results_by_dataset_and_scale['baseline']

                    # make the analysis row
                    analysis = [
                        dataset,
                        f'scale_{scale}' if scale else 'All',
                        psnr_mean,
                        psnr_improvement.mean(),
                        psnr_improvement.var()
                    ]

                    LOG.info(analysis)
                    analyses.append(analysis)

        results_analysis = pd.DataFrame(
            data=analyses,
            # index=0,
            columns=[
                'dataset',
                'scale',
                'psnr_mean',
                'psnr_improvement_mean',
                'psnr_improvement_var',
            ]
        )

        results_save_path = os.path.join(
            'modules', 'evaluation', 'results', self._model.version, 'analysis.csv')

        results_analysis.to_csv(results_save_path, index_label='index')

        return results_analysis


from __future__ import print_function

import psi4

import numpy as np
from scipy.optimize import curve_fit

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

he_dimer = """
He
--
He 1 **R**
"""

distances = [2.875, 3.0, 3.125, 3.25, 3.375, 3.5, 3.75, 4.0, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 12.0, 14.0, 16.0, 20.0, 25.0, 30.0]
energies = []
for d in distances:
    mol = psi4.geometry(he_dimer.replace('**R**', str(d)))
    en = psi4.energy('MP2/aug-cc-pVDZ', molecule=mol, bsse_type='nocp')
    en *= 219474.6
    energies.append(en)

# Lennard-Jones fit
powers = [-12, -6]
x = np.power(np.array(distances).reshape(-1, 1), powers)
coeffs = np.linalg.lstsq(x, energies)[0]
fpoints = np.linspace(2, max(distances), 100).reshape(-1, 1)
fdata = np.power(fpoints, powers)
fit_energies_lj = np.dot(fdata, coeffs)

# Buckingham fit
def buckingham(r, a, b, c):
    return a * np.exp(-b * r) - (c * r ** (-6))
popt, pcov = curve_fit(buckingham, distances, energies, method='trf')
fit_energies_buckingham = buckingham(fpoints, *popt)

# Morse fits
# def morse_2(r, a, b):
#     return a * (1 - np.exp(-b * r)) ** 2
# def morse_3(r, a, b, c):
#     return a * (1 - np.exp(-b * (r - c))) ** 2
def morse_4(r, a, b, c, d):
    return (a * (1 - np.exp(-b * (r - c))) ** 2) + d
# tstart = [1.0e+3, 1]
# popt, pcov = curve_fit(morse_2, distances, energies, method='trf', p0=tstart, maxfev=40000000)
# fit_energies_morse_2 = morse_2(fpoints, *popt)
# tstart = [3.41838629,  1.7536397,  3.32438717]
# popt, pcov = curve_fit(morse_3, distances, energies, method='trf', p0=tstart, maxfev=40000000)
# fit_energies_morse_3 = morse_3(fpoints, *popt)
tstart = [1.0e+3, 1, 3, 0]
popt, pcov = curve_fit(morse_4, distances, energies, method='trf', p0=tstart, maxfev=40000000)
fit_energies_morse_4 = morse_4(fpoints, *popt)

fig, ax = plt.subplots()
ax.set_xlim((2, max(distances)))
ax.set_ylim((-10, 2))
ax.scatter(distances, energies, color='black', label='MP2/aug-cc-pVDZ (no CP)')
ax.plot(fpoints, fit_energies_lj, label='Lennard-Jones fit')
ax.plot(fpoints, fit_energies_buckingham, label='Buckingham fit')
# ax.plot(fpoints, fit_energies_morse_2, label='Morse fit (2 param)')
# ax.plot(fpoints, fit_energies_morse_3, label='Morse fit')
ax.plot(fpoints, fit_energies_morse_4, label='Morse fit (shifted)')
ax.plot([0,10], [0,0], 'k-')
ax.set_xlabel(r'interatomic separation ($\AA{}$)')
ax.set_ylabel(r'interaction energy ($\mathrm{cm^{-1}}$)')
ax.legend(loc='best', fancybox=True, framealpha=0.50)
fig.savefig('1b_molecule_buckingham_nocp_long.pdf', bbox_inches='tight')


from bs4 import BeautifulSoup
from src.BrowseUtils import fetch_url, SOUP_PARSER_HTML
from src.log import log

base_url = "http://9anime.to"
base_watch_url = "https://9anime.to/watch"

def sanitize_name(name):
    new_name = ''
    for c in name.lower():
        if c.isalnum() or c.isspace():
            new_name += c
        else:
            new_name += ' '

    new_name = ' '.join(new_name.split())  # remove double-spaces and strip
    return new_name

def sanitized(func):
    def sanitized_func(name, *args, **kwargs):
        return func(sanitize_name(name), *args, **kwargs)
    return sanitized_func

@sanitized
def search_series_urls_by_name(name):
    log('searching "{}"'.format(name))
    page = fetch_url(base_url + "/search?keyword=" + name.replace(' ', '+'))
    soup = BeautifulSoup(page, SOUP_PARSER_HTML)
    posters = soup.find_all('a', {'class': 'poster'})
    names = [poster.find('img')['alt'] for poster in posters]
    urls = [poster['href'] for poster in posters]
    return names, urls

@sanitized
def find_series_urls_by_name_substring(name):
    return [url for _name, url in zip(*search_series_urls_by_name(name)) if sanitize_name(_name).find(name) >= 0]

@sanitized
def find_series_urls_by_keywords(name):
    @sanitized
    def is_match(txt):
        for keyword in name.split(' '):
            if txt.find(keyword) == -1:
                return False
        return True

    return [url for _name, url in zip(*search_series_urls_by_name(name)) if is_match(_name)]

@sanitized
def find_series_url_by_name(name):
    results = [url for _name, url in zip(*search_series_urls_by_name(name)) if name == sanitize_name(_name)]

    if len(results) == 0:
        log("watching page of {} couldn't be found. please check for typos or switch to names in opposite language (english/japanese)".format(name))
        raise Exception()
    elif len(results) > 1:
        log("more than 1 result were found for {}, choosing the first one;".format(name))

    result = results[0]
    log('found watching page of {}: {}'.format(name, result))
    return result


import pygame

WIDTH = 7
HEIGHT = 6
TILE_SIZE = 100

COLOURS = [None, (200, 200, 100), (200, 100, 100)]

class Board:
    def __init__(self, copy_from = None):
        """ No arguments: initialise a new empty board
            With argument: initialise a clone of the given board """
        if copy_from is None:
            self.squares = [0] * (WIDTH * HEIGHT)
            self.current_player = 1
            self.winning_line = None
        else:
            self.squares = copy_from.squares[:]
            self.current_player = copy_from.current_player
            self.winning_line = copy_from.winning_line

    def get_square(self, x, y):
        """ Return 0 for an empty square, 1 or 2 for a player's counter """
        return self.squares[y * WIDTH + x]

    def get_available_moves(self):
        """ Return a list of available moves (column numbers) """
        moves = []
        for x in xrange(WIDTH):
            if self.get_square(x, 0) == 0:
                moves.append(x)
        return moves

    def play_move(self, x):
        """ Given a move (a column number), play that move for the current player.
            If the move is legal, flip the current player and return True.
            If not, return False. """
        for y in xrange(HEIGHT-1, -1, -1): # bottom to top, to find the first available space
            i = y * WIDTH + x
            if self.squares[i] == 0:
                self.squares[i] = self.current_player
                self.current_player = 3 - self.current_player # Flip current player
                return True
        return False

    def unplay_move(self, x):
        """ Remove the top counter from column x, and flip the current player. """
        for y in xrange(HEIGHT): # top to bottom, to find the first occupied space
            i = y * WIDTH + x
            if self.squares[i] != 0:
                self.squares[i] = 0
                self.current_player = 3 - self.current_player # Flip current player
                return True
        return False

    def is_board_full(self):
        """ Return True if the board is completely full, False otherwise """
        return len(self.get_available_moves()) == 0

    def check_line(self, x, y, dx, dy):
        """ Check if a player has a line starting at x,y in the direction dx,dy.
            Return 1 or 2 for which player has a line, or None if there is no line. """
        if x + 3*dx >= WIDTH or y + 3*dy >= HEIGHT:
            return None

        owner = self.get_square(x, y)
        if owner == 0:
            return None

        for i in xrange(1, 4):
            if self.get_square(x + i*dx, y + i*dy) != owner:
                return None
        return owner

    def get_game_result(self, player):
        """ If this is a terminal state, return its value for the given player:
            +1 for a win, -1 for a loss, 0 for a draw.
            If this is a nonterminal state, return None. """
        if self.is_board_full():
            return 0

        for y in xrange(HEIGHT):
            for x in xrange(WIDTH):
                for dx, dy in [(1, 0), (0, 1), (1, 1), (1, -1)]:
                    winner = self.check_line(x, y, dx, dy)
                    if winner is not None:
                        self.winning_line = (x, y, dx, dy)
                        if winner == player:
                            return 1
                        else:
                            return -1

        return None

    def draw(self, screen):
        """ Draw the board """
        for x in xrange(WIDTH):
            if x % 2 == 1:
                rect = pygame.Rect(x * TILE_SIZE, 0, TILE_SIZE, HEIGHT * TILE_SIZE)
                pygame.draw.rect(screen, (245, 245, 245), rect)

            for y in xrange(HEIGHT):
                c = self.get_square(x, y)
                colour = COLOURS[c]
                if colour is not None:
                    rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
                    pygame.draw.ellipse(screen, colour, rect)
                    pygame.draw.ellipse(screen, (0,0,0), rect, 2)


# Needed on case-insensitive filesystems
from __future__ import absolute_import

# Try to import PIL in either of the two ways it can be installed.
try:
    from PIL import Image, ImageDraw
except ImportError:  # pragma: no cover
    import Image
    import ImageDraw

import qrcode.image.base
from qrcode.image import Style

import base64
import cStringIO
import webcolors

class PilImage(qrcode.image.base.BaseImage):
    """
    PIL image builder, default format is PNG.
    """
    kind = "PNG"
    mask = None

    def new_image(self, **kwargs):
        back_color = kwargs.get("back_color", "white")
        fill_color = kwargs.get("fill_color", "black")

        if fill_color.lower() != "black" or back_color.lower() != "white":
            if back_color.lower() == "transparent":
                mode = "RGBA"
                back_color = None
            else:
                mode = "RGB"
        else:
            mode = "1"

        img = Image.new(mode, (self.pixel_size, self.pixel_size), back_color)
        self.fill_color = fill_color
        try:
            self.back_color = webcolors.name_to_rgb(back_color)
        except:
            self.back_color = back_color
        self._idr = ImageDraw.Draw(img)
        return img

    def draw(self, row, col, style=Style.SQUARE):
        if self.escavate and not self.is_empty(row, col):
            return

        if style == Style.SQUARE:
            self._drawrect(row, col)
        elif style == Style.DOT:
            self._drawround(row, col)

    def _drawrect(self, row, col):
        box = self.pixel_box(row, col)
        self._idr.rectangle(box, fill=self.fill_color)

    def _drawround(self, row, col):
        circle_offset = self.box_size / 10
        box = self.pixel_box(row, col)
        box = (box[0][0]+circle_offset, box[0][1]+circle_offset, box[1][0]-circle_offset, box[1][1]-circle_offset)
        self._idr.ellipse(box, fill=self.fill_color)

    def add_logo(self, logo, override_img=None):
        image_string = cStringIO.StringIO(base64.b64decode(logo))
        img = Image.open(image_string)
        new_size = int(self.pixel_size / 6.2) * 2
        img = img.resize((new_size, new_size))
        center = self.pixel_size / 2
        (override_img or self._img).paste(img, (center - new_size/2, center - new_size/2, center + new_size/2, center + new_size/2), img)

    def set_mask(self, logo):
        img = Image.new("RGB", (self.pixel_size, self.pixel_size), "white")
        self.add_logo(logo, img)
        self.mask = img

    def save(self, stream, format=None, **kwargs):
        if format is None:
            format = kwargs.get("kind", self.kind)
        if "kind" in kwargs:
            del kwargs["kind"]
        if 'size' in kwargs:
            size = int(kwargs.get('size'))
            self._img = self._img.resize((size, size), Image.ANTIALIAS)
        self._img.save(stream, format=format, **kwargs)

    def __getattr__(self, name):
        return getattr(self._img, name)


import random

import matplotlib.image as mpimg
import numpy as np
import scipy.io
import functions as fn
from functions import nnCostFunction
from functions import optimize
from functions import predict
from functions import sigmoidGradient
from matplotlib import pyplot as plt

show_image = True
mat_dict = scipy.io.loadmat('./data/ex4data1.mat')
X = mat_dict['X']
print(X.shape)
y = mat_dict['y']
print(y.size)
# y = np.array([x - 1 if x < 10 else 0 for x in y])
num_labels = 10

m = X.shape[0]
print(m)

print(X[1].shape)
print(X[1].reshape((20, 20)).shape)
for i in random.sample(range(5000), 2):
    plt.imshow(X[i].reshape((20, 20)).T, cmap='gray',
               interpolation='bicubic')  # interpolation = 'bicubic'
    print(y[i])
    if show_image:
        plt.show()
img = mpimg.imread('./images/nn.png')
imgplot = plt.imshow(img)

if show_image:
    plt.show()

# % Weight regularization parameter (we set this to 0 here).
input_layer_size = 400  # % 20x20 Input Images of Digits
hidden_layer_size = 25  # % 25 hidden units
num_labels = 10  # % 10 labels, from 0 to 9
lambd = 1

parameters = fn.initialize_parameters(
    input_layer_size, hidden_layer_size, num_labels)
print(parameters["W1"].shape)
W1 = parameters["W1"]
b1 = parameters["b1"]
print(W1.shape)
W2 = parameters["W2"]
b2 = parameters["b2"]

yVec = np.zeros((m, num_labels))  # each number became bit vector
for l in range(m):
    idx = y[l]  # set bit according to index
    if idx == 10:
        idx = 0
    yVec[l, idx] = 1

J, cache = fn.nnCostFunction(W1, W2, b1, b2, input_layer_size, hidden_layer_size, num_labels, X, yVec, lambd)
print('Cost at parameters (loaded from ex4weights): ' +
      str(J) + ' (this value should be about 0.287629)')
print(fn.sigmoidGradient(0))
assert (fn.sigmoidGradient(0) == 0.25)

w1, w2, b1, b2 = fn.optimize(W1, W2, b1, b2, input_layer_size,
                             hidden_layer_size, num_labels, X, yVec, 1, 5000, True)

pred = fn.predict(w1, w2, b1, b2, X)

res = (np.count_nonzero(pred == np.array(y).reshape(m)) / m) * 100

print('Training Set Accuracy: in ' + str(res) + '%')

for i in random.sample(range(5000), num_labels):
    r = pred[i]
    res = 'The predicted value is ' + \
        str(r) + ', actual y is ' + str(y[i]) + '...'
    print(res)
    plt.imshow(X[i].reshape((20, 20)).T, cmap='gray',
               interpolation='bicubic')  # interpolation = 'bicubic'
    if show_image:
        plt.show()


from file_utils import WEIGHTS_NAME
from configuration_bert import BertConfig
from modeling_bert_align import BertForSequenceClassification
from tokenization_bert import BertTokenizer
import torch
import numpy as np
from processors.glue_align import glue_convert_examples_to_features as convert_examples_to_features
from processors.utils_align import DataProcessor, InputExample, InputFeatures
import xlrd
import json
from sklearn.metrics import f1_score

all_count=0
correct=0
fp=0
fn=0
tp=0
tn=0
results=[]
#logits_all=None
#paths=["D:\数据/data1.xlsx","D:\数据/data2.xlsx"]
paths=["./data/data1.xlsx","./data/data2.xlsx"]
config_class, model_class, tokenizer_class = BertConfig, BertForSequenceClassification, BertTokenizer
config = config_class.from_pretrained(r"./bert_align2/config.json")
tokenizer = tokenizer_class.from_pretrained(r"./pretrained/bertbase")
model = model_class.from_pretrained(r"./bert_align2/pytorch_model.bin", from_tf=False,config=config)
#config = config_class.from_pretrained(r"D:\代码\服务器代码中转\transformer\pretrained\checkpoint-500\config.json")
#tokenizer = tokenizer_class.from_pretrained(r"D:\代码\服务器代码中转\transformer\pretrained\robertalarge")
#model = model_class.from_pretrained(r"D:\代码\服务器代码中转\transformer\pretrained\checkpoint-500\pytorch_model.bin", from_tf=False,config=config)
model=model.cuda()
model.eval()

for path in paths:
    data = xlrd.open_workbook(path)
    table = data.sheets()[0]
    nrows = table.nrows
    for j in range(1, nrows):
        #output =None
        #labels=None
        all_count += 1
        samples = []
        sentences = table.row_values(j)[6].split("|")
        for sentence in sentences:
            sample = []
            sample.append(sentence)
            sample.append(table.row_values(j)[4])
            samples.append(sample)
        examples = []
        for i in range(len(samples)):
            guid = "%s" % (i)
            text_a = samples[i][0].lower()
            text_b = samples[i][1].lower()
            label = str(int(table.row_values(j)[3] >= 0.5))
            examples.append(
                InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
        features = convert_examples_to_features(examples,
                                                tokenizer,
                                                label_list=["0", "1"],
                                                max_length=64,
                                                output_mode="classification",
                                                pad_on_left=False,
                                                # pad on the left for xlnet
                                                pad_token=tokenizer.convert_tokens_to_ids([tokenizer.pad_token])[0],
                                                pad_token_segment_id=0,
                                                )
        #for f in features:
            #input_ids = torch.tensor(f.input_ids, dtype=torch.long).unsqueeze(0)
            #attention_mask = torch.tensor(f.attention_mask, dtype=torch.long).unsqueeze(0)
            #align_mask = torch.tensor(f.align_mask, dtype=torch.long).unsqueeze(0)
            #label = torch.tensor(f.label, dtype=torch.long).unsqueeze(0)
            #input_ids = torch.tensor(f.input_ids, dtype=torch.long).unsqueeze(0).cuda()
            #attention_mask = torch.tensor(f.attention_mask, dtype=torch.long).unsqueeze(0).cuda()
            #align_mask = torch.tensor(f.align_mask, dtype=torch.long).unsqueeze(0).cuda()
            #label = torch.tensor(f.label, dtype=torch.long).unsqueeze(0).cuda()
            #print(label.shape)
        all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long).cuda()
        all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long).cuda()
        all_align_mask = torch.tensor([f.align_mask for f in features], dtype=torch.long).cuda()
        all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long).cuda()
        all_labels = torch.tensor([f.label for f in features], dtype=torch.long).cuda()
        outputs = model(input_ids=all_input_ids, attention_mask=all_attention_mask, token_type_ids=all_token_type_ids,align_mask=all_align_mask,labels=all_labels)
        logits = outputs[1]
        logits=torch.nn.functional.softmax(logits,dim=-1)
        #print(logits.shape)
        output = logits.detach().cpu().numpy()
        labels = all_labels.detach().cpu().numpy()
        """
        if logits_all is None:
            logits_all=logits.detach().cpu().numpy()
        else:
            logits_all=np.append(logits_all,logits.cpu().detach().numpy(),axis=0)
        """
        #if output is None:
                #output = logits.detach().numpy()
                #labels = label.detach().numpy()
         #   output = logits.detach().cpu().numpy()
         #   labels = label.detach().cpu().numpy()
                #print(output.shape)
                #print(labels.shape)
        #else:
                #output = np.append(output, logits.detach().numpy(), axis=0)
                #labels = np.append(labels, label.detach().numpy(), axis=0)
        #    output = np.append(output, logits.detach().cpu().numpy(), axis=0)
         #   labels = np.append(labels, label.detach().cpu().numpy(), axis=0)
                #print(output.shape)
                #print(labels.shape)
        output=np.argmax(output,axis=1)
        correct+=int(np.any(output==labels))
        if int(np.any(output==labels))==1 and labels[0]==1:
            tp+=1
        if int(np.any(output == labels)) == 1 and labels[0] == 0:
            tn+=1
        if int(np.any(output == labels)) == 0 and labels[0] == 1:
            fn+=1
        if int(np.any(output == labels)) == 0 and labels[0] == 0:
            fp+=1
        """
        if labels[0]==1:
            correct+=int(np.any(output==labels))
        else:
            correct += int(np.all(output == labels))
        results.append({"answer": table.row_values(j)[4], "ref": table.row_values(j)[6], "logits": logits.detach().cpu().numpy().tolist(),"label":int(labels[0]),"iscorrect": int(np.any(output == labels)) if labels[0]==1 else int(np.all(output == labels))})
        """
        results.append({"answer": table.row_values(j)[4], "ref": table.row_values(j)[6],
                        "logits": logits.detach().cpu().numpy().tolist(), "label": int(labels[0]),
                        "iscorrect": int(np.any(output == labels))})
        print(correct)
        print(all_count)
        print(correct / all_count)
p=tp/(tp+fp)
r=tp/(tp+fn)
print(tp)
print(fp)
print(fn)
print(tn)
print(1.25*p*r/(0.25*p+r))
#print(correct/all_count)
#np.save("results.npy",results)
#np.save("logits.npy",logits_all)
#np.savetxt("logits.txt",logits_all)
with open("./results2/results_bert_align.json","w",encoding="utf8") as fout:
    for result in results:
        fout.write(json.dumps(result,ensure_ascii=False)+"\n")


import torch
import numpy as np

class Pruner:
    r"""General pruning class.
        """
    def __init__(self, masked_parameters):
        self.masked_parameters = list(masked_parameters)
        self.scores = {}
        self.processed_scores = {}

    def score(self, model, loss, dataloader, device):
        r"""Scoring function.
        """
        raise NotImplementedError

    def process(self, normalize):
        r"""Normalizes scores before masking.
        """
        for k, v in self.scores.items():
            if normalize:
                mean = torch.mean(v)
                std = torch.std(v)
                v = (v - mean) / std
            self.processed_scores[k] = v

    def _global_mask(self, sparsity):
        r"""Updates masks of model with scores by sparsity level globally.
        """
        global_scores = torch.cat([torch.flatten(v) for v in self.processed_scores.values()])
        k = int((1.0 - sparsity) * global_scores.numel())
        if not k < 1:
            threshold, _ = torch.kthvalue(global_scores, k)
            for mask, param in self.masked_parameters:
                score = self.processed_scores[id(param)]
                zero = torch.tensor([0.]).to(mask.device)
                one = torch.tensor([1.]).to(mask.device)
                mask.copy_(torch.where(score <= threshold, zero, one))

    def _local_mask(self, sparsity):
        r"""Updates masks of model with scores by sparsity level parameter-wise.
        """
        for mask, param in self.masked_parameters:
            score = self.processed_scores[id(param)]
            k = int((1.0 - sparsity) * score.numel())
            if not k < 1:
                threshold, _ = torch.kthvalue(torch.flatten(score), k)
                zero = torch.tensor([0.]).to(mask.device)
                one = torch.tensor([1.]).to(mask.device)
                mask.copy_(torch.where(score <= threshold, zero, one))

    def mask(self, sparsity, scope):
        r"""Updates masks of model with scores by sparsity according to scope.
        """
        if scope == 'global':
            self._global_mask(sparsity)
        if scope == 'local':
            self._local_mask(sparsity)

    @torch.no_grad()
    def apply_mask(self):
        r"""Applies mask to prunable parameters.
        """
        for mask, param in self.masked_parameters:
            param.mul_(mask)

    def alpha_mask(self, alpha):
        r"""Set all masks to alpha in model.
        """
        for mask, _ in self.masked_parameters:
            mask.fill_(alpha)

class Rand(Pruner):
    def __init__(self, masked_parameters):
        super(Rand, self).__init__(masked_parameters)

    def score(self, model, loss, dataloader, device):
        for _, p in self.masked_parameters:
            self.scores[id(p)] = torch.randn_like(p)

class Mag(Pruner):
    def __init__(self, masked_parameters):
        super(Mag, self).__init__(masked_parameters)

    def score(self, model, loss, dataloader, device):
        for _, p in self.masked_parameters:
            self.scores[id(p)] = torch.clone(p.data).detach().abs_()

class SNIP(Pruner):
    def __init__(self, masked_parameters):
        super(SNIP, self).__init__(masked_parameters)

    def score(self, model, loss, dataloader, device):

        for batch_idx, (data, target) in enumerate(dataloader):
            data, target = data.to(device), target.to(device)
            output = model(data)
            loss(output, target).backward()

        for _, p in self.masked_parameters:
            self.scores[id(p)] = torch.clone(p.grad * p.data).detach().abs_()
            p.grad.data.zero_()

class GraSP(Pruner):
    def __init__(self, masked_parameters):
        super(GraSP, self).__init__(masked_parameters)

    def score(self, model, loss, dataloader, device):

        for batch_idx, (data, target) in enumerate(dataloader):

            data, target = data.to(device), target.to(device)
            L = loss(model(data), target)

            grad = torch.autograd.grad(L, [p for (_, p) in self.masked_parameters], create_graph=True)

            flatten = torch.cat([g.reshape(-1) for g in grad if g is not None])
            gnorm = 0.5*flatten.pow_(2).sum()
            gnorm.backward()

        for _, p in self.masked_parameters:
            self.scores[id(p)] = torch.clone(-p.grad * p.data).detach()
            p.grad.data.zero_()

class SynFlow(Pruner):
    def __init__(self, masked_parameters):
        super(SynFlow, self).__init__(masked_parameters)

    def score(self, model, loss, dataloader, device):

        @torch.no_grad()
        def linearize(model):
            signs = {}
            for name, param in model.state_dict().items():
                signs[name] = torch.sign(param)
                param.abs_()
            return signs

        @torch.no_grad()
        def nonlinearize(model, signs):
            for name, param in model.state_dict().items():
                param.mul_(signs[name])

        signs = linearize(model)

        (data, _) = next(iter(dataloader))
        input_dim = list(data[0,:].shape)
        input = torch.ones([1] + input_dim).to(device)
        output = model(input)
        torch.sum(output).backward()

        for _, p in self.masked_parameters:
            self.scores[id(p)] = torch.clone(p.grad * p).detach().abs_()
            p.grad.data.zero_()

        nonlinearize(model, signs)


import atexit

#------------------------------------------------------------
# Register Failure Functions
#------------------------------------------------------------
# Make the build fail if we pass fail=1 on the command line
def bf_to_str(bf):
    """Convert an element of GetBuildFailures() to a string
    in a useful way."""
    import SCons.Errors
    if bf is None: # unknown targets product None in list
        return '(unknown tgt)'
    elif isinstance(bf, SCons.Errors.StopError):
        return str(bf)
    elif bf.node:
        return str(bf.node) + ': ' + bf.errstr
    elif bf.filename:
        return bf.filename + ': ' + bf.errstr
    return 'unknown failure: ' + bf.errstr

def build_status():
    """Convert the build status to a 2-tuple, (status, msg)."""
    from SCons.Script import GetBuildFailures
    bf = GetBuildFailures()
    if bf:
        # bf is normally a list of build failures; if an element is None,
        # it's because of a target that scons doesn't know anything about.
        status = 'failed'
        failures_message = "\n".join(["Failed building %s" % bf_to_str(x)
                           for x in bf if x is not None])
    else:
        # if bf is None, the build completed successfully.
        status = 'ok'
        failures_message = ''
    return (status, failures_message)


from django.db import models
from main.models import User,Language

# Create your models here.
class ForumSections(models.Model):
    name = models.CharField(max_length=64,unique=True)
    title = models.CharField(max_length=256)
    description = models.TextField()

class Thread(models.Model):
    section = models.ForeignKey(ForumSections,
                                on_delete=models.RESTRICT)
    language = models.ForeignKey(Language,
                                 on_delete=models.RESTRICT)
    title = models.CharField(max_length=512)
    created_by = models.ForeignKey(User,
                                   related_name='forum_thread',
                                   on_delete=models.RESTRICT)
    created_on = models.DateTimeField(auto_now_add = True)
# Thread()

class ThreadPost(models.Model):
    Thread = models.ForeignKey(Thread,
                               on_delete=models.RESTRICT)
    created_by = models.ForeignKey(User,
                                   on_delete=models.RESTRICT,
                                   related_name='forum_post')
    created_on = models.DateTimeField(auto_now_add=True)
    post = models.TextField()
# ThreadPost()


import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# from moviepy.editor import VideoFileClip

overhead = cv2.imread('test_images/test7.jpg')
#h,l = cv2.cvtColor(overhead, cv2.COLOR_BGR2HLS)
(h,l,s) = cv2.cvtColor(overhead, cv2.COLOR_BGR2HLS)[:,:,:]

plt.imshow(cv2.cvtColor(overhead,cv2.COLOR_BGR2RGB) )
plt.show()
plt.imshow(hls[0])
plt.show()
exit(1)

(h,l,s) = cv2.cvtColor(overhead, cv2.COLOR_BGR2HLS)[:,:,:]
l_channel = cv2.cvtColor(overhead, cv2.COLOR_BGR2LUV)[:, :, 0]
b_channel = cv2.cvtColor(overhead, cv2.COLOR_BGR2Lab)[:, :, 2]

# Threshold color channel
s_thresh_min = 150
s_thresh_max = 255
s_binary = np.zeros_like(s)
s_binary[(s >= s_thresh_min) & (s <= s_thresh_max)] = 1

b_thresh_min = 155
b_thresh_max = 190
b_binary = np.zeros_like(b_channel)
b_binary[(b_channel >= b_thresh_min) & (b_channel <= b_thresh_max)] = 1

l_thresh_min = 210
l_thresh_max = 255
l_binary = np.zeros_like(l_channel)
l_binary[(l_channel >= l_thresh_min) & (l_channel <= l_thresh_max)] = 1

combined_binary = np.zeros_like(s_binary)
combined_binary[(l_binary == 1) | (b_binary == 1)] = 1

min = 150
max = 255

min = 200
max = 255
while True:
    print('min',min)
    # min = int(input('Enter min: '))
    # max = int(input('Enter max: '))
    s_binary = np.zeros_like(s)
    s_binary[(s >= 210) & (s <= 255)] = 1
    b_binary = np.zeros_like(b_channel)
    b_binary[(b_channel >= 170) & (b_channel <= 255)] = 1
    l_binary = np.zeros_like(l_channel)
    l_binary[(l_channel >= 210) & (l_channel <= 255)] = 1

    # cv2.imshow('Hi',s_binary)
    # cv2.imshow('Hi',combined_binary)
    plt.imshow(s_binary)
    plt.show()
    plt.imshow(b_binary)
    plt.show()
    plt.imshow(l_binary)
    plt.show()
    # if cv2.waitKey(1000) == 27 or min > max:
    #     break
    # min = min + 1

cv2.destroyAllWindows()


# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
FILE: sample_check_document_statuses_async.py

DESCRIPTION:
    This sample demonstrates how to begin translation and then monitor each document's status
    and progress.

    To set up your containers for translation and generate SAS tokens to your containers (or files)
    with the appropriate permissions, see the README.

USAGE:
    python sample_check_document_statuses_async.py

    Set the environment variables with your own values before running the sample:
    1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
    2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
    3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents
        to be translated.
    4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents
        will be written.
"""

import asyncio

async def sample_document_status_checks_async():
    # [START list_document_statuses_async]
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
    source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"]
    target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

    async with client:
        poller = await client.begin_translation(source_container_url, target_container_url, "es")

        completed_docs = []
        while poller.status() in ["Running", "NotStarted"]:
            await asyncio.sleep(30)

            doc_statuses = client.list_document_statuses(poller.id)
            async for document in doc_statuses:
                if document.id not in completed_docs:
                    if document.status == "Succeeded":
                        print(f"Document at {document.source_document_url} was translated to {document.translated_to} "
                              f"language. You can find translated document at {document.translated_document_url}")
                        completed_docs.append(document.id)
                    if document.status == "Failed" and document.error:
                        print(f"Document at {document.source_document_url} failed translation. "
                              f"Error Code: {document.error.code}, Message: {document.error.message}")
                        completed_docs.append(document.id)
                    if document.status == "Running":
                        print(f"Document ID: {document.id}, translation progress is "
                              f"{document.translation_progress * 100} percent")

        print("\nTranslation completed.")
    # [END list_document_statuses_async]

async def main():
    await sample_document_status_checks_async()

if __name__ == '__main__':
    asyncio.run(main())


url = 'https://hahow.in/courses'

# Part.1 static crawl
import requests
from bs4 import BeautifulSoup
response = requests.get(url).text
soup = BeautifulSoup(response, features='lxml')
with open('static.html', 'w') as fd:
    fd.write(str(soup.prettify()))
    print('static way only get partial response')

# Part.2 dynamic crawl
from selenium import webdriver
browser = webdriver.Chrome(executable_path='../tools/chromedriver.exe')
browser.get(url)
content = browser.page_source
browser.close() # only close the window, the driver is still work.
browser.quit() # close all the windows and also the driver.
soup = BeautifulSoup(content, features='lxml')
with open('dynamic.html', 'w') as fd:
    fd.write(str(soup.prettify()))
    print('dynamic way get the full response')