#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
import wsgiref.handlers
import os
import re
import logging
import urllib
import simplejson
from operator import itemgetter
import ConfigParser
from main import BaseRequestHandler
import datetime

config = ConfigParser.ConfigParser()
config.read('config.cfg')

class MainHandler(BaseRequestHandler):

	def fetch_day(self):
		date = self.request.get("date")

		if (not(date) or date == 'latest'):
			today = datetime.date.today()
			yesterday = today - datetime.timedelta(1)
			date = yesterday.strftime("%Y-%m-%d")

		if (not(date) or not(re.match("^\d\d\d\d-\d\d-\d\d$", date))):
			return ([], None)

		twends = memcache.get("twends/date/%s" % (date))
		if (twends is not None):
			return (twends, date)

		base_url = 'http://search.twitter.com/trends/daily.json'
		json = simplejson.load(urllib.urlopen(base_url + '?' + urllib.urlencode({
								'date': date
							})
		))

		try:
				trends = json['trends']
		except (KeyError, IndexError):
				return (None, None)
		structured_trends = {}
		all_times = []

		for date_time in trends.keys():
			date = date_time[:10]
			time = re.sub(".*? (.*?)", "\1", date_time)
			all_times.append(time)

			for trend in trends[date_time]:
				if (not(trend["name"] in structured_trends)):
					structured_trends[trend["name"]] = { "count": 0, "timelist": [] }

				structured_trends[trend["name"]]["timelist"].append(time)

		all_times = sorted(all_times)

		for trend in structured_trends:
			structured_trends[trend]["timelist"] = sorted(structured_trends[trend]["timelist"])
			structured_trends[trend]["count"] = len(structured_trends[trend]["timelist"])
			i = 0
			timespans = []

			while i < len(structured_trends[trend]["timelist"]):
				all_times_index = all_times.index(structured_trends[trend]["timelist"][i])
				trend_index_start = i
				trend_index_counter = i

				while ((trend_index_counter < len(structured_trends[trend]["timelist"]) and
							cmp(all_times[all_times_index], structured_trends[trend]["timelist"][trend_index_counter]) == 0)):
					all_times_index += 1
					trend_index_counter += 1

				timespans.append((structured_trends[trend]["timelist"][i],structured_trends[trend]["timelist"][trend_index_counter-1]))
				i = trend_index_counter
			structured_trends[trend]["timelist"] = timespans

		sorted_trends = sorted(structured_trends.items(), key=lambda k: k[1]['count'])
		sorted_trends.reverse()
		memcache.set("twends/date/%s" % (date), sorted_trends)
		return (sorted_trends, date)

	def get(self):
		(twends, date) = self.fetch_day()

		context = {
				"twends": twends,
				"date": date,
				"success": True
				}

		if (date != None):
			(year, month, day) = date.split('-')
			today = datetime.date(int(year), int(month), int(day))

			start_date = datetime.date(2008,11,19)
			diff = start_date - today
			if (diff.days > 0):
				context['too_early'] = True
				context['success'] = False
			else:
				context['wikipedia_date'] = today.strftime("%B_%Y#%Y_%B_%d")

				yesterday = today - datetime.timedelta(1)
				tomorrow = today + datetime.timedelta(1)
				context['yesterday'] = yesterday.strftime("%Y-%m-%d")

				delta = datetime.date.today() - today
				if (delta.days != 1):
					context["tomorrow"] = tomorrow.strftime("%Y-%m-%d")

				if (delta.days < 0):
					context["in_future"] = True
					context["success"] = False
		else:
			context["success"] = False

		context['google_analytics_key'] = config.get('blog', 'google_analytics_key')
		self.render('projects/twends/base.html', context)

def main():
	logging.getLogger().setLevel(logging.DEBUG)

	application = webapp.WSGIApplication(
																				[
																					(r'.*', MainHandler),
																				],
																				debug=True)
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
	main()


# -*- coding: utf-8 -*-
"""
Created on Tue Apr  1 10:58:45 2014

@author:
"""

import matplotlib
from pylab import *
import numpy as np

data = np.loadtxt('/Users/pczrr/Documents/Work/Project-Folders/B-subtilis-DNA-Replication/Processing/DnaE3-alignment/Analysis/HMM_method/Overall_DnaE_noPolC_FScomparison.txt', converters=None, dtype='float',
                  delimiter='\t', skiprows=1, usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20))

new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', cmap='RdGy', N=50)
pcolor(data, cmap=new_map, vmin=-4, vmax=4)
colorbar()
savefig('map.png')
show()


# -*- coding: utf-8 -*-
"""
Created on Sun Oct 18 12:10:49 2020

@author: koushb
"""

import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score

import lightgbm as lgbm
from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

df_train=pd.read_csv("../Data/House Price Prediction/train.csv")
df_train['date']=pd.to_datetime(df_train['date'])
df_train['year']=df_train['date'].dt.year
df_train['month']=df_train['date'].dt.month

y=df_train['price']
X=df_train.drop(['price','id','date'],axis=1)

reg=lgbm.LGBMRegressor(
    objective='regression',
    boosting_type='gbdt',
    learning_rate=0.1,
    num_leaves=31,
    n_estimators=25,
    min_split_gain=0.1,
    subsample=0.6,
    colsample_bytree=0.8,
    reg_alpha=5,
    reg_lambda=5,
    )

reg.fit(X,y)
pred=reg.predict(X)
pred[pred>df_train['price'].max()]=df_train['price'].max()
pred[pred<df_train['price'].min()]=df_train['price'].min()
print('MSE is:',np.sqrt(mse(y,pred)))
print('R2 is:',r2_score(y,pred))
plt.scatter(y,pred,s=1)
plt.xlim(0,8000000)
plt.ylim(0,8000000)
scores=cross_val_score(reg,X=X,y=y,cv=5,scoring='r2')
print(scores)
print('cv_mean:',round(scores.mean(),2))
print('cv_std:',round(scores.std(),2))

gridparam={
    'num_leaves':[30,50],
    'boosting_type':['gbdt','dart'],
    'learning_rate':[0.1,0.01],
    'n_estimators':[25,50],
    'subsample':[0.7,0.9],
    'colsample_bytree':[0.8,0.9],
    'reg_alpha':[0,5,10],
    'reg_lambda':[0,5,10]

    }

grid=GridSearchCV(reg,gridparam,cv=4,verbose=1)
grid.fit(X,y)

print(grid.best_score_)
print(grid.best_params_)

reg=lgbm.LGBMRegressor(
    objective='regression',
    boosting_type=grid.best_params_['boosting_type'],
    learning_rate=grid.best_params_['learning_rate'],
    num_leaves=grid.best_params_['num_leaves'],
    n_estimators=grid.best_params_['n_estimators'],
    min_split_gain=0.1,
    subsample=grid.best_params_['subsample'],
    colsample_bytree=grid.best_params_['colsample_bytree'],
    reg_alpha=grid.best_params_['reg_alpha'],
    reg_lambda=grid.best_params_['reg_lambda'],
    )

reg.fit(X,y)
pred=reg.predict(X)
pred[pred>df_train['price'].max()]=df_train['price'].max()
pred[pred<df_train['price'].min()]=df_train['price'].min()
print('MSE is:',np.sqrt(mse(y,pred)))
print('R2 is:',r2_score(y,pred))
plt.scatter(y,pred,s=1)
plt.xlim(0,8000000)
plt.ylim(0,8000000)
scores=cross_val_score(reg,X=X,y=y,cv=5,scoring='r2')
print(scores)
print('cv_mean:',round(scores.mean(),2))
print('cv_std:',round(scores.std(),2))

#######################################################
params = {
    'application': 'regression',
#     'num_class' : 1, # used for multi-classes
    'boosting': 'gbdt', # traditional gradient boosting decision tree
    'num_iterations': 1000,
    'learning_rate': 0.05,
    'num_leaves': 62,
    'device': 'cpu', # you can use GPU to achieve faster learning
    'max_depth': -1, # <0 means no limit
    'max_bin': 510, # Small number of bins may reduce training accuracy but can deal with over-fitting
    'lambda_l1': 5, # L1 regularization
    'lambda_l2': 10, # L2 regularization
    'metric' : 'rmse',
    'subsample_for_bin': 200, # number of samples for constructing bins
    'subsample': 0.8, # subsample ratio of the training instance
    'colsample_bytree': 0.8, # subsample ratio of columns when constructing the tree
    'min_split_gain': 0.5, # minimum loss reduction required to make further partition on a leaf node of the tree
    'min_child_weight': 1, # minimum sum of instance weight (hessian) needed in a leaf
    'min_child_samples': 5# minimum number of data needed in a leaf
}

X_train,X_valid,y_train,y_valid=train_test_split(X,y,test_size=0.2)
d_train=lgbm.Dataset(X_train,label=y_train)
d_valid=lgbm.Dataset(X_valid,label=y_valid)

model=lgbm.train(params,train_set=d_train,num_boost_round=1000,valid_sets=d_valid,early_stopping_rounds=50,verbose_eval=4)


import pygame
import random
import math
import physEnv as env
pygame.init()

# random() - returns a random number between 0.0 and 1.0
# uniform(a, b) - returns a random number between a and b
# randint(a, b) - returns a random integer between a and b
# choice(list) - returns a random element from the list
# shuffle(list) - rearranges the order of the list
pygame.mouse.get_pos()

run = True
width = 500
height = 500
gravity = [math.pi, 0.45]
elasticity = 0.75

pygame.display.set_caption("hello")
win = pygame.display.set_mode((width, height))

clock = pygame.time.Clock()
selected_particle = None

particles = env.addParticle(20, 500, 500)

def display(self):
    pygame.draw.circle(win, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)

while run is True:

    clock.tick(30)
    win.fill((0, 0, 0))
    keys= pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            (mouseX, mouseY) = pygame.mouse.get_pos()
            selected_particle = env.find(particles, mouseX, mouseY)
        elif event.type == pygame.MOUSEBUTTONUP:
            selected_particle = None

    if selected_particle:
        (mouseX, mouseY) = pygame.mouse.get_pos()
        selected_particle.colour = (255, 0, 0)
        dx = mouseX - selected_particle.x
        dy = mouseY - selected_particle.y
        selected_particle.angle = 0.5*math.pi + math.atan2(dy, dx)
        selected_particle.speed = math.hypot(dx, dy) * 0.1
    else:
        for i in range(len(particles)):
            particles[i].colour = (255, 255, 255)

    for i in range(len(particles)):
        env.Particle.move(particles[i])
        display(particles[i])
        env.Particle.bounce(particles[i])
        env.collide(particles)
    pygame.display.flip()

pygame.quit


import matplotlib.pyplot as plt
import numpy as np
import math as math
#from scipy import spatial

import sys
print(sys.version)
import warnings
warnings.filterwarnings('ignore')

from sklearn.neighbors import KDTree

def SVDTransformFinder(X,P):

    Mu_X=np.mean(X, axis=1,keepdims=True)
    Mu_P=np.mean(P, axis=1,keepdims=True)

    X_Prime=X-Mu_X
    P_Prime=P-Mu_P
    W=np.dot(X_Prime,P_Prime.T)

    u,s,vh=np.linalg.svd(W , full_matrices=True)

    print("---------------SVD result----------------\n")

    R=np.matmul(u,vh)
    print ("rotation: ",R)
    T=Mu_X-np.matmul(R,Mu_P)
    print ("Translation: ",T)
    return R,T

if __name__ == "__main__":

    point_count = 50
    th = np.pi / 8
    move = np.array([[0.40], [0.5]])
    x1 = np.linspace(0, 1.1, point_count)
    y1 = np.sin(x1 * np.pi)
    d1 = np.array([x1, y1])

    rot = np.array([[np.cos(th), -np.sin(th)], [np.sin(th), np.cos(th)]])
    d2 = np.dot(rot, d1) + move
    #d2 = np.dot(rot, d1)
    #d2 = d1 + move

    print ("-------------original values---------------\n")
    print ("rot", rot)
    print ("move", move)

    print ("---------------------------------\n")

    xmin=-2
    xmax=6
    ymin=-1
    ymax=4

    plt.axis([xmin,xmax,ymin,ymax])

#Ploting points
    plt.plot(d1[0], d1[1])
    plt.plot(d2[0], d2[1])

    x1=d1[0]
    y1=d1[1]
    x2=d2[0]
    y2=d2[1]

    print ("---------------------------x1.shape-----------------------------\n")

    print (x1.shape)

    plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)

    R,T=SVDTransformFinder(d2,d1)

    d = np.dot(R, d1)+T

    print ("rotation: ",R)

    print ("Translation: ",T)

    plt.plot(d[0], d[1],color='green')

    plt.show()

    print ("-------------------------------------------------------------\n")

    tmp= d1.T
    qurey_point= d2.T

    print ("qurey_point.shape", qurey_point.shape)
    tree = KDTree(tmp)
    nearest_dist, nearest_ind = tree.query(qurey_point, k=1)  # k=2 nearest neighbors where k1 = identity
    print ("nearest_ind", nearest_ind )    # drop id)
    print ("tmp[nearest_ind]", tmp[nearest_ind])

    result=tmp[nearest_ind]
    result=result.reshape(50,2)
    print ( "result.shape", result.shape)

    x1=result[:,0]
    y1=result[:,1]

    x2=qurey_point[:,0]
    y2=qurey_point[:,1]

    plt.axis([xmin,xmax,ymin,ymax])

    print ("--------------------------------------")
    print (x2)
    print ("--------------------------------------")
    print (y2)

    plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)
    plt.scatter([x1, x2], [y1, y2])

    plt.show()

#     tmp=np.array([ [1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ]    )
#     qurey_point=np.array(    [ [1.5, 1.4] ,  [1.5, 1.9]  ]    )
#     tree = KDTree(tmp)
#     nearest_dist, nearest_ind = tree.query(qurey_point, k=1)  # k=2 nearest neighbors where k1 = identity
#     print(nearest_ind)     # drop id
#     print(tmp[nearest_ind])

#     tree = KDTree(d1)
#     nearest_dist, nearest_ind = tree.query(d2, k=1)
#
#
#
#     print "----------------------------nearest_ind---------------------------------\n"
#
#     print nearest_ind
#
#     print nearest_dist
#
#     print "----------------------------########################---------------------------------\n"
#
#
#
#     x1=d1[nearest_ind][0]
#     y1=d1[nearest_ind][1]
#
#
#     x1=x1.T
#     y1=y1.T
#
#     print x1.shape
#     print y1.shape
#
#
#
#     x1=x1.reshape(50,)
#     y1=y1.reshape(50,)
#
#
#     x2=d2[0]
#     y2=d2[1]
#
#
#
#
#
# #    x2=x2.reshape(50,1)
# #    y2=y2.reshape(50,1)
#
#     print x2.shape
#     print y2.shape
#
#     plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)
#
#     plt.show()


"""oj URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import include, url

urlpatterns = [
    url(r"^api/", include("demo.urls.oj")),
    url(r"^teacher/lab/", include("lab.urls.admin")),
    url(r"^teacher/submission/", include("submission.urls.admin")),
    url(r"^api", include("lecture.urls.admin")),
    url(r"^administrator/role/", include("user.urls.admin")),
    url(r"^exam/student", include("exam.urls.admin")),
]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Nov 18, 2011

@author: 章红兵
"""
from os import path
from scrapy import signals
from scrapy.xlib.pydispatch import dispatcher
import codecs
import time
class FilePipeline(object):
    #   保存数据的文件名
    filename = '360buy_news_%s.txt' % time.strftime('%Y%m%d')
    def __init__(self):
        self.f = None
        dispatcher.connect(self.open, signals.engine_started)
        dispatcher.connect(self.close, signals.engine_stopped)

    def process_item(self,item,spider):
        # 从对象中提取数据
        subject = item['subject'][0]
        url = item['url'][0]
        created = item['created'][0]
        modified = item['modified'][0]
        url = item['url'][0]

        sql = "INSERT INTO think_discountnews (subject, url, created,modified) \
                VALUES ('%s', %s, '%s', '%s', '%s');\n" % (subject, url, created, modified )

        #把数据写入文件
#        self.f.write(sql + '\n')

        with codecs.open(self.filename, 'a+', 'utf-8') as f:
            f.write(sql.replace('[]',''))
        return item

    def open(self):
        # 当文件存在只写
        if path.exists(self.filename):
            self.f = open(self.filename, 'a')
        else:
            #当文件不存在创建后再写
            self.f = open(self.filename, 'w')

    def close(self):
        self.f.close() if self.f is not None else None


# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from azure.identity import DefaultAzureCredential
from azure.mgmt.netapp import NetAppManagementClient

"""
# PREREQUISITES
    pip install azure-identity
    pip install azure-mgmt-netapp
# USAGE
    python volume_groups_create.py

    Before run the sample, please set the values of the client ID, tenant ID and client secret
    of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
    AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
    https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""

def main():
    client = NetAppManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id="D633CC2E-722B-4AE1-B636-BBD9E4C60ED9",
    )

    response = client.volume_groups.begin_create(
        resource_group_name="myRG",
        account_name="account1",
        volume_group_name="group1",
        body={
            "location": "westus",
            "properties": {
                "groupMetaData": {
                    "applicationIdentifier": "DEV",
                    "applicationType": "SAP-HANA",
                    "deploymentSpecId": "20542149-bfca-5618-1879-9863dc6767f1",
                    "groupDescription": "Volume group",
                },
                "volumes": [
                    {
                        "name": "test-data-mnt00001",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-data-mnt00001",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "data",
                        },
                    },
                    {
                        "name": "test-log-mnt00001",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-log-mnt00001",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "log",
                        },
                    },
                    {
                        "name": "test-shared",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-shared",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "shared",
                        },
                    },
                ],
            },
        },
    ).result()
    print(response)

# x-ms-original-file: specification/netapp/resource-manager/Microsoft.NetApp/stable/2022-11-01/examples/VolumeGroups_Create.json
if __name__ == "__main__":
    main()


from asgiref.sync import async_to_sync

from django.dispatch import receiver
from django.db.models.signals import post_save, m2m_changed

from channels.layers import get_channel_layer

from game.models import Session
from game.serializers import SessionSerializer

@receiver(post_save, sender=Session)
def session_changed(sender, instance, *args, **kwargs):
    channel_layer = get_channel_layer()
    session_id = instance.session_id
    group_name = f"session-{session_id}"

    serializer = SessionSerializer(instance)

    event = {
        "type": "session_data",
        "session": serializer.data,
    }

    async_to_sync(channel_layer.group_send)(
        group_name,
        event
    )

@receiver(m2m_changed, sender=Session.players.through)
def session_players_changed(sender, instance, *args, **kwargs):
    if kwargs["action"] == "pre_add":
        print(f"Added into: {instance}")

        players = instance.players.all()
        if len(players) == 0:
            print("no players. adding 1st")
        elif len(players) < 5:
            print("not capacity")
        elif len(players) >= 5:
            print("Room is at capacity")

    if kwargs["action"] == "post_remove":
        players = instance.players.all()

        if len(players) == 0:
            print("no players left. removing")

            try:
                instance.delete()
            except:
                print("Delete failed")

    channel_layer = get_channel_layer()
    session_id = instance.session_id
    group_name = f"session-{session_id}"

    serializer = SessionSerializer(instance)

    event = {
        "type": "session_data",
        "session": serializer.data,
    }

    async_to_sync(channel_layer.group_send)(
        group_name,
        event
    )


import logging
import sys

from cliff.command import Command

import utils
import settings

LOG = logging.getLogger(__name__)

class install(object):

    def __init__(self):
        pass

    def install(self, node, pkg_name):
        utils.apt_get(node, pkg_name)

    def conf(self, node, content, remote_path, tmp_path='/tmp/salt_tmp.conf'):
        '''
        args type:
          node: str
          content: str
          remote_path: str
        '''

        utils.write_data(tmp_path, content)
        utils.upload_file(tmp_path, remote_path, node)

    def service(self, node, service_name):
        ''' restart service no remote host'''
        cmd = "service {0} restart".format(service_name)
        utils.exec_remote_cmd(cmd, node)

    def iptables(self, node, port):
        cmd = r''' grep -q "{0}" /etc/iptables/rules.v4 || echo none'''.format(port)
        result = utils.exec_remote_cmd(cmd, node)
        if result == 'none':
            cmd = ''' sed -r -i '/INPUT -p icmp/s/(.*)/\1\n-A INPUT -p tcp -m multiport --ports {0} -j ACCEPT/' /etc/iptables/rules.v4 '''.format(port)
            utils.exec_remote_cmd(cmd, node)
            self.service(node, 'iptables-persistent')

class salt_minion(install):
    def __init__(self, nodes, salt_master, pkg_name='salt-minion'):
        self.pkg_name = pkg_name
        self.salt_master = salt_master

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-minion on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-minion on {0}:\033[0m'.format(node))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-minion on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node):
        LOG.info('\033[1;31mcreating /etc/salt/minion_id on {0}:\033[0m'.format(node))
        mkdir = '''mkdir -p /etc/salt/'''
        utils.exec_remote_cmd(mkdir, node)
        minion_id = '''echo {0} > /etc/salt/minion_id'''.format(node)
        utils.exec_remote_cmd(minion_id, node)

        LOG.info('\033[1;31minstalling salt-minion on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)

        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/minion.d/master.conf'
        content = 'master: {0}'.format(self.salt_master)
        self.conf(node, content, remote_path)

        LOG.info('\033[1;31mrestart salt-minion on {0}:\033[0m'.format(node))
        self.service(node, 'salt-minion')
        self.iptables(node, '4505')
        self.iptables(node, '4506')

class salt_api(install):
    def __init__(self, nodes, pkg_name='salt-api python-cherrypy3'):
        self.pkg_name = pkg_name

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-api on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-api on {0}:\033[0m'.format(nodes))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-api on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node, api_user='salt_api', api_pass='salt_api', api_port=18000, api_listen='0.0.0.0'):
        LOG.info('\033[1;31minstalling salt-api on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)
        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/master.d/api.conf'

        content = ['external_auth:',
                   '  pam:',
                   '    {0}:'.format(api_user),
                   '      - .*',
                   'rest_cherrypy:',
                   '  port: {0}'.format(api_port),
                   '  host: {0}'.format(api_listen),
                   '  debug: True',
                   '  ssl_crt: /etc/pki/tls/certs/localhost.crt',
                   '  ssl_key: /etc/pki/tls/certs/localhost.key',
                   '\n',]
        content = '\n'.join(content)
        self.conf(node, content, remote_path)
        LOG.info('\033[1;31madd salt_api user on {0}:\033[0m'.format(node))
        cmd = " grep -q {0} /etc/passwd || useradd {0}".format(api_user)
        utils.exec_remote_cmd(cmd, node)
        cmd = " echo {0}:{1} | chpasswd".format(api_user, api_pass)
        utils.exec_remote_cmd(cmd, node)

        LOG.info('\033[1;31mgenrate keys on {0}:\033[0m'.format(node))
        cmd = 'salt-call --local tls.create_self_signed_cert '
        utils.exec_remote_cmd(cmd, node)

        LOG.info('\033[1;31mrestart salt-master salt-api on {0}:\033[0m'.format(node))
        self.service(node, 'salt-master')
        self.service(node, 'salt-api')

        self.iptables(node, api_port)

class salt_master(install):

    def __init__(self, nodes, groups, pkg_name='salt-master'):
        self.pkg_name = pkg_name
        self.groups = groups

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-master on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-master on {0}:\033[0m'.format(nodes))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-master on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node):
        LOG.info('\033[1;31minstalling salt-master on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)
        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/master.d/nodegroups.conf'
        content = 'auto_accept: True\n'
        content += 'nodegroups:\n'
        for role, nodes in self.groups.items():
            content += '  {0}: {1}\n'.format(role, ','.join(nodes))
        self.conf(node, content, remote_path)
        LOG.info('\033[1;31mrestart salt-master on {0}:\033[0m'.format(node))
        self.service(node, 'salt-master')

        self.iptables(node, '4505')
        self.iptables(node, '4506')

class saltstack(Command):
    '''install saltstack in openstack cluster. primary-controller is salt-master'''

    def get_parser(self, prog_name):
        parser = super(saltstack, self).get_parser(prog_name)
        parser.add_argument('--env', type=int, required=True, help='Fuel cluster ID')
        parser.add_argument('--master', action='store_true', help='Install salt-master on primary-controller')
        parser.add_argument('--api', action='store_true', help='Install salt-api on primary-controller')
        parser.add_argument('--minion', action='store_true', help='Install salt-minion on all nodes')
        return parser

    def take_action(self, parsed_args):
        self._get_nodedata(parsed_args.env)

        #for node in self.nodes:
        #    LOG.info('\033[1;31mdeploy mirror_cilent no {0}:\033[0m'.format(node))
        #    self.mirror_client(node)

        if parsed_args.master:
            salt_master(self.primary_controller, self.groups)

        if parsed_args.api:
            salt_api(self.primary_controller)

        if parsed_args.minion:
            salt_minion(self.nodes, self.primary_controller)

        if (not parsed_args.master) and (not parsed_args.minion) and (not parsed_args.api):
            salt_master(self.primary_controller, self.groups)
            salt_api(self.primary_controller)
            salt_minion(self.nodes, self.primary_controller)

    def _get_nodedata(self, env):
        astute = '{0}{1}{2}'.format('/etc/fuel/cluster/',env,'/astute.yaml')
        data = utils.parse_yaml(astute)

        self.groups = {}

        for node, metadata in data['network_metadata']['nodes'].items():

            for role in metadata['node_roles']:
                if role in self.groups:
                    self.groups[role].append(node)
                else:
                    self.groups[role] = [node]

            if 'primary-controller' in metadata['node_roles']:
                self.primary_controller = node
                break
        self.nodes = data['network_metadata']['nodes'].keys()
        self.app.stdout.write('\033[1;31msalt-master: {0}\nsalt-minion: {1} \033[0m\n'.format(
                              self.primary_controller,
                              ','.join(self.nodes),))
        self.app.stdout.write('*' * 100 + '\n')

    def mirror_client(self, node):
        cmd1 = 'echo {0} > {1}'.format(settings.deb_client_content, settings.deb_client_listfile)
        cmd2 = 'wget -q -O - {0} | sudo apt-key add -'.format(settings.deb_public_key)
        cmd3 = 'apt-get -y update'
        utils.exec_remote_cmd(cmd1, node)
        utils.exec_remote_cmd(cmd2, node)
        utils.exec_remote_cmd(cmd3, node)


from django import forms
from manager.models import PanelUser, Story, Task

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['name', 'description', 'hours', 'assigned']
        widgets = {
            'description': forms.Textarea(attrs={'rows': 2})
        }

    def __init__(self, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        self.fields['assigned'].queryset = PanelUser.objects.filter(role=PanelUser.ROLES.DEVELOPER)

    def clean_hours(self):
        value = self.cleaned_data.get('hours')
        if value <= 0:
            self.errors['hours'] = ['This must be a positive integer value']
        return value

class TaskStatusForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['status']


# -*- cording: utf-8 -*-
# Created by Kim.youngjae at 2018/12/03

import os
import math
import numpy as np
import cv2
import opencv_knn as knn

# module level variables ##########################################################################

kNearest = cv2.ml.KNearest_create()

# constants for checkIfPossibleChar, this checks one possible char only (does not compare to another char)
MIN_PIXEL_WIDTH = 2
MIN_PIXEL_HEIGHT = 8

MIN_ASPECT_RATIO = 0.25
MAX_ASPECT_RATIO = 1.0

MIN_PIXEL_AREA = 80

# constants for comparing two chars
MIN_DIAG_SIZE_MULTIPLE_AWAY = 0.3
MAX_DIAG_SIZE_MULTIPLE_AWAY = 5.0

MAX_CHANGE_IN_AREA = 0.5

MAX_CHANGE_IN_WIDTH = 0.8
MAX_CHANGE_IN_HEIGHT = 0.2

MAX_ANGLE_BETWEEN_CHARS = 12.0

# other constants
MIN_NUMBER_OF_MATCHING_CHARS = 3

RESIZED_CHAR_IMAGE_WIDTH = 20
RESIZED_CHAR_IMAGE_HEIGHT = 30

MIN_CONTOUR_AREA = 100

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

###################################################################################################
class PossibleChar:

    # constructor #################################################################################
    def __init__(self, _contour):
        self.contour = _contour

        self.boundingRect = cv2.boundingRect(self.contour)

        [intX, intY, intWidth, intHeight] = self.boundingRect

        self.intBoundingRectX = intX
        self.intBoundingRectY = intY
        self.intBoundingRectWidth = intWidth
        self.intBoundingRectHeight = intHeight

        self.intBoundingRectArea = self.intBoundingRectWidth * self.intBoundingRectHeight

        self.intCenterX = (self.intBoundingRectX + self.intBoundingRectX + self.intBoundingRectWidth) / 2
        self.intCenterY = (self.intBoundingRectY + self.intBoundingRectY + self.intBoundingRectHeight) / 2

        self.fltDiagonalSize = math.sqrt((self.intBoundingRectWidth ** 2) + (self.intBoundingRectHeight ** 2))

        self.fltAspectRatio = float(self.intBoundingRectWidth) / float(self.intBoundingRectHeight)
    # end constructor

# end class

###################################################################################################
def checkIfPossibleChar(possible_char):
    # this function is a 'first pass' that does a rough check on a contour to see if it could be a char,
    # note that we are not (yet) comparing the char to other chars to look for a group
    if (possible_char.intBoundingRectArea > MIN_PIXEL_AREA and
            possible_char.intBoundingRectWidth > MIN_PIXEL_WIDTH and possible_char.intBoundingRectHeight > MIN_PIXEL_HEIGHT and
            MIN_ASPECT_RATIO < possible_char.fltAspectRatio and possible_char.fltAspectRatio < MAX_ASPECT_RATIO):
        return True
    else:
        return False
    # end if

# end function

def find_possible_chars_in_box(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_hist = cv2.equalizeHist(img_gray)

    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(10, 10))
    img_clahe = clahe.apply(img_gray)

    # Set threshold and maxValue
    thresh = 130  # 175
    maxValue = 255  # 255
    # apply thresholding which helps for finding lines
    th, img_thresh = cv2.threshold(img_clahe, thresh, maxValue, cv2.THRESH_BINARY_INV)

    kernel = np.ones((3, 3), np.uint8)
    img_dilate = cv2.dilate(img_thresh, kernel, 1)
    _, contours, _ = cv2.findContours(img_dilate,
                                      cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE
                                      )
    # sort the contours by their x-axis position, ensuring
    # that we read the numbers from left to right
    contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])

    # test for draw all find contours
    # cv2.drawContours(img, contours, -1, (255, 0, 0), 3)

    list_possible_chars = []
    num = 0
    saveNpz = False
    FNAME = 'digits.npz'
    train, train_labels = knn.loadtraindata(FNAME)
    for i in contours:
        (x, y, w, h) = cv2.boundingRect(i)
        if (w >= 5 and w <= 20) and (h >= 20 and h <= 40):
            list_possible_chars.append(i)
            img_num = img_thresh[y:y + h, x:x + w]

            train, train_labels = knn.loadtraindata(FNAME)
            saveNpz = False
            test = knn.resize20(img_num)
            result = knn.checkDigit(test, train, train_labels)
            result = int(result[0][0])

            print(result)

            num += 1
            # cv2.imwrite('images/num{}.png'.format(num), img_num)
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)

            cv2.putText(img, str(result), (x, y - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)

            # save training
            # k = cv2.waitKey(0)
            # if k > 47 and k < 58:
            #     saveNpz = True
            #     train = np.append(train, test, axis=0)
            #     newLabel = np.array(int(chr(k))).reshape(-1, 1)
            #     train_labels = np.append(train_labels, newLabel, axis=0)
            #
            # if saveNpz:
            #     np.savez(FNAME, train=train, train_labels=train_labels)

    # cv2.destroyAllWindows()
    if saveNpz:
        np.savez(img_num, train=train, train_labels=train_labels)
    return img

# print(x, y, w, h)
# print(cv2.contourArea(i))

# test
# cv2.imwrite('img.jpg', img)
# cv2.imwrite('img_gray.jpg', img_gray)
# cv2.imwrite('img_hist.jpg', img_hist)
# cv2.imwrite('img_clahe.jpg', img_clahe)
# cv2.imwrite('img_thresh.jpg', img_thresh)
# cv2.imwrite('img_img_dilate.jpg', img_dilate)

# contours = []
#
# _, contours, _ = cv2.findContours(img_thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#
# for contour in contours:
#     possible_char = PossibleChar(contour)
#
#     if checkIfPossibleChar(possible_char):
#         list_possible_chars.append(possible_char)
#
# print(list_possible_chars)
# return list_possible_chars, img_gray, img_thresh

if __name__ == '__main__':
    cwd = os.getcwd()
    file = 'img_meterbox.jpg'
    loc_file = os.path.join(cwd, file)
    img = cv2.imread(loc_file)
    find_possible_chars_in_box(img)
    cv2.imshow("frame", img)

    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


import os
import ntpath
import logging

def make_directory_if_not_exists(dir_path):

    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

    return dir_path

def get_index_from_file_path(path):

    try:
        file_parent_path, file_name = ntpath.split(path)
        base_name, extension = os.path.splitext(file_name)
        index = int(base_name[base_name.rfind("_") + 1:])
    except Exception as e:
        file_name = ""
        index = ""
        print(e)

    return file_name, index

def load_text(filename):

    if os.path.isfile(filename):
        file = open(filename, 'r')
        text = file.read()
        file.close()
    else:
        text = ''

    return text

def save_file(content, filename):

    with open(filename, "wb") as f:
        f.write(content)
        f.close()

    return

def log_print(info_str, file_path, only_print=False):

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        datefmt='%d/%b/%Y %H:%M:%S',
                        filename=file_path
                        )
    # print(info_str)

    if not only_print:
        logging.info(info_str)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Mike
# @Contact : 597290963@qq.com
# @Time    : 2021/1/21 23:47
# @File    : LongestCommonPrefix.py
from typing import List

class LongestCommonPrefix(object):

    def __init__(self):
        pass

    def longestCommonPrefix(self, strs: List[str]) -> str:
        """
        暴力解法：纵向查找，用第一个字符串为基准，依次比较其他字符串相同位置是否相等
        :param strs:
        :return:
        """
        if not strs:
            return ""

        length, counter = len(strs[0]), len(strs)

        for i in range(length):
            s = strs[0][i]
            if any(i == len(strs[j]) or strs[j][i] != s for j in range(1, counter)):
                return strs[0][:i]

        return strs[0]

    def longestCommonPrefix1(self, strs: List[str]) -> str:
        """
        二分查找
        :param strs:
        :return:
        """
        # 最长公共前缀的长度不会超过字符串数组中的最短字符串长度
        if not strs:
            return ""

        def isCommonPrefix1(mid):
            return all(strs[0][:mid] == strs[j][:mid] for j in range(1, len(strs)))

        right = min([len(i) for i in strs])
        left = 0
        while left < right:
            mid = (right - left + 1) // 2 + left  # 偏右
            if isCommonPrefix1(mid):
                left = mid
            else:
                right = mid - 1

        return strs[0][:left]

if __name__ == '__main__':
    print(LongestCommonPrefix().longestCommonPrefix1(["f"]))


from datetime import datetime

from flask_restplus import Resource, fields
from flask import request, current_app
from app.extensions import api

from app.api.mines.mine.models.mine import Mine
from app.api.mines.permits.permit.models.permit import Permit

from app.api.utils.access_decorators import requires_role_view_all

VERIFY_PERMIT_MINE_MODEL = api.model(
    'VerifyPermitMineModel', {
        'a_Result': fields.String,
        'a_MineInfo': fields.String,
        'a_ResponseMessage': fields.String,
        'a_Timestamp': fields.DateTime
    })

class VerifyPermitMineResource(Resource):
    @api.doc(
        description=
        'Verifies that a permit is valid for a mine based on the type of deemed authorization. NOTE: This exists for integration purposes and does not follow the typical patterns of this API.',
        params={
            'a_PermitNumber': f'The permit number.',
            'a_TypeofDeemedAuth': f'The type of deemed authorization, eg INDUCED.'
        })
    @api.marshal_with(VERIFY_PERMIT_MINE_MODEL, code=200)
    @requires_role_view_all
    def get(self):
        result = ""
        mine_info = ""
        response_message = ""

        try:
            permit_no = request.args.get('a_PermitNumber')
            permit_prefix = permit_no.split("-")[0]
            type_of_deemed_auth = request.args.get('a_TypeofDeemedAuth')

            permits = Permit.find_by_permit_no_all(permit_no)

            for permit in permits:
                for mine in permit._all_mines:

                    # Mine must be operating.
                    if not mine.mine_status or mine.mine_status[
                            0].mine_status_xref.mine_operation_status_code != "OP":
                        break

                    # IP SURVEYS (Induced): Valid MMS mine types: 'CX','ES','EU'
                    # There may be need of a check against mine_tenure_type_code IN ["MIN", "COL"] and mine_disturbance_code IN ["SUR", "UND"]
                    # but this data is inconsistant for now.
                    if type_of_deemed_auth == "INDUCED" and permit_prefix not in ["CX", "MX"]:
                        break

                    # DRILL PROGRAM (Drill): Valid MMS mine types: 'CS','CU','MS','MU','IS','IU'
                    if type_of_deemed_auth != "INDUCED" and permit_prefix not in ["C", "M"]:
                        break

                    mine_info = mine_info + mine.mine_no + ' - ' + mine.mine_name + '\r'

            if mine_info != "":
                result = "Success"
            else:
                result = "Failure"
                response_message = "NoValidMinesForPermit"

        except Exception as e:
            current_app.logger.error(str(e))
            result = "Failure"
            mine_info = ""
            response_message = "Unhandled Exception"

        return {
            "a_Result": result,
            "a_MineInfo": mine_info,
            "a_ResponseMessage": response_message,
            "a_Timestamp": datetime.utcnow()
        }


#!/usr/bin/env python

import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name="ondelta",
    version="0.1",
    author="Adam Haney",
    author_email="adam.haney@campusbellhops.com",
    description=("A mixin that allows models to register methods that are notified when their values change, or register a method that is notified of all changes. Basically, OnDeltaMixin implements the observer pattern."),
    license="LGPL",
    keywords="Django, observer",
    url="",
    packages=['ondelta'],
    long_description=read('README.md'),
    dependency_links = [],
    install_requires=[
        'django'
        ],
    classifiers=[]
)


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

import logging
import time
from uuid import uuid4

from zuul.driver import Driver, TriggerInterface
from zuul.driver.zuul.zuulmodel import ZuulTriggerEvent
from zuul.driver.zuul import zuultrigger
from zuul.lib.logutil import get_annotated_logger

PARENT_CHANGE_ENQUEUED = 'parent-change-enqueued'
PROJECT_CHANGE_MERGED = 'project-change-merged'

class ZuulDriver(Driver, TriggerInterface):
    name = 'zuul'
    log = logging.getLogger("zuul.ZuulTrigger")

    def __init__(self):
        self.parent_change_enqueued_events = {}
        self.project_change_merged_events = {}

    def registerScheduler(self, scheduler):
        self.sched = scheduler

    def reconfigure(self, tenant):
        for pipeline in tenant.layout.pipelines.values():
            for ef in pipeline.manager.event_filters:
                if not isinstance(ef.trigger, zuultrigger.ZuulTrigger):
                    continue
                if PARENT_CHANGE_ENQUEUED in ef._types:
                    # parent-change-enqueued events need to be filtered by
                    # pipeline
                    for pipeline in ef._pipelines:
                        key = (tenant.name, pipeline)
                        self.parent_change_enqueued_events[key] = True
                elif PROJECT_CHANGE_MERGED in ef._types:
                    self.project_change_merged_events[tenant.name] = True

    def onChangeMerged(self, tenant, change, source):
        # Called each time zuul merges a change
        if self.project_change_merged_events.get(tenant.name):
            try:
                self._createProjectChangeMergedEvents(change, source)
            except Exception:
                self.log.exception(
                    "Unable to create project-change-merged events for "
                    "%s" % (change,))

    def onChangeEnqueued(self, tenant, change, pipeline, event):
        log = get_annotated_logger(self.log, event)

        # Called each time a change is enqueued in a pipeline
        tenant_events = self.parent_change_enqueued_events.get(
            (tenant.name, pipeline.name))
        log.debug("onChangeEnqueued %s", tenant_events)
        if tenant_events:
            try:
                self._createParentChangeEnqueuedEvents(
                    change, pipeline, tenant, event)
            except Exception:
                log.exception(
                    "Unable to create parent-change-enqueued events for "
                    "%s in %s" % (change, pipeline))

    def _createProjectChangeMergedEvents(self, change, source):
        changes = source.getProjectOpenChanges(
            change.project)
        for open_change in changes:
            self._createProjectChangeMergedEvent(open_change)

    def _createProjectChangeMergedEvent(self, change):
        event = ZuulTriggerEvent()
        event.type = PROJECT_CHANGE_MERGED
        event.trigger_name = self.name
        event.project_hostname = change.project.canonical_hostname
        event.project_name = change.project.name
        event.change_number = change.number
        event.branch = change.branch
        event.change_url = change.url
        event.patch_number = change.patchset
        event.ref = change.ref
        event.zuul_event_id = str(uuid4().hex)
        event.timestamp = time.time()
        self.sched.addEvent(event)

    def _createParentChangeEnqueuedEvents(self, change, pipeline, tenant,
                                          event):
        log = get_annotated_logger(self.log, event)

        log.debug("Checking for changes needing %s:" % change)
        if not hasattr(change, 'needed_by_changes'):
            log.debug("  %s does not support dependencies" % type(change))
            return

        # This is very inefficient, especially on systems with large
        # numbers of github installations.  This can be improved later
        # with persistent storage of dependency information.
        needed_by_changes = set(change.needed_by_changes)
        for source in self.sched.connections.getSources():
            log.debug("  Checking source: %s", source)
            needed_by_changes.update(
                source.getChangesDependingOn(change, None, tenant))
        log.debug("  Following changes: %s", needed_by_changes)

        for needs in needed_by_changes:
            self._createParentChangeEnqueuedEvent(needs, pipeline)

    def _createParentChangeEnqueuedEvent(self, change, pipeline):
        event = ZuulTriggerEvent()
        event.type = PARENT_CHANGE_ENQUEUED
        event.trigger_name = self.name
        event.pipeline_name = pipeline.name
        event.project_hostname = change.project.canonical_hostname
        event.project_name = change.project.name
        event.change_number = change.number
        event.branch = change.branch
        event.change_url = change.url
        event.patch_number = change.patchset
        event.ref = change.ref
        event.zuul_event_id = str(uuid4().hex)
        self.sched.addEvent(event)

    def getTrigger(self, connection_name, config=None):
        return zuultrigger.ZuulTrigger(self, config)

    def getTriggerSchema(self):
        return zuultrigger.getSchema()


# -*- coding: utf-8 -*-
import collections
import math
from settings import *

try:
    from __pypy__ import newlist_hint
except ImportError:
    newlist_hint = lambda size: []

ncounter = 1

def sig(x):
    return x / (1 + abs(x))

class Neuron(object):
    def __init__(self):
        self.b = 0  # p_random_ev()
        self.output = 0
        self.sigmoid = sig(self.output)
        self.tau = 3.0

    def f(self, input_l):
        """
        The function to compute the output of a neuron
        :param input_l: Liste mit tupeln aus Gewicht und input
        :type input_l: list
        :return: None
        """
        input_sum = 0
        for x, w in input_l:
            input_sum += x * w

        self.output = self.output + (1.0/self.tau) * (-self.output + input_sum + self.b)

        self.sigmoid = sig(self.output)

    def set_output(self, output):
        self.output = output
        self.sigmoid = sig(output)

class Network(object):
    def __init__(self):
        """
        Creates the network
        :return: None
        """
        global ncounter
        self.id = ncounter
        ncounter += 1
        self.version = 0
        self.parent = 0
        self.layers = []
        for i in range(len(p_nr)):
            l = []
            for x in range(p_nr[i]):
                l.append(Neuron())
            self.layers.append(l)

        self.weights = collections.OrderedDict()

    def __str__(self):
        return str(self.id) + "_" + str(self.parent) + "_" + str(self.version)

    def compute(self, in_values):
        """
        Berechnet den Output des Netzes
        :param in_values: list of integers with input values
        :type in_values: list
        :return:
        """

        for nr in range(len(self.layers[0])):
            self.layers[0][nr].set_output(in_values[nr])

        for l in range(1, len(self.layers) - 1):

            for neuron in self.layers[l]:
                f_input = []
                for inp in self.layers[l - 1]:
                    f_input.append((inp.sigmoid, self.weights[(inp, neuron)]))
                for inp in self.layers[l]:
                    f_input.append((inp.sigmoid, self.weights[(inp, neuron)]))

                neuron.f(f_input)

        return_list = []

        for neuron in self.layers[-1]:
            f_input = []
            for inp in self.layers[-2]:
                f_input.append((inp.sigmoid, self.weights[(inp, neuron)]))
            neuron.f(f_input)

            return_list.append(neuron.output)

        return return_list


from typing import List

from . import BaseObject

class Client(BaseObject):

    def __init__(self,
                 service_name: str,
                 url: str,
                 _id: str = None,
                 users: List[str] = None):
        super().__init__(_id)
        self.service_name = service_name
        self.url = url
        self.users = users if users else list()


# coding: utf-8
#
# Copyright (c) 2010, Logica
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <ORGANIZATION> nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
# Created on 14 Sep 2010
# @author enmarkp
import re
from datetime import datetime
from genshi.filters.transform import Transformer
from genshi.template.loader import TemplateLoader
from pkg_resources import resource_filename
from trac.util.datefmt import to_utimestamp, utc
from trac.config import ListOption
from trac.core import implements, Component
from trac.web.api import IRequestHandler, ITemplateStreamFilter
from trac.env import IEnvironmentSetupParticipant
from trac.db.schema import Table, Column
from trac.db.api import DatabaseManager
from trac.web.chrome import ITemplateProvider, add_javascript, add_stylesheet, Chrome
from trac.util.translation import _
from trac.admin.api import IAdminPanelProvider

class Feedback(Component):
    realms = ListOption('feedback', 'realms', '*',
                        doc="Show feedback option in these realms")
    implements(ITemplateStreamFilter, IRequestHandler, IAdminPanelProvider,
               IEnvironmentSetupParticipant, ITemplateProvider)

    _schema = [Table('project_feedback', key='id')[
                   Column('id', type='int', auto_increment=True),
                   Column('author'),
                   Column('feedback'),
                   Column('path'),
                   Column('created', type='int64'),
                   Column('modified', type='int64')]]

    # IAdminPanelProvider

    def get_admin_panels(self, req):
        if req.perm.has_permission('TRAC_ADMIN'):
            yield ('feedback', _('Feedback'), 'index', _('Feedback'))

    def render_admin_panel(self, req, category, page, path_info):
        feedback = self._get_feedback_list(req)
        return 'feedback.html', dict(feedback=feedback)

    # ITemplateStreamFilter

    def filter_stream(self, req, method, filename, stream, data):
        if req.authname == 'anonymous':
            return stream
        add_stylesheet(req, 'feedback/feedback.css')
        add_javascript(req, 'feedback/feedback.js')
        Chrome(self.env).add_wiki_toolbars(req)
        tmpl = TemplateLoader(self.get_templates_dirs()).load('feedback-box.html')
        feedbackbox = tmpl.generate(req=req)
        stream |= Transformer('//div[@id="footer"]').append(feedbackbox)
        return stream

    # IRequestHandler
    _url_re = re.compile(r'^/ajax/feedback(?:/(\d+))?/?$')
    def match_request(self, req):
        if req.authname == 'anonymous' or not req.method == 'POST':
            return False
        m = self._url_re.search(req.path_info)
        if m is None:
            return False
        id_ = m.group(1)
        req.args['feedback_id'] = id_ and int(id_) or None
        return True

    def process_request(self, req):
        if req.method == 'POST':
            msg = self._update_feedback(req)
            req.send('{"message":"%s"}' % msg, 'text/json')
        else:
            feedback = self._get_feedback_list(req)
            return 'feedback.html', dict(feedback=feedback), None

    def _get_feedback_list(self, req):
        db = self.env.get_read_db()
        cursor = db.cursor()
        if req.perm.has_permission('TRAC_ADMIN'):
            cursor.execute('SELECT * FROM project_feedback ORDER BY created DESC')
        else:
            cursor.execute('SELECT * FROM project_feedback '
                           'WHERE author=%s ORDER BY created DESC', (req.authname,))
        return list(cursor)

    def _update_feedback(self, req):
        author = req.authname
        action = req.args.get('action', 'create')
        msg = None
        if action == 'create':
            feedback = req.args['feedback']
            path = req.args['path']
            created = modified = to_utimestamp(datetime.now(utc))
            @self.env.with_transaction()
            def add_feedback(db):
                cursor = db.cursor()
                self.log.debug('INSERT INTO project_feedback ('
                               'author, feedback, path, created, modified'
                               ") VALUES ('%s', '%s', '%s', '%s', '%s')",
                               author, feedback, path, created, modified)
                cursor.execute('INSERT INTO project_feedback ('
                               'author, feedback, path, created, modified'
                               ') VALUES (%s, %s, %s, %s, %s)',
                               (author, feedback, path, created, modified))
            msg = _("Your feedback has been received, thank you")
        elif action == 'delete':
            id_ = req.args['feedback_id']
            @self.env.with_transaction()
            def del_feedback(db):
                cursor = db.cursor()
                if req.perm.has_permission('TRAC_ADMIN'):
                    cursor.execute('DELETE FROM project_feedback '
                                   'WHERE id=%s', (id_,))
                else:
                    cursor.execute('DELETE FROM project_feedback '
                                   'WHERE id=%s AND author=%s',
                                   (id_, author))
            msg = _("Feedback item %s was deleted") % id_
        return msg

    # IEnvironmentSetupParticipant

    def environment_created(self):
        @self.env.with_transaction()
        def try_upgrade(db=None):
            self.upgrade_environment(db)

    def environment_needs_upgrade(self, db):
        try:
            @self.env.with_transaction()
            def check(db):
                sql = ('SELECT id, author, feedback, path, created, '
                       'modified FROM project_feedback LIMIT 1')
                cursor = db.cursor()
                cursor.execute(sql)
                cursor.fetchone()
        except Exception:
            self.log.debug("Upgrade of schema needed for feedback plugin", exc_info=True)
            return True
        else:
            return False

    def upgrade_environment(self, db):
        self.log.debug("Upgrading schema for feedback plugin")
        db_backend, _ = DatabaseManager(self.env).get_connector()
        cursor = db.cursor()
        for table in self._schema:
            for stmt in db_backend.to_sql(table):
                self.log.debug(stmt)
                cursor.execute(stmt)

    # ITemplateProvider

    def get_htdocs_dirs(self):
        return [('feedback', resource_filename(__name__, 'htdocs'))]

    def get_templates_dirs(self):
        return [resource_filename(__name__, 'templates')]


import pytest

import threading
from parsl import python_app
from parsl.tests.configs.htex_local import fresh_config
from parsl.executors.errors import SerializationError

local_config = fresh_config()
local_config.retries = 2

@python_app
def fail_pickling(x):
    return True

@pytest.mark.local
def test_serialization_error():

    lock = threading.Lock()
    x = fail_pickling(lock)

    try:
        x.result()
    except Exception as e:
        assert isinstance(e, SerializationError)
    else:
        raise ValueError


from config import PY_ANYWHERE_USERNAME
from github import Github
import pexpect
import time

def create_remote_repo(github_token):
    # accepts the user's github token and uses the pygithub api to create a
    # new remote github repository
    g = Github(github_token)
    user = g.get_user()
    user.create_repo('auto-repo-creation')

def push_code(github_username, github_token):
    # pushes files from the local git repository on the server to the
    # auto-repo-creation remote repository
    my_bash = pexpect.spawn('/bin/bash', cwd='/home/' + PY_ANYWHERE_USERNAME + '/auto-repo-creation')
    my_bash.delaybeforesend = 1
    my_bash.sendline(
        'git remote set-url origin https://github.com/' + github_username +
        '/auto-repo-creation.git')
    my_bash.delaybeforesend = 1
    my_bash.sendline('git add .')
    my_bash.delaybeforesend = 1
    my_bash.sendline("git commit -m 'populate repo'")
    my_bash.delaybeforesend = 1
    my_bash.sendline('git push -u origin master')
    my_bash.expect("Username for 'https://github.com':")
    my_bash.delaybeforesend = 1
    my_bash.sendline(github_username)
    my_bash.expect("Password for 'https://" + github_username + "@github.com':")
    my_bash.delaybeforesend = 1
    my_bash.sendline(github_token)
    time.sleep(10)