import yaml_read_write
import yaml_to_cnf
# Project imports

#
# Environments - {docker|linux|juju}]
#
ENV_DOCKER     = "DOCKER"
ENV_LINUX      = "LINUX"
ENV_JUJU       = "JUJU"
ENV_UNDEFINED  = ""

#
# Docker - constants
#
DOCKER_NODE_STATE_BOOTED      =  1
DOCKER_NODE_STATE_STOPPED     =  0
DOCKER_NODE_STATE_UNDEFINED   = -1
DOCKER_INFO_UNDEFINED         = "?"

#
# scaledb-admin.cnf - sections
#
CONFIG_SA_SECTION_NODE = "nodes"
CONFIG_SA_SECTION_SLM  = "slm"
CONFIG_SA_SECTION_CAS  = "cas"

#
# scaledb-admin.cnf - states
#
CONFIG_SA_INFO_UNDEFINED  = "?"
CONFIG_SA_STATE_PRP       = "PRP"
CONFIG_SA_STATE_READY     = "RDY"
CONFIG_SA_STATE_RUN       = "RUN"
CONFIG_SA_STATE_MNT       = "MNT"

CONFIG_SA_CAS_PRIMARY  = "0"
CONFIG_SA_CAS_MIRROR   = "1"

#
# Host server constants
#
SERVER_STATE_RUUNING        =  1
SERVER_STATE_NOT_RESPONDING =  0
SERVER_STATE_UNDEFINED      = -1

SERVER_TIME_OUT             = 5 # seconds, used with the SSH command - ssh -o ServerAliveInterval=SERVER_TIME_OUT

#
# Node constants
#
NODE_STATE_RUNNING   =  1
NODE_STATE_STOPPED   =  0
NODE_STATE_UNDEFINED = -1

#
# Node Type
#
NODE_TYPE_CAS          = "CAS"
NODE_TYPE_SLM          = "SLM"
NODE_TYPE_DB           = "DB"
NODE_TYPE_UNDEFINED    = "?"

NODE_CAS_TYPE_PRIMARY  = "primary"
NODE_CAS_TYPE_MIRROR   = "mirror"

NODE_SLM_TYPE_ACTIVE   = "active"
NODE_SLM_TYPE_STANDBY  = "standby"

NODE_NOT_APPLICABLE    = -1

#
# Cluster constants
#
CLUSTER_MAX_SIZE = 100  # in number of nodes

#
# LOG severities
#
CRITICAL = 50
ERROR	 = 40
WARNING	 = 30
INFO	 = 20
DEBUG	 = 10
NOTSET	 = 0

logSeverity  = {
    'NOTSET'   : 'NOTSET',
    'DEBUG'    : 'DEBUG',
    'INFO'     : 'INFO',
    'WARNING'  : 'WARNING',
    'ERROR'    : 'ERROR',
    'CRITICAL' : 'CRITICAL',
    }

# YAML to CNF constants
# For list sets (such as casDefaultParam):
#   the left value is the name in the YAML configuration and the right is the corresponding name in *.cnf

yamlFile = "/home/ori/yaml2cnf/yaml-files/ScaleDB-RA_name.yaml"
confDir = "/usr/local/scaledb/etc"
yaml2cnf=yaml_to_cnf.c_convertYAML2ClusterConfig()

slmDefaultParams = [
    ['ip', 'slm_ip'],
    ['server_port', 'slm_port'],
    ['service_port', 'scaledb_slm_service_port'],
    ['slm_debug_file', 'slm_debug_file']
]
slm_debug_file = '/usr/local/scaledb/tmp/slm.log'

casDefaultParams = [
    ['volume', 'cas_id'],
    ['storage', 'cas_type'],
    ['ip', 'cas_ip'],
    ['server_port', 'cas_port'],
    #other ("less" required) parameters
    ['force_system_key','stream_with_user_time_stamp'],
    ['max_file_size','scaledb_max_file_size'],
    ['file_extent_size','scaledb_file_extent_size'],
    ['cas_data_directory', 'scaledb_data_directory'],
    ['cas_log_directory', 'scaledb_log_directory'],
    ['cas_debug_file', 'scaledb_debug_file'],
    ['disable_varchar', 'scaledb_disable_varchar_check'],
    ['async', 'scaledb_streaming_autocommit'],
    ['cas_recovery_mode', 'scaledb_recovery_mode'],

]
cas_data_directory = '/usr/local/scaledb/data'
cas_log_directory = '/usr/local/scaledb/logs'
cas_debug_directory = '/usr/local/scaledb/tmp/cas.log'

dbDefaultParams = [
    ['name', 'scaledb_node_name'],
    ['service_port','scaledb_service_port'],
    ['db_debug_file', 'scaledb_debug_file'],
    ['scaledb_buffer_size_index', 'scaledb_buffer_size_index'],
    ['scaledb_buffer_size_data', 'scaledb_buffer_size_data'],
    ['scaledb_buffer_size_blob', 'scaledb_buffer_size_blob']
]
db_debug_file = '/usr/local/scaledb/tmp/db.log'

# YAML I/O Parameter
yamlReadWrite = yaml_read_write.c_yamlReadWrite()


import sys
#assume that this script is run from "tntech-arc/asimov"
sys.path.append( "network" )   #tntech-arc/asimov/network
sys.path.append( "hokoyu" )    #tntech-arc/asimov/hokoyu
sys.path.append( "messages" )  #tntech-arc/asimov/messages

import client_bsd  #the python client
import Hoky        #the hokoyu hardware driver
import sensors_pb2 #the sensor data
import message_ids #the message identifiers

#queries sensor data and broadcasts to the server
class hokoyu_client:
	#the constructor
	def __init__( self ):
		#Initialize a client_bsd with a public name to use.
		self.client_ = client_bsd.client_bsd( name = 'hokoyu client'  )
		#initialize a LaserScan parsing object
		self.laserscan_ = sensors_pb2.msg_LaserScan()

	#start the hokoyu client and start broadcasting to the server at the given address
	def start( self, address = "127.0.0.1" ):
		#if the client connection doesn't work, exit.
		if self.client_.connect( address ) == False:
			return False
		#if the Hoky driver doesn't start, exit.
		try:
			self.hokoyu_ = Hoky.Hoky()
		except:
			return False
		#feeds LaserScans to the server as fast as the server can take it.
		while self.client_.listen() == True:
			#If the client's buffer is empty, fill it again.
			if self.client_.buffer_size() == 0:
				#clear the parsing object
				self.laserscan_.Clear()
				#the list of polar coordinates
				polar_coords = self.hokoyu_.getPolarCoords()
				#for each pair of values, check validity.
				for i in range( len( polar_coords[0] ) ):
					if polar_coords[0][i] >= 0.0 and polar_coords[0][i] <= 3.14159:
						self.laserscan_.angles.append( polar_coords[0][i] )
						self.laserscan_.ranges.append(   polar_coords[1][i] )
				#write the laserscan to the send buffer
				self.client_.write( message_ids.msg_LaserScan_ID, self.laserscan_.SerializeToString() )
		#close the hardware driver
		self.hokoyu_.close()
		return True

#the entry point
if __name__ == '__main__':
	sensor = hokoyu_client()

	#if there is a command line argument, use it as the address of the server
	if len( sys.argv) < 2:
		sensor.start()
	else:
		sensor.start( address = argv[1] )


"""add_index_on_offer_subcategoryId

Revision ID: a9a2e51e8e24
Revises: f8f80e3ca099
Create Date: 2021-06-30 15:26:01.922044

"""
from alembic import op

from pcapi import settings

# revision identifiers, used by Alembic.
revision = "a9a2e51e8e24"
down_revision = "f8f80e3ca099"
branch_labels = None
depends_on = None

def upgrade():
    op.execute("COMMIT")
    op.execute(
        """
        SET SESSION statement_timeout = '300s'
        """
    )
    op.execute(
        """
        CREATE INDEX CONCURRENTLY IF NOT EXISTS "ix_offer_subcategoryId" ON offer ("subcategoryId")
        """
    )
    op.execute(
        f"""
        SET SESSION statement_timeout={settings.DATABASE_STATEMENT_TIMEOUT}
        """
    )

def downgrade():
    op.execute("COMMIT")
    op.execute(
        """
        DROP INDEX CONCURRENTLY IF EXISTS "ix_offer_subcategoryId"
        """
    )


from email import message
import re

def AutoKeyEncrypt(msg,key):
    index=0;
    cText=''
    for letter in msg:
        num=ord(letter)-65
        newno=(num+(ord(key[index%len(key)])-65))%26
        cText+=chr(newno+65)
        index+=1
    print (cText)
    return cText
def AutoKeyDecrypt(cText,key):
    index=0;
    dText=''
    for letter in cText:
        num=ord(letter)-65
        newno=(num-(ord(key[index%len(key)])-65))%26
        dText+=chr(newno+65)
        index+=1
    print (dText)
    return dText

msg="hello world"
key="jash"
msg=re.sub(r'[^A-Z]','',msg.upper())
key=re.sub(r'[^A-Z]','',key.upper())
keysize=len(key)
msgindex=0
while keysize<len(msg):
    key+=msg[msgindex]
    keysize+=1
    msgindex+=1
print(msg)
print(key)
cText=AutoKeyEncrypt(msg,key)
AutoKeyDecrypt(cText,key)


import os
import sys
import time
import logging
logging.getLogger("PIL.TiffImagePlugin").setLevel(logging.CRITICAL+1)
import spidev as SPI
sys.path.append("..")
sys.path.append("/home/gix/TECHIN514/Shinmood/")
from lib import LCD_1inch28
from PIL import Image,ImageDraw,ImageFont, TiffImagePlugin
TiffImagePlugin.DEBUG=False
Font3 = ImageFont.truetype("/home/gix/TECHIN514/Shinmood/Font/Font02.ttf",32)

# Reading the images into the memory
HAPPY_PATH = '/home/gix/TECHIN514/Shinmood/emoji/Happy 13FTS_Per Secend'
CRY_PATH = '/home/gix/TECHIN514/Shinmood/emoji/Cry 16FTS_Per Second'
LOADED_IMAGES = {
    'happy': [Image.open(os.path.join(HAPPY_PATH, f)) for f in os.listdir(HAPPY_PATH)],
    'cry': [Image.open(os.path.join(CRY_PATH, f)) for f in os.listdir(CRY_PATH)]
}

class LCD():
    def __init__(self, fps):
        # Raspberry Pi pin configuration:
        RST = 6
        DC = 5
        BL = 13
        bus = 0
        device = 0
        self.fps = fps
        self.delay = 1 / fps
        self.timer = 0
        self.arcmax = 135
        self.arcspeed = 270 # arc per second

        logging.basicConfig(level=logging.DEBUG)
        try:
            # display with hardware SPI:
            ''' Warning!!!Don't  creation of multiple displayer objects!!! '''
            self.disp = LCD_1inch28.LCD_1inch28(spi=SPI.SpiDev(bus, device),spi_freq=10000000,rst=RST,dc=DC,bl=BL)
            #self.disp = LCD_1inch28.LCD_1inch28()
            # Initialize library.
            self.disp.Init()
            # Clear display.
            self.disp.clear()
        except IOError as e:
            logging.info(e)
        except KeyboardInterrupt:
            self.disp.module_exit()
            logging.info("quit:")
            exit()

    def show_img(self, mode='happy', index=0, lux: float = 0):
        face = LOADED_IMAGES[mode][index]
        image = Image.new("RGB", (self.disp.width, self.disp.height), "BLACK")
        face_r = face.resize((120,120))
        image.paste(face_r, (60,60))
        # Create blank image for drawing.
        draw = ImageDraw.Draw(image)
        draw.text((74, 150),"%.2f" % lux, fill = "WHITE",font=Font3)
        # draw.text((74, 180),"%.2f" % patting_score, fill = "WHITE",font=Font3)
        image = image.rotate(180)
        draw = ImageDraw.Draw(image)
        # logging.info("draw point")
        # draw.rectangle((20,20,220,220), fill = "blue")
        # logging.info("draw circle")
        draw.arc((10,10,230,230),0, min(self.arcmax, self.delay * self.timer * self.arcspeed), fill =(0,0,255),width=5)
        image = image.rotate(180)
        self.disp.ShowImage(image)
        time.sleep(self.delay)
        self.timer += 1
    def end(self):
        self.disp.module_exit()
        logging.info("quit:")


#!/usr/bin/env python

from icecube import icetray, phys_services, dataio
from icecube import dataclasses as dc
from I3Tray import I3Tray
import numpy as np
import argparse
from usefulFunctions import checkdir

class SaveSnowHeight(icetray.I3Module):
    def __init__(self, context):
        icetray.I3Module.__init__(self, context)
        self.AddOutBox('OutBox')
        self.AddParameter('outFile', 'Input name for output file', 'test')

    def Configure(self):
        self.outFile = self.GetParameter('outFile')
        pass

    def Geometry(self, frame):
    # Load I3Geometry to be used later
        self.geometry = frame['I3Geometry']
        self.PushFrame(frame)

    def DetectorStatus(self, frame):
    # Load I3DetectorStatus to gather positions and snow heights
    # for active tanks
        self.om2index = {}
        self.tankpositions = []
        self.snowheight = []
        self.status = frame['I3DetectorStatus']
        self.dom = self.status.dom_status
        index = 0
        for number, station in self.geometry.stationgeo:
            for tank in station:
                omList = tank.omkey_list
                if not any([om in self.dom.keys() for om in omList]):
                    continue
                for om in omList:
                    self.om2index[om] = index
                self.tankpositions.append(dc.i3position_to_tuple(tank.position))
                self.snowheight.append(tank.snowheight)
                index += 1

        if len(self.tankpositions) < 5: # or some arbitrary low number
            raise ValueError("Not enough tanks found!")

        self.snowheight = np.array(self.snowheight)
        self.PushFrame(frame)

    def Finish(self):
        if self.outFile:
            d = {}
            d['positions']   = self.tankpositions
            d['snowheights'] = self.snowheight
            checkdir(self.outFile)
            np.save(self.outFile, d)
        return

if __name__ == "__main__":

    # Global variables setup for path names
    # my.setupShowerLLH(verbose=False)

    p = argparse.ArgumentParser(
            description='Builds binned histograms for use with ShowerLLH')
    p.add_argument('-f', '--file', dest='file',
            help='Input filelist to run over')
    p.add_argument('-o', '--outFile', dest='outFile',
            help='Output filename')
    args = p.parse_args()

    # Execute
    tray = I3Tray()
    tray.Add('I3Reader', FileName=args.file)
    tray.Add(SaveSnowHeight,
        outFile=args.outFile)
    tray.Execute()
    tray.Finish()


import netCDF4 as nc
import requests
from bs4 import BeautifulSoup
import urllib
import numpy
import pandas as pd

#function to get max at a given latitude
def get_max(data,latitude):
    lat = latitude + 90
    maximum = 0
    for depth in range(len(data)):
        if data[depth][lat]>maximum:
            maximum = data[depth][lat]
        #maximum = max(maximum,data[depth][lat])
        #maximum = max(maximum, data[depth, lat].item
    return maximum

#function to access individual url and convert to usable netcdf object
def access_url(url):
    data = requests.get(url).content
    # Create a NetCDF Dataset object from the in-memory buffer.
    data_nc = nc.Dataset("anynamehere", memory=data)
    return data_nc

#function used to save extracted data into csv
def save_data(data_lists,titles,filename):#format:([x,y,...], [x title, y title, ...], filename)
    data={}
    # Combine the arrays using zip()
    for i in range(len(data_lists)):
        data[titles[i]] = data_lists[i]
    df = pd.DataFrame(data)
    df.to_csv(filename, index=False)
    print("data successfully saved as ", filename)

# URL of the website + preprocessing to find all files links
url = 'https://portal.nccs.nasa.gov/datashare/modelE_ocean/E213SSP245/'
box_url = urllib.request.urlopen(url).read()
box_soup = BeautifulSoup(box_url,'lxml')
folders = box_soup.find('tbody').find_all('a')[1:]
all_amocs = []
all_years = []
titles = ["Years"]
#print(folders)
for ensemble in folders:
    ensemble_name = ensemble.text
    box_url_temp = urllib.request.urlopen(url+ensemble_name).read()
    box_soup_temp = BeautifulSoup(box_url_temp,'lxml')
    #get links to individual files, split by type
    oijl_files=[]
    for element in box_soup_temp.find_all('td', class_='link')[1:][::2]:
        oijl_files.append(element.find('a').get('href'))

    ojl_files=[]
    for element in box_soup_temp.find_all('td', class_='link')[2:][::2]:
        ojl_files.append(element.find('a').get('href'))

    #iterate through list to get data from all files
    years = []
    max_amoc = []
    #pick latitude
    latitude = 48
    for file in ojl_files:
        #get year from file name
        year = file[3:7]
        years.append(year)
        #get complete file path from poral + file name
        complete_url = url + ensemble_name + file
        #print(complete_url)
        data = access_url(complete_url)
        yearly_max = get_max(data.variables["sf_Atl"],latitude)
        #append to results array
        max_amoc.append(yearly_max)

        print("year:",year,"ensemble:", ensemble_name)#checking progress, can be removed
    all_amocs.append(max_amoc)
    titles.append(ensemble_name[:-1])
    if all_years != years:
        print("ERROR")
    all_years = years

# File path for saving the CSV file
filename = 'AMOCvsTIME_lat48AllEnsembles.csv'

# next function used in testing to save data to avoid rerunning extraction
save_data([all_years] + all_amocs,titles,filename)


from __future__ import print_function
import matplotlib as mpl
mpl.use('agg')

import glob
import sys,os,shutil

sys.path.insert(0,'../')
import pyathena.yt_analysis.phase_plots_with_yt as ph
import yt

narg=len(sys.argv)
system='tigress'
base='/tigress/changgoo/'

# first argument to set system
system = sys.argv[1]
if system == 'pleiades':
    base='/u/ckim14/'
elif system =='tigress':
    base='/tigress/changgoo/'
elif system =='perseus':
    base='/perseus/scratch/gpfs/changgoo/'
elif system =='cori':
    base='/global/cscratch1/sd/changgoo/'
else:
    print('{} is not supported'.format(system))
    sys.exit()

# second argument to set problem ids
dirs=glob.glob('{}/{}'.format(base,sys.argv[2]))

# third and fourth arguments to set time range
t1 = eval(sys.argv[3])
t2 = eval(sys.argv[4])

overwrite=False
if narg > 5:
    overwrite=eval(sys.argv[5])

do_pickling = False
ids=[]
for dd in dirs:
    if os.path.isdir(dd):
        if os.path.isdir(dd+'/id0/'):
            ids.append(os.path.basename(dd))
            do_pickling = True
        else:
            do_pickling = False
            if os.path.isdir(dd+'/phase/'):
                ids.append(os.path.basename(dd))

for pid in ids:
    outdir='{}{}/phase/'.format(base,pid)
    if not os.path.isdir(outdir): os.mkdir(outdir)
    print(pid)
    if pid.startswith('R8') or pid.startswith('MHD') or pid.startswith('LGR8'):
        params=ph.phase_parameters(Sigma_factor=1.)
    elif pid.startswith('R4') or pid.startswith('LGR4'):
        params=ph.phase_parameters(Sigma_factor=10.)
        print(params.extrema['nH'])
    elif pid.startswith('R2') or pid.startswith('LGR2'):
        params=ph.phase_parameters(Sigma_factor=100.)
    elif pid.startswith('R16'):
        params=ph.phase_parameters(Sigma_factor=0.1)
    else:
        raise ValueError('need to set scale factor for {}'.format(pid))

    for i in range(t1,t2):
        filename='{}/{}/id0/{}.{:04d}.vtk'.format(base,pid,pid,i)
        if os.path.isfile(filename):
            print('*** Creating joint PDFs for {} ***'.format(os.path.basename(filename)))
            ph.phase_by_slab(filename,zmax=yt.YTQuantity(2048,'pc'),params=params,
                             verbose=60,overwrite=overwrite)
            for bf in params.bin_fields:
                print('*** Drawing for itime={} fields={bf[0]}-{bf[1]}'.format(i,bf=bf))
                for sidx in [[1,2],[8,9]]:
                    fig=ph.plot_joint_PDFs_slab(base,pid,i,bf,slab_index=sidx)
                    pngfname='{}{}/phase/{}.{:04d}-{b[0]}-{b[1]}-slab{s[0]}-{s[1]}.png'
                    pngfname=pngfname.format(base,pid,pid,i,b=bf,s=sidx)
                    fig.savefig(pngfname,bbox_inches='tight',dpi=150)


# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, get_host
from django.template import RequestContext
from django.db.models import Q
from django.http import Http404
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from external.excel_response import ExcelResponse
from photos.models import PhotoSet

from events.models import Event, AnswerList
from events.forms import EventForm, QuestionsForm

def events_widget(request, template_name = "homepage.html"):

    events = Event.objects.filter(approved=True).order_by("-date_begin")

    return render_to_response(template_name, {
        "events": events,
        }, context_instance=RequestContext(request))

@login_required
def map(request, template_name="events/map.html"):
    return render_to_response(template_name)

@login_required
def destroy(request, id):
    """
    Delete event
    """
    event = get_object_or_404(Event, id=id)
    event.delete()

    redirect_to = reverse("events")

    return HttpResponseRedirect(redirect_to)

#@login_required
def details(request, id, template_name="events/details.html"):

    event = get_object_or_404(Event, id=id)

    if event.creator == request.user:
        is_me = True
    else:
        is_me = False

    is_member = event.user_is_member(request.user)

    if (not event.approved) and (not is_me):
        raise Http404
    photoset = get_object_or_404(PhotoSet, pk =   event.gallery.id)

    return render_to_response(template_name, {
    "event": event,
    "is_me": is_me,
    "is_member": is_member,
    "photoset": photoset,
    }, context_instance=RequestContext(request))

@login_required
def add_event(request, form_class=EventForm, template_name="events/add_event.html"):
    """
    Add new event
    """
    event_form = form_class(request.user)

    if request.method == 'POST' and request.POST.get("action") == "add":
        event_form = form_class(request.user, request.POST, request.FILES)

        if event_form.is_valid():
            event = event_form.save(commit=False)
            event.creator = request.user
            photoset = PhotoSet(name = event.title, user = request.user)
            photoset.save()

            event.gallery = photoset
            event.approved = request.user.is_staff
            # automatically approved if user is an administrator
            event.save()
            event.members.add(request.user)
            event.save()
            photoset.content_object = event
            photoset.save()

            if not event.approved:
                request.user.message_set.create(message=_(u"Адміністратор розгляне вашу заявку."))
            if request.POST.get("photoset") == "1":
                redirect_to = reverse('photos.views.editphotoset', kwargs={"id": photoset.pk})
            else:
                redirect_to = reverse("event_details", kwargs={"id": event.id})
            return HttpResponseRedirect(redirect_to)
#        else:
#            event_form = form_class()
    return render_to_response(template_name, {
        "event_form": event_form
    }, context_instance=RequestContext(request))

@login_required
def edit(request, id, form_class=EventForm, template_name="events/edit.html"):
    """
    Edit event
    """
    event = get_object_or_404(Event, id=id)

    event_form = form_class(request.user, instance=event)
    if request.method == "POST" and request.POST.get("action") == "update":
        event_form = form_class(request.user, request.POST, request.FILES, instance=event)
        if event_form.is_valid():
            event = event_form.save()

            include_kwargs = {"id": event.id}
            redirect_to = reverse("event_details", kwargs=include_kwargs)
            return HttpResponseRedirect(redirect_to)

    return render_to_response(template_name, {
        "event_form": event_form,
        "event": event,
    }, context_instance=RequestContext(request))

#@login_required
def events(request, template_name="events/latest.html"):
    """
    Latest ivents
    """
    events = Event.objects.filter(approved=True).order_by("-date_begin")
    photosets = PhotoSet.objects.all()

    photosets = photosets.order_by("-date_added")

    return render_to_response(template_name, {
        "events": events,
        "photosets": photosets,
        }, context_instance=RequestContext(request))

@login_required
def your_events(request, template_name="events/your_events.html"):
    return render_to_response(template_name, {
        "events": Event.objects.filter(creator=request.user),
    }, context_instance=RequestContext(request))

@login_required
def join(request, id, form_class=QuestionsForm, template_name="events/questions.html"):

    event = get_object_or_404(Event, id=id)

    if not event.approved:
        raise Http404

    include_kwargs = {"id": event.id}
    redirect_to = reverse("event_details", kwargs=include_kwargs)

    if event.user_is_member(request.user):
        request.user.message_set.create(message=_(u"Ви вже є учасником цієї події"))
    else:
        if event.questions == "":
            event.members.add(request.user)
            event.save()
        else:
            questions_form = form_class(event, request.user)
            questions_form.setQuestions(event.questions)

            if request.method == "POST":
                questions_form.setData(request.POST)
                if questions_form.is_valid():
                    questions_form.save()

                    event.members.add(request.user)
                    event.save()
                    return HttpResponseRedirect(redirect_to)

            return render_to_response(template_name, {
                "questions_form": questions_form,
                "event": event
            }, context_instance=RequestContext(request))

    return HttpResponseRedirect(redirect_to)

@login_required
def leave(request, id, template_name="events/details.html"):
    event = Event.objects.get(id = id)

    if event.creator != request.user:
        raise Http404

    if event.user_is_member(request.user):
        event.members.remove(request.user)
        try:
            answer_list = AnswerList.objects.get(event=event, user=request.user)
            answer_list.delete()
        except AnswerList.DoesNotExist:
            pass

    include_kwargs = {"id": event.id}
    redirect_to = reverse("event_details", kwargs=include_kwargs)
    return HttpResponseRedirect(redirect_to)

@login_required
def answers(request, id, template_name="events/answers.html"):
    event = get_object_or_404(Event, id=id)

    if event.creator != request.user:
        raise Http404

    if request.GET.get('action') == 'export':
        data = event.get_excel_data()
        return ExcelResponse(data, output_name='answers')

    table = event.get_table_data()

    return render_to_response(template_name, {
        "event": event,
        "table": table,
    }, context_instance=RequestContext(request))

@login_required
def members(request, id, template_name="events/members.html"):
    event = get_object_or_404(Event, id=id)

    if event.creator == request.user:
        is_me = True
    else:
        is_me = False

    if is_me and request.method == "POST" and request.POST.get('action') == "delete":
        try:
            member = User.objects.get(id = request.POST.get('member'))
            if event.user_is_member(member):
                event.members.remove(member)
                try:
                    answer_list = AnswerList.objects.get(event=event, user=member)
                    answer_list.delete()
                except AnswerList.DoesNotExist:
                    pass
                request.user.message_set.create(message=_(u"Учасника %s видалено з події.") % unicode(member.get_profile()))
        except User.DoesNotExist:
            pass

    return render_to_response(template_name, {
        "event": event,
        "is_me": is_me,
    }, context_instance=RequestContext(request))

def photoset(request, id):
    event = get_object_or_404(Event, id=id)
    pk = event.gallery.id
    photoset = get_object_or_404(PhotoSet, pk = pk)
    return render_to_response('events/details.html', {'photoset': photoset}, context_instance = RequestContext(request))


import numpy as np

from tenpy.algorithms import dmrg
from tenpy.networks.mps import MPS
from tenpy.networks import site

from tenpy.models.model import CouplingMPOModel
from tenpy.networks.site import FermionSite, GroupedSite
from tenpy.models import lattice

class TripartiteTriangular(lattice.Lattice):
    def __init__(self, Lx, Ly, siteA, **kwargs):
        basis = np.array(([3., 0.], [0.5, 0.5 * np.sqrt(3)]))
        pos = np.array(([0., 0.], [1., 0.], [2., 0.]))
        kwargs.setdefault('order', 'default')
        kwargs.setdefault('bc', 'periodic')
        kwargs.setdefault('bc_MPS', 'infinite')
        kwargs.setdefault('basis', basis)
        kwargs.setdefault('positions', pos)

        super().__init__([Lx, Ly], [siteA, siteA, siteA], **kwargs)

        self.NN = [(0, 2, np.array([-1, 1])), (0, 1, np.array([0, 0])), (0, 0, np.array([0, -1])),
                   (1, 0, np.array([0, 1])), (1, 2, np.array([0, 0])), (1, 1, np.array([0, -1])),
                   (2, 1, np.array([0, 1])), (2, 0, np.array([1, 0])), (2, 2, np.array([0, -1]))]

        self.nNNA = [(0, 2, np.array([-1, 2])), (0, 2, np.array([0, -1])),
                     (0, 2, np.array([-1, -1])), (1, 0, np.array([0, 2])), (1, 0, np.array([1,
                                                                                -1])),
                     (1, 0, np.array([0, -1])), (2, 1, np.array([0, 2])), (2, 1, np.array([1,
                                                                                -1])),
                     (2, 1, np.array([0, -1]))]

        self.nNNB = [(0, 1, np.array([0, 1])), (0, 1, np.array([-1, 1])), (0, 1, np.array([0,
                                                                                -2])),
                     (1, 2, np.array([0, 1])), (1, 2, np.array([-1, 1])), (1, 2, np.array([0,
                                                                                -2])),
                     (2, 0, np.array([1, 1])), (2, 0, np.array([0, 1])), (2, 0, np.array([1, -2]))]

        self.nnNN = [(0, 1, np.array([-1, 2])), (0, 2, np.array([0, 0])), (0, 0, np.array([0,
                                                                                -2])),
                     (1, 2, np.array([-1, 2])), (1, 0, np.array([1, 0])), (1, 1, np.array([0,
                                                                                -2])),
                     (2, 0, np.array([0, 2])), (2, 1, np.array([1, 0])), (2, 2, np.array([0, -2]))]

class FermionicC3HaldaneModel(CouplingMPOModel):
    def init_sites(self, model_params):
        conserve = model_params.get('conserve', 'N')
        fs = FermionSite(conserve=conserve)
        gs = GroupedSite([fs, fs], labels=['A', 'B'], charges='same')
        gs.add_op('Ntot', gs.NA + gs.NB, False)
        return gs

    def init_lattice(self, model_params):
        Lx = model_params.get('Lx', 1)
        Ly = model_params.get('Ly', 3)
        fs = self.init_sites(model_params)
        lat = TripartiteTriangular(Lx, Ly, fs)
        return lat

    def init_terms(self, model_params):
        t = np.asarray(model_params.get('t', -1.))
        V = np.asarray(model_params.get('V', 0))
        phi_ext = 2 * np.pi * model_params.get('phi_ext', 0.)

        t1 = t
        t2 = 0.39 * t * 1j
        t3 = -0.34 * t

        for u1, u2, dx in self.lat.NN:
            t1_phi = self.coupling_strength_add_ext_flux(t1, dx, [0, phi_ext])
            self.add_coupling(t1_phi, u1, 'CdA', u2, 'CB', dx, 'JW', True)
            self.add_coupling(np.conj(t1_phi), u2, 'CdB', u1, 'CA', -dx, 'JW', True)
            self.add_coupling(V, u1, 'Ntot', u2, 'Ntot', dx)

        for u1, u2, dx in self.lat.nNNA:
            t2_phi = self.coupling_strength_add_ext_flux(t2, dx, [0, phi_ext])
            self.add_coupling(t2_phi, u1, 'CdA', u2, 'CA', dx, 'JW', True)
            self.add_coupling(np.conj(t2_phi), u2, 'CdA', u1, 'CA', -dx, 'JW', True)

        for u1, u2, dx in self.lat.nNNB:
            t2_phi = self.coupling_strength_add_ext_flux(t2, dx, [0, phi_ext])
            self.add_coupling(t2_phi, u1, 'CdB', u2, 'CB', dx, 'JW', True)
            self.add_coupling(np.conj(t2_phi), u2, 'CdB', u1, 'CB', -dx, 'JW', True)

        for u1, u2, dx in self.lat.nnNN:
            t3_phi = self.coupling_strength_add_ext_flux(t3, dx, [0, phi_ext])
            self.add_coupling(t3_phi, u1, 'CdA', u2, 'CB', dx, 'JW', True)
            self.add_coupling(np.conj(t3_phi), u2, 'CdB', u1, 'CA', -dx, 'JW', True)

def plot_lattice():

    import matplotlib.pyplot as plt
    ax = plt.gca()
    fs = site.FermionSite()
    lat = TripartiteTriangular(3, 3, fs)
    lat.plot_sites(ax)
    lat.plot_coupling(ax, lat.NN, linestyle='--', color='green')
    lat.plot_coupling(ax, lat.nNNA, linestyle='--', color='red')
    lat.plot_coupling(ax, lat.nNNB, linestyle='--', color='blue')
    lat.plot_coupling(ax, lat.nnNN, linestyle='--', color='black')
    ax.set_aspect('equal')
    plt.show()

def run(phi_ext=np.linspace(0, 1.0, 7)):

    data = dict(phi_ext=phi_ext, QL=[], ent_spectrum=[])

    model_params = dict(conserve='N', t=-1, V=0, Lx=1, Ly=3, verbose=1)

    dmrg_params = {
        'mixer': True,  # setting this to True helps to escape local minima
        'mixer_params': {
            'amplitude': 1.e-5,
            'decay': 1.2,
            'disable_after': 30
        },
        'trunc_params': {
            'svd_min': 1.e-10,
        },
        'lanczos_params': {
            'N_min': 5,
            'N_max': 20
        },
        'chi_list': {
            0: 9,
            10: 49,
            20: 100
        },
        'max_E_err': 1.e-10,
        'max_S_err': 1.e-6,
        'max_sweeps': 150,
        'verbose': 1.,
    }

    prod_state = ['full_A empty_B', 'empty_A full_B', 'full_A empty_B'
                  ] * (model_params['Lx'] * model_params['Ly'])

    eng = None

    for phi in phi_ext:

        print("=" * 100)
        print("phi_ext = ", phi)

        model_params['phi_ext'] = phi

        if eng is None:  # first time in the loop
            M = FermionicC3HaldaneModel(model_params)
            psi = MPS.from_product_state(M.lat.mps_sites(), prod_state, bc=M.lat.bc_MPS)
            eng = dmrg.TwoSiteDMRGEngine(psi, M, dmrg_params)
        else:
            del eng.options['chi_list']
            M = FermionicC3HaldaneModel(model_params)
            eng.init_env(model=M)

        E, psi = eng.run()

        data['QL'].append(psi.average_charge(bond=0)[0])
        data['ent_spectrum'].append(psi.entanglement_spectrum(by_charge=True)[0])

    return data

def plot_results(data):

    import matplotlib.pyplot as plt

    plt.figure()
    ax = plt.gca()
    ax.plot(data['phi_ext'], data['QL'], marker='o')
    ax.set_xlabel(r"$\Phi_y / 2 \pi$")
    ax.set_ylabel(r"$ \langle Q^L(\Phi_y) \rangle$")
    plt.savefig("haldane_C3_charge_pump.pdf")

    plt.figure()
    ax = plt.gca()
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    color_by_charge = {}
    for phi_ext, spectrum in zip(data['phi_ext'], data['ent_spectrum']):
        for q, s in spectrum:
            q = q[0]
            label = ""
            if q not in color_by_charge:
                label = "{q:d}".format(q=q)
                color_by_charge[q] = colors[len(color_by_charge) % len(colors)]
            color = color_by_charge[q]
            ax.plot(phi_ext * np.ones(s.shape),
                    s,
                    linestyle='',
                    marker='_',
                    color=color,
                    label=label)
    ax.set_xlabel(r"$\Phi_y / 2 \pi$")
    ax.set_ylabel(r"$ \epsilon_\alpha $")
    ax.set_ylim(0., 8.)
    ax.legend(loc='upper right')
    plt.savefig("haldane_C3_ent_spec_flow.pdf")

if __name__ == "__main__":

    # plot_lattice()
    data = run()
    plot_results(data)


# -*- coding = utf-8 -*-
# @Time : 2022/9/10 15:15
# @Author : Juyi
# @File : 感知机.py
# @Software : PyCharm
import numpy as np

# 定义一个接收参数x1和x2的AND函数
def AND(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.7
    tmp = w1 * x1 + w2 * x2
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1
    # 当输入的加权总和超过阈值时返回1，否则返回0。

# 测试如下(相当于实现了与门逻辑)
print(AND(0, 0))
print(AND(1, 0))
print(AND(0, 1))
print(AND(1, 1))

# 考虑将θ换为b，b称为偏置，w1和w2称为权重。
# 感知机会计算输入信号和权重的乘积，然后加上偏置，如果这个值大于0则输出1，否则输出0

x = np.array([0, 1])  # 输入的值x
w = np.array([0.5, 0.5])  # 权重
b = -0.7  # 偏置

print(w * x)
print(np.sum(w * x) + b)  # np.sum(w*x)再计算相乘后的各个元素的总和

# 使用权重和偏置，可以像下面这样实现与门
def AND1(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7  # 把−θ命名为偏置b
    tmp = np.sum(w * x) + b
    if tmp > 0:
        return 1
    else:
        return 0

# 偏置的值决定了神经元被激活的容易程度

# 继续实现与非门和或门
def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])  # 仅权重和偏置与AND不同！
    b = 0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])  # 仅权重和偏置与AND不同！
    b = -0.2
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

# 感知机的局限性就在于它只能表示由一条直线分割的空间 (显然其难以实现异或门)
# 曲线分割而成的空间称为非线性空间，由直线分割而成的空间称为线性空间

# 这样通过三个门组合可以实现异或
def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
    return y
# 异或门是一种多层结构的神经网络


from dataHelper import get_dataset
import datasets
import logging
import torch
import os
import random
from dataclasses import dataclass, field
import numpy as np
import evaluate
import transformers
import wandb
from transformers import (
    AutoConfig,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    PretrainedConfig,
    Trainer,
    TrainingArguments,
    default_data_collator,
    set_seed,
)
from peft import LoraConfig, get_peft_model
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version

torch.cuda.set_per_process_memory_fraction(1.0, 0)
torch.cuda.empty_cache()

'''
settings
'''
lr = 1e-5
epoch_num = 5
batch_size = 16

classes_dic = {'restaurant_sup': 3, 'acl_sup': 6, 'agnews_sup': 4}

'''
Initialize metircs
'''
from datasets import load_metric
metric = load_metric("./metric.py")

# You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
# predictions and label_ids field) and has to return a dictionary string to float.
def compute_metrics(p: EvalPrediction):
    preds = p.predictions
    preds = np.argmax(preds, axis=1)
    result = metric.compute(predictions=preds, references=p.label_ids)
    return result

@dataclass
class Modelargs():
    model_name: str
    add_adaptor: bool

@dataclass
class Dataargs():
    dataset_name: str

logger = logging.getLogger(__name__)

def main():

        os.environ["HF_EVALUATE_OFFLINE"] = "1"

        '''
        initialize logging, seed, argparse...
        '''

        parser = HfArgumentParser((Modelargs, Dataargs, TrainingArguments))
        model_args, dataset_args, training_args = parser.parse_args_into_dataclasses()

        dataset_name = dataset_args.dataset_name
        model_name = model_args.model_name

        print("-----------------------------------------------------------------")
        print("Now we are running:")
        print("Dataset: "+ dataset_name)
        print("Model: " + model_name)
        print("Adaptor: " + str(model_args.add_adaptor))
        print("-----------------------------------------------------------------")

        training_args = TrainingArguments(
            output_dir=".\\results",
            learning_rate=lr,
            per_device_train_batch_size=batch_size,
            per_device_eval_batch_size=batch_size,
            num_train_epochs=epoch_num,
            logging_dir='./logs',            # directory for storing logs
            logging_steps=25,
            evaluation_strategy='steps',
            save_strategy='epoch',
            report_to='wandb',
        )

        logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

        set_seed(training_args.seed)

        '''
            load datasets
        '''

        dataset = get_dataset(dataset_name, "<SEP>")

        '''
            load models
        '''

        config = AutoConfig.from_pretrained(model_name, num_labels=classes_dic[dataset_name])
        peft_config = LoraConfig(
            lora_alpha=16,
            lora_dropout=0.1,
            r=64,
            bias="none",
            task_type="SEQ_CLS",
        )

        tokenizer = AutoTokenizer.from_pretrained(model_name)
        tokenizer.pad_token = tokenizer.eos_token
        model = AutoModelForSequenceClassification.from_pretrained(model_name,config=config)
        if model_args.add_adaptor:
            model =  get_peft_model(model, peft_config)

        '''
            process datasets and build up datacollator
        '''

        def tokenize_dataset(dataset):
            return tokenizer(dataset["text"],truncation=True, padding=True, max_length=16, return_tensors="pt")

        dataset = dataset.map(tokenize_dataset, batched=True)

        data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

        trainer = Trainer(
            model=model,
            args=training_args,
            train_dataset=dataset["train"],
            eval_dataset=dataset["test"],
            # compute_metrics=compute_metrics,
            tokenizer=tokenizer,
            data_collator=data_collator,
            compute_metrics=compute_metrics
        )

        wandb.init(project='Adaptor_v1.0', name=dataset_name+'-'+model_name, config=training_args)
        wandb.watch(model)
        # make the wandb obeserver to log the accuracy, micro_f1, macro_f1
        # wandb.log({'accuracy': trainer.compute_metrics['accuracy']})
        # define the name of the runs\

        trainer.train()

        trainer.evaluate()
        wandb.finish()

if __name__ == "__main__":
    main()


import numpy as np
from scipy.signal import butter, lfilter, sosfiltfilt
import time
import os
from keras.models import load_model
import shutil
from datetime import datetime
import socket

### The Methods for Signal Processing ###
def Downsampling(eegData, downsampleRate):
        num = np.floor(eegData.shape[1] / downsampleRate).astype(int)
        Downsampled = np.zeros((eegData.shape[0],num))
        for i in range(num):
            for j in range(eegData.shape[0]):
                Downsampled[j,i] = np.mean(eegData[j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
        return Downsampled

def Re_referencing(eegData, channelNum, sampleNum):
        after_car = np.zeros((channelNum,sampleNum))
        for i in np.arange(channelNum):
            after_car[i,:] = eegData[i,:] - np.mean(eegData,axis=0)
        return after_car

def butter_bandpass(lowcut, highcut, fs, order=5):
        nyq = 0.5 * fs
        low = lowcut / nyq
        high = highcut / nyq
        sos = butter(order, [low, high], btype='band', output='sos')
        return sos
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
        sos = butter_bandpass(lowcut, highcut, fs, order=order)
        y = sosfiltfilt(sos, data)
        return y

def Epoching(eegData, stims, code, samplingRate, nChannel, epochSampleNum, epochOffset,baseline):
        Time = stims[np.where(stims[:,1] == code),0][0]
        Time = np.floor(np.multiply(Time,samplingRate)).astype(int)
        Time_after = np.add(Time,epochOffset).astype(int)
        Time_base = np.add(Time,baseline).astype(int)
        Num = Time.shape
        Epochs = np.zeros((Num[0], nChannel, epochSampleNum))
        for j in range(Num[0]):
            Epochs[j, :, :] = eegData[:,Time_after[j]:Time_after[j] + epochSampleNum]
            for i in range(nChannel):
                Epochs[j, i, :] = np.subtract(Epochs[j, i, :], np.mean(eegData[i,Time_after[j]:Time_base[j]]))
        return [Epochs,Num[0]]

def DownsamplingOnlineEpoch(Epochs1, Epochs2, Epochs3, Epochs4, Epochs5, Epochs6, downsampleRate):
        num = np.floor(Epochs1.shape[2] / downsampleRate).astype(int)
        Downsampled1 = np.zeros((Epochs1.shape[0],Epochs1.shape[1],num))
        Downsampled2 = np.zeros((Epochs2.shape[0],Epochs2.shape[1],num))
        Downsampled3 = np.zeros((Epochs3.shape[0],Epochs3.shape[1],num))
        Downsampled4 = np.zeros((Epochs4.shape[0],Epochs4.shape[1],num))
        Downsampled5 = np.zeros((Epochs5.shape[0],Epochs5.shape[1],num))
        Downsampled6 = np.zeros((Epochs6.shape[0],Epochs6.shape[1],num))
        for i in range(num):
            for j in range(Epochs1.shape[1]):
                for k in range(Epochs1.shape[0]):
                    Downsampled1[k,j,i] = np.mean(Epochs1[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
                    Downsampled2[k,j,i] = np.mean(Epochs2[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
                    Downsampled3[k,j,i] = np.mean(Epochs3[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
                    Downsampled4[k,j,i] = np.mean(Epochs4[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
                    Downsampled5[k,j,i] = np.mean(Epochs5[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
                    Downsampled6[k,j,i] = np.mean(Epochs6[k,j,i*downsampleRate:(i+1)*downsampleRate],dtype=np.float64)
        return [Downsampled1, Downsampled2, Downsampled3, Downsampled4, Downsampled5, Downsampled6, num]

def main():
        #load cnn model and predict result
        model = load_model('C:/Users/wldk5/WorldSystem/Zero/ZeroModel/ZeroCNN2.h5')
#        global file_exist, file1, file2, channelNum
        eegData_txt = 'WorldSystem/Zero/CNNtemp/eegData.out'
        stims_txt = 'WorldSystem/Zero/CNNtemp/stims.out'
        start_txt = 'WorldSystem/Zero/CNNtemp/start.out'
        moveData_eeg = 'WorldSystem/Zero/Online/Data/txt_files/eegData/'
        moveData_stims = 'WorldSystem/Zero/Online/Data/txt_files/stims/'

        serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serverSock.bind(('', 12240))
        serverSock.listen(0)
        connectionSock, addr = serverSock.accept()
        print('The connection has been confirmed at',str(addr))

        for i in range(0, 12):
            #load text file
            while True:
                if os.path.isfile(start_txt):
                    break
            start_time = time.time()

            while(time.time() - start_time < 25):
                pass

            while True:
                if os.path.isfile(eegData_txt) & os.path.isfile(stims_txt):
                    processing_time = time.time()
                    os.remove(start_txt)
                    eegData = np.loadtxt(eegData_txt, delimiter = ",")
                    stims = np.loadtxt(stims_txt, delimiter = ",")
                    ctime = datetime.today().strftime("%m%d_%H%M%S")
                    moveData_e = moveData_eeg + ctime + 'eegData.out'
                    moveData_s = moveData_stims + ctime + 'stims.out'
                    shutil.move(eegData_txt, moveData_e)
                    shutil.move(stims_txt, moveData_s)
                    break
            print("got process")
            channelNum = 7
            channelIdx = []
            downsampleRate = 4
            samplingFreq = 512
            ### Preprocessing process
            sampleNum = eegData.shape[1]

            #Downsampling
            eegData = Re_referencing(eegData[channelIdx], channelNum, sampleNum)

            #Bandpass Filter
            eegData = butter_bandpass_filter(eegData, 0.5, 10, samplingFreq, 4)

            #Epoching
            epochSampleNum = int(np.floor(0.6 * samplingFreq))
            offset = int(np.floor(0. * samplingFreq))
            baseline = int(np.floor(0.6 * samplingFreq))
            [Epochs1, Num1] = Epoching(eegData, stims, 1, samplingFreq, channelNum, epochSampleNum, offset, baseline)
            [Epochs2, Num2] = Epoching(eegData, stims, 2, samplingFreq, channelNum, epochSampleNum, offset, baseline)
            [Epochs3, Num3] = Epoching(eegData, stims, 3, samplingFreq, channelNum, epochSampleNum, offset, baseline)
            [Epochs4, Num4] = Epoching(eegData, stims, 4, samplingFreq, channelNum, epochSampleNum, offset, baseline)
            [Epochs5, Num5] = Epoching(eegData, stims, 5, samplingFreq, channelNum, epochSampleNum, offset, baseline)
            [Epochs6, Num6] = Epoching(eegData, stims, 6, samplingFreq, channelNum, epochSampleNum, offset, baseline)

            #Downsampling Epochs
            [Epochs1,Epochs2,Epochs3,Epochs4,Epochs5,Epochs6,epochSampleNum] = DownsamplingOnlineEpoch(Epochs1, Epochs2, Epochs3, Epochs4, Epochs5, Epochs6, downsampleRate)
            samplingFreq = samplingFreq/4

            result = np.zeros((1,6))

            Epochs1 = np.reshape(Epochs1, (Num1,1,channelNum,epochSampleNum))
            Epochs2 = np.reshape(Epochs2, (Num2,1,channelNum,epochSampleNum))
            Epochs3 = np.reshape(Epochs3, (Num3,1,channelNum,epochSampleNum))
            Epochs4 = np.reshape(Epochs4, (Num4,1,channelNum,epochSampleNum))
            Epochs5 = np.reshape(Epochs5, (Num5,1,channelNum,epochSampleNum))
            Epochs6 = np.reshape(Epochs6, (Num6,1,channelNum,epochSampleNum))

            a1 = model.predict(Epochs1)
            a2 = model.predict(Epochs2)
            a3 = model.predict(Epochs3)
            a4 = model.predict(Epochs4)
            a5 = model.predict(Epochs5)
            a6 = model.predict(Epochs6)

            result[0,0] = np.sum(a1[:,1])
            result[0,1] = np.sum(a2[:,1])
            result[0,2] = np.sum(a3[:,1])
            result[0,3] = np.sum(a4[:,1])
            result[0,4] = np.sum(a5[:,1])
            result[0,5] = np.sum(a6[:,1])

            answer = np.argmax(result) + 1
#            answer = [answer]

#            np.savetxt(result_txt, answer)
            print("Process time: ", time.time() - processing_time)
            print("Result: ", answer)
            connectionSock.send(str(answer).encode("utf-8"))

if __name__ == "__main__":
    main()


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

#THEME = "mmenu-pelican"
THEME = "simple-animation"

SITENAME = 'Adam Tindale'
SITESUBTITLE = ''
SITEURL = 'http://www.adamtindale.com'

ARTICLE_PATHS = ['blog']
ARTICLE_URL = 'blog/{category}/{slug}/'
ARTICLE_SAVE_AS = 'blog/{category}/{slug}/index.html'

AUTHOR = 'Adam Tindale'
AUTHOR_URL = False
AUTHOR_SAVE_AS = False
AUTHORS_URL = False
AUTHORS_SAVE_AS = False

PDF_GENERATOR = False

PAGE_PATHS = ['pages']
PAGE_URL = '{slug}/'
PAGE_SAVE_AS = '{slug}/index.html'

CATEGORY_SAVE_AS = 'blog/{slug}/index.html'
CATEGORY_URL = 'blog/{slug}/'

ARCHIVES_SAVE_AS = 'blog/archives/index.html'
#FEED_ATOM = 'blog/feeds/'
#FEED_RSS = 'blog/feeds/'

TAG_SAVE_AS = False
TAG_URL = False

INDEX_SAVE_AS = 'blog/index.html'

TIMEZONE = 'America/Toronto'
# http://stackful-dev.com/static-site-jinja-pelican-shared-templates.html

DEFAULT_LANG = 'en'

DEFAULT_DATE_FORMAT = '%A %B %d, %Y'

# Blogroll
# LINKS =  ('Pelican', 'http://docs.notmyidea.org/alexis/pelican/')

DEFAULT_PAGINATION = 5

#PAGINATED_DIRECT_TEMPLATES = (
#    ('index'),
#)

DEFAULT_CATEGORY = 'Uncategorized'
DIRECT_TEMPLATES = ('index', 'archives')

IGNORE_FILES = ['.DS_Store', '.DS_store']

# http://docs.getpelican.com/en/3.3.0/settings.html#using-pagination-patterns
PAGINATION_PATTERNS = (
        (1, '{base_name}/', '{base_name}/index.html'),
        (2, '{base_name}/page/{number}/', '{base_name}/page/{number}/index.html'),
)

MENUITEMS = (
        ('Home', ''),
        ('Bio/CV/Events', 'info'),
        ('Projects', 'projects'),
        ('Research', 'research'),
        ('Code', 'code'),
        )
DISPLAY_PAGES_ON_MENU = False

STATIC_PATHS = [
        'img',
        'js/jquery.github-activity.js',
        'js/jquery.timeago.js',
        'js/jquery.knob.js',
        'js/flocking-all.min.js',
        'js/prism.js',
        'css/prism.css',
        'missing.html',
        'records/record.html',
        'records/simple.html',
        'records/oscillate.html',
        'records/sphere.html',
        'flocking/template.html',
        'flocking/waveshapetest.html',
        ]

EXTRA_PATH_METADATA = {
        'js/jquery.github-activity.js' : {'path': 'theme/js/jquery.github-activity.js'},
        'js/jquery.knob.js' : {'path': 'theme/js/jquery.knob.js'},
        'js/jquery.timeago.js' : {'path': 'theme/js/jquery.timeago.js'},
        'js/prism.js' : {'path': 'theme/js/prism.js'},
        'css/prism.js' : {'path': 'theme/css/prism.css'},
        'js/flocking-all.min.js' : {'path': 'theme/js/flocking-all.min.js'},
        'records/record.html' : {'path': 'record/index.html'},
        'records/simple.html' : {'path': 'simple/index.html'},
        'records/oscillate.html' : {'path': 'oscillate/index.html'},
        'records/sphere.html' : {'path': 'sphere/index.html'},
        }

GITHUB_URL = 'https://github.com/drart/'

SOCIAL = (
    #('twitter', 'http://twitter.com/adamtindale'),
    ('github', 'https://github.com/drart/adamtindale.com'),
    ('email', 'mailto:adam@adamtindale.com'),
    ('stackoverflow', 'http://stackoverflow.com/users/1945902/adam-tindale'),
    ('soundcloud', 'https://soundcloud.com/adamtindale'),
    ('googlescholar', 'http://scholar.google.ca/citations?user=cy820NIAAAAJ&hl=en'),
    )

# theme variables
BLOGTITLE = 'Adam\'s Blog'
BLOGDIR = 'blog'
RECENT_BLOG = True

WORKTITLE = 'Online Pieces'
WORKLINKS = (
        ('Slide (2017)', 'http://www.adamtindale.com/slide'),
        ('OSCILLATE (2015)', 'http://www.adamtindale.com/oscillate'),
        ('SIMPLE (2013)', 'http://www.adamtindale.com/simple'),
        ('RECORD (2011)', 'http://www.adamtindale.com/record'),
        ('Sphere (2010)', 'http://www.adamtindale.com/sphere'),
   )

PLUGIN_PATHS = ['pelican-plugins']
#PLUGINS = ['neighbors', 'code_include', 'pelican_gist', 'gist_directive', 'pelican_git']
PLUGINS = ['neighbors', 'code_include',  'gist_directive']

GIT_CACHE_ENABLED = True

SITEMAP = {
        'format': 'xml',
        'changefreqs': {
            'articles': 'monthly',
            'indexes': 'daily',
            'pages': 'monthly'
            }
        }

LOAD_CONTENT_CACHE = False


import scrapy
import scrapy.spiders

from techradarscraper.items import TechLoader

class TechRadarSpider(scrapy.spiders.Spider):
    name = 'techradar'
    start_urls = ['https://www.thoughtworks.com/radar/a-z']

    def parse(self, response):
        links = response.css('.a-z-links > ul > li.blip.hit > a')
        for link in links:
            title = link.css('::text').extract_first()
            href = link.css('::attr(href)').extract_first()
            url = response.urljoin(href)
            yield scrapy.Request(url, self.parse_tech)

    def parse_tech(self, response):
        return TechLoader(response=response).load_item()

if __name__ == '__main__':
    from scrapy.utils.project import get_project_settings
    settings = get_project_settings()
    settings.setdict({
        'LOG_LEVEL': 'INFO',
        'OUTPUT_FILENAME': '../../out/techradar.json',
        'ITEM_PIPELINES': {
            'techradarscraper.pipelines.JsonWriterPipeline': 300,
        },
    })

    from scrapy.crawler import CrawlerProcess
    process = CrawlerProcess(settings)
    process.crawl(TechRadarSpider)
    process.start()


import torch
from torch import nn
from torch.nn import functional as F
from torchvision import transforms
from model.networks import resnet

def partial_load(pretrained_dict, model, skip_keys=[]):
    model_dict = model.state_dict()

    # 1. filter out unnecessary keys
    filtered_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict and not any([sk in k for sk in skip_keys])}
    skipped_keys = [k for k in pretrained_dict if k not in filtered_dict]

    # 2. overwrite entries in the existing state dict
    model_dict.update(filtered_dict)

    # 3. load the new state dict
    model.load_state_dict(model_dict)

    print('\nSkipped keys: ', skipped_keys)
    print('\nLoading keys: ', filtered_dict.keys())

def load_vince_model(path):
    checkpoint = torch.load(path, map_location={'cuda:0': 'cpu'})
    checkpoint = {k.replace('feature_extractor.module.model.', ''): checkpoint[k] for k in checkpoint if 'feature_extractor' in k}
    return checkpoint

def load_tc_model():
    path = 'tc_checkpoint.pth.tar'
    model_state = torch.load(path, map_location='cpu')['state_dict']

    net = resnet.resnet50()
    net_state = net.state_dict()

    for k in [k for k in model_state.keys() if 'encoderVideo' in k]:
        kk = k.replace('module.encoderVideo.', '')
        tmp = model_state[k]
        if net_state[kk].shape != model_state[k].shape and net_state[kk].dim() == 4 and model_state[k].dim() == 5:
            tmp = model_state[k].squeeze(2)
        net_state[kk][:] = tmp[:]

    net.load_state_dict(net_state)

    return net

def load_uvc_model():
    net = resnet.resnet18()
    net.avgpool, net.fc = None, None

    ckpt = torch.load('uvc_checkpoint.pth.tar', map_location='cpu')
    state_dict = {k.replace('module.gray_encoder.', ''):v for k,v in ckpt['state_dict'].items() if 'gray_encoder' in k}
    net.load_state_dict(state_dict)

    return net

class From3D(nn.Module):
    ''' Use a 2D convnet as a 3D convnet '''
    def __init__(self, resnet):
        super(From3D, self).__init__()
        self.model = resnet

    def forward(self, x):
        B, N, T, C, h, w = x.shape
        xx = x.reshape(-1, C, h, w)
        m = self.model(xx)
        return m #.reshape(B, N, T, *m.shape[-3:])#.permute(0, 2, 1, 3, 4)

def make_encoder(model_type, remove_layers=None):
    if model_type == 'scratch':
        net = resnet.resnet18()
        net.modify(padding='reflect')

    elif model_type == 'scratch_zeropad':
        net = resnet.resnet18()

    elif model_type == 'imagenet18':
        net = resnet.resnet18(pretrained=True)

    elif model_type == 'imagenet50':
        net = resnet.resnet50(pretrained=True)

    elif model_type == 'moco50':
        net = resnet.resnet50(pretrained=False)
        net_ckpt = torch.load('moco_v2_800ep_pretrain.pth.tar')
        net_state = {k.replace('module.encoder_q.', ''):v for k,v in net_ckpt['state_dict'].items() \
                     if 'module.encoder_q' in k}
        partial_load(net_state, net)

    elif model_type == 'timecycle':
        net = load_tc_model()

    elif model_type == 'uvc':
        net = load_uvc_model()

    else:
        assert False, 'invalid model_type'

    if hasattr(net, 'modify') and remove_layers is not None:
        net.modify(remove_layers=remove_layers)
        # parser.add_argument('--remove-layers', default=['layer4'], help='layer[1-4]')

    if 'Conv2d' in str(net):
        net = From3D(net)

    return net

#             # Make spatial radius mask TODO use torch.sparse
#             restrict = utils.MaskedAttention(args.radius, flat=False)
#             D = restrict.mask(*feats.shape[-2:])[None]
#             D = D.flatten(-4, -3).flatten(-2)
#             D[D==0] = -1e10; D[D==1] = 0
#
# class MaskedAttention(nn.Module):
#     '''
#     A module that implements masked attention based on spatial locality
#     TODO implement in a more efficient way (torch sparse or correlation filter)
#     '''
#     def __init__(self, radius, flat=True):
#         super(MaskedAttention, self).__init__()
#         self.radius = radius
#         self.flat = flat
#         self.masks = {}
#         self.index = {}
#
#     def mask(self, H, W):
#         if not ('%s-%s' %(H,W) in self.masks):
#             self.make(H, W)
#         return self.masks['%s-%s' %(H,W)]
#
#     def index(self, H, W):
#         if not ('%s-%s' %(H,W) in self.index):
#             self.make_index(H, W)
#         return self.index['%s-%s' %(H,W)]
#
#     def make(self, H, W):
#         if self.flat:
#             H = int(H**0.5)
#             W = int(W**0.5)
#
#         gx, gy = torch.meshgrid(torch.arange(0, H), torch.arange(0, W))
#         D = ( (gx[None, None, :, :] - gx[:, :, None, None])**2 + (gy[None, None, :, :] - gy[:, :, None, None])**2 ).float() ** 0.5
#         D = (D < self.radius)[None].float()
#
#         if self.flat:
#             D = self.flatten(D)
#         self.masks['%s-%s' %(H,W)] = D
#
#         return D
#
#     def flatten(self, D):
#         return torch.flatten(torch.flatten(D, 1, 2), -2, -1)
#
#     def make_index(self, H, W, pad=False):
#         mask = self.mask(H, W).view(1, -1).byte()
#         idx = torch.arange(0, mask.numel())[mask[0]][None]
#
#         self.index['%s-%s' %(H,W)] = idx
#
#         return idx
#
#     def forward(self, x):
#         H, W = x.shape[-2:]
#         sid = '%s-%s' % (H,W)
#         if sid not in self.masks:
#             self.masks[sid] = self.make(H, W).to(x.device)
#         mask = self.masks[sid]
#
#         return x * mask[0]


import random
HANGMAN_PICS =['''
    +---+
        |
        |
        |
       === ''', '''
     +---+
     o   |
         |
         |
        === ''', '''
     +---+
     o   |
     |   |
         |
        === ''', '''
     +---+
     o   |
    /|   |
         |
        === ''', '''
     +---+
     o   |
    /|\  |
         |
        === ''', '''
     +---+
     o   |
    /|\  |
    /    |
        === ''', '''
     +---+
     o   |
    /|\  |
    / \  |
        === ''']

words = 'aigle singe consitution pays amour visage civilisation chien chat victoire singe continent pause pose ours cochon' \
        'oie tigre lion panthere serpent aigle pigeon singe gorille asticot furet sanglier belier vache chevre poule porc' \
        'motive paresseux sociable bordelique rigoureux autonome equipe solitude affection amour amoureux maladroit ' \
        'continent europe afrique asie amerique antarctique oceanie volcan montagne riviere plaine jungle terrain' \
        'playstation xbox wii zelda warcraft java python sql php javascript html css  '.split()

##Fonction pour que l'ordinateur choississe au hasard un mot dans notre liste
def getRandomWord(wordList):
    ##permet de parcourir notre list en choissisant un mot au hasard
    ##Dans une list, l'index commence à 0
    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]

##Fonction pour faire affichier le pendu ainsi que les lettres correctes et incorrectes
def displayBoard(missedLetters, correctLetters, secretWords):
    print(HANGMAN_PICS[len(missedLetters)])
    print()

    print('Mauvaises lettres :', end='')
    for letter in missedLetters:
        print(letter, end='')
    print()

    blanks = '_' * len(secretWords)
    ##Remplace les tirets par les lettres correctes
    for i in range(len(secretWords)):
        if secretWords[i] in correctLetters:
            blanks = blanks[:i] + secretWords[i] + blanks[i+1:] ##Parcours du debut à la fin le mot secret et verifie à quel endroit ce trouve la lettre exacte. On ajoute +1 aussi car notre boucle commence à l'index 0 et non 1.
    ##on affiche ici les lettres correctes
    for letter in blanks:
        print(letter, end='')
    print()

def getGuess(alreadyGuessed):
    ##Permet au joueurs d'afficher la lettre saisie. La fonction s'assurera qu'il s'agit d'une unique lettre et de rien d'autre
    while True:
        print('Propose une lettre.')
        guess=input()
        guess=guess.lower()
        ##Vérifier la proposition du joueur
        if len(guess) != 1:
            print('Propose une seul lettre à la fois !')
        elif guess in alreadyGuessed:
            print('Tu as déjà demandé cette lettre.')
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print('Propose une lettre de l\'alphabet !')
        else:
            return guess

##Demande au joueur s'il veut rejouer
def playAgain():
    print('Veux-tu rejouer (oui ou non)?')
    return input().lower().startswith('o')

print('P E N D U ')
missedLetters = ''
correctLetters = ''
secretWords = getRandomWord(words)
gameIsDone = False

##Ce boucle nous permet d'appeler la fonction displayBoard() en passant les trois variables pré-configurées et permet de dessiner le pendu en fonction du nombre de lettres fausses ou correctes
while True:
    displayBoard(missedLetters, correctLetters, secretWords)

    ##Inviter le joueur à saisir une lettre
    guess = getGuess(missedLetters + correctLetters)

    ##Vérifier si la lettre est bonne
    if guess in secretWords:
        correctLetters = correctLetters + guess

    ##Verifier si le joueur a gagné
    foundAllLetters = True
    for i in range(len(secretWords)):
        if secretWords[i] not in correctLetters:
            foundAllLetters = False
            break
    if foundAllLetters:
        print('Oui ! Le mot secret est "' + secretWords + ' " ! Tu as gagné !')
        gameIsDone = True
        ##Gérer une mauvaise réponse
    else:
        missedLetters = missedLetters + guess

    ##Vérifier si le joueur à perdu
    if len(missedLetters) == len(HANGMAN_PICS) - 1:
        displayBoard(missedLetters, correctLetters, secretWords)
        print('Tu as épuisé tous tes essais ! \nAprès '
              + str(len(missedLetters))
              + 'mauvaises lettres et '
              + str(len(correctLetters))
              + 'lettres exactes, le mot secret était"'
              + secretWords + '".')
        gameIsDone = True

    ##Arrêter yu recommencer le jeu
    if gameIsDone:
        if playAgain():
            missedLetters=''
            correctLetters=''
            gameIsDone= False
            secretWords = getRandomWord(words)
        else:
            break


import filecmp
import os
import shutil
import sys
import tempfile
import unittest

TESTSCRIPT_DIR = os.path.dirname(__file__)
SCRIPT_DIR = os.path.realpath(os.path.join(TESTSCRIPT_DIR, os.pardir, os.pardir))
sys.path.append(SCRIPT_DIR)
import paypal2homebank # pylint: disable=import-error,wrong-import-position

class Testpaypal2homebank(unittest.TestCase):
    '''overall test'''

    @staticmethod
    def gen_tempfolder():
        '''Returns temporary folder name'''
        return tempfile.mkdtemp(prefix="tmp_paypal2homebank_tests")

    def test_01(self):
        '''Standard test'''

        temp_folder = self.gen_tempfolder()
        in_filepath = os.path.join(TESTSCRIPT_DIR, "paypal.csv")
        out_filepath = os.path.join(temp_folder, "homebank.csv")
        with open(out_filepath, mode="w", encoding="utf-8") as out_file:
            paypal2homebank.paypal2homebank(in_filepath, out_file)

        expected_out_filepath = os.path.join(TESTSCRIPT_DIR, "homebank_expected.csv")
        self.assertTrue(filecmp.cmp(out_filepath, expected_out_filepath, shallow=False))
        shutil.rmtree(temp_folder)

if __name__ == "__main__":
    unittest.main()


import torch
import torch.nn as nn
import numpy as np
import os
import argparse
from unidecode import unidecode

from fastspeech2.model_fs2 import FastSpeech2
from text_fs2 import text_to_sequence, sequence_to_text
import hparams as hp
import utils
from string import punctuation
from G2p import G2p
from time import time
import re
from evaluating_sentence import EvalMeanFrequencyScore

ev = EvalMeanFrequencyScore()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

def preprocess(text, g2p):
    text = text.rstrip(punctuation).lower()
    print(text)
    phone = g2p.g2p(text)
    # phone = list(filter(lambda p: p != ' ', phone))
    # print(phone)

    phone = '{' + '}{'.join(phone.split()) + '}'
    phone = re.sub(r'\{[^\w\s]?\}', '{sp}', phone)
    phone = phone.replace('}{', ' ')
    # print('|' + phone + '|')
    sequence = np.array(text_to_sequence(phone, hp.text_cleaners))
    # print(sequence_to_text(sequence))
    # print(sequence)
    sequence = np.stack([sequence])
    return torch.from_numpy(sequence).long().to(device)

def get_FastSpeech2():
    checkpoint_path = os.path.join(
        hp.restore_path, "checkpoint_{}.pth.tar".format('120000'))
    model = nn.DataParallel(FastSpeech2())
    model.load_state_dict(torch.load(checkpoint_path, map_location=device)['model'])
    model.requires_grad = False
    model.eval()
    return model

def synthesize(model, waveglow, text, idx, prefix='', duration_control=1.0, pitch_control=1.0, energy_control=1.0):
    t = time()
    src_len = torch.from_numpy(np.array([text.shape[1]])).to(device)
    mel, mel_postnet, log_duration_output, f0_output, energy_output, _, _, mel_len = model(
        text, src_len, d_control=duration_control, p_control=pitch_control, e_control=energy_control)

    if device == 'cuda':
        mel_postnet = mel_postnet.transpose(1, 2).detach()
    else:
        mel_postnet = mel_postnet.cpu().transpose(1, 2).detach()

    if not os.path.exists(args.test_path):
        os.makedirs(args.test_path)
    # Audio.tools.inv_mel_spec(mel_postnet, os.path.join(
    #     hp.test_path, '{}_griffin_lim_{}.wav'.format(prefix, name)))
    t1 = time() - t
    if waveglow is not None:
        utils.waveglow_infer(mel_postnet, waveglow, os.path.join(
            args.test_path, '{}_{}_{}.wav'.format(prefix, hp.vocoder, idx)))
    t2 = time() - t
    print('{}: time FS: {} (s) time {}: {}'.format(idx, t1, hp.vocoder, t2 - t1))

def gen_mel(model, text, idx, duration_control=1.0, pitch_control=1.0, energy_control=1.0):
    src_len = torch.from_numpy(np.array([text.shape[1]])).to(device)
    mel, mel_postnet, log_duration_output, f0_output, energy_output, _, _, mel_len = model(
        text, src_len, d_control=duration_control, p_control=pitch_control, e_control=energy_control)
    mel_postnet = mel_postnet[0].cpu().transpose(0, 1).detach()
    mel = mel[0].cpu().transpose(0, 1).detach()
    mel_filename = '{}-mel.npy'.format(idx)
    np.save(os.path.join(args.test_path, mel_filename), mel, allow_pickle=False)
    mel_filename = '{}-mel-post.npy'.format(idx)
    np.save(os.path.join(args.test_path, mel_filename), mel_postnet, allow_pickle=False)

if __name__ == "__main__":
    # Test
    parser = argparse.ArgumentParser()
    parser.add_argument('--test_path', type=str, default='./test/')
    parser.add_argument('--path', type=str, default='test.txt')
    parser.add_argument('--dict_path', type=str, default='syllable_g2p.txt')
    parser.add_argument('--sent', type=str, default='?')
    parser.add_argument('--duration_control', type=float, default=1.0)
    parser.add_argument('--pitch_control', type=float, default=1.0)
    parser.add_argument('--energy_control', type=float, default=1.0)

    args = parser.parse_args()

    g2p = G2p(args.dict_path)

    sentences = []
    if args.sent != '?':
        sentences.append(args.sent)
    else:
        with open(args.path, 'r', encoding='utf-8') as rf:
            lines = rf.read().split('\n')
            for i, line in enumerate(lines):
                sentences.append(line)

    model = get_FastSpeech2().to(device)
    waveglow = utils.get_waveglow()
    # waveglow = None
    with torch.no_grad():
        for idx, sentence in enumerate(sentences):
            score = ev.cal(sentence)
        text = preprocess(sentence, g2p)
        name = str(idx) + '_' + str(score)
        print(text.shape)
        synthesize(model, waveglow, text, name, 'step_{}'.format(
            '120000'), args.duration_control, args.pitch_control, args.energy_control)
        # gen_mel(model, text_fs2, name, args.duration_control, args.pitch_control, args.energy_control)
        print('DONE', idx)

# print(text_to_sequence('{o2_T1 l e2_T1 sp k o1_T3 t ie2_T3 ng ng uoi3_T2 sp n oi_T3}', ['basic_cleaners']))


import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from django.http import HttpResponse,JsonResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
import neo4j
from neo4j import GraphDatabase, RoutingControl, exceptions

uri = 'neo4j+s://b35e138c.databases.neo4j.io'
auth = ("neo4j", 'VGfvQTk0VCkEzne79CGPXTKA_Eykhx0OwudLZUKG7sQ')

sem_zim = ["01","09","10","11","12"]
sem_let = ["02","03","04","05","06","07","08"]

def add_group(driver, ID):
    try:
        driver.execute_query(
        "Create (g:Group {ID: $name}) ",
        name=ID,database_="neo4j",
    )
    except neo4j.exceptions.ConstraintError:
        print(f"grupa {ID} jest juz w bazie danych")

def print_group(driver):
    records, _, _= driver.execute_query(
        "MATCH (g:Group)"
        "RETURN g.ID",
         database_="neo4j", routing_=RoutingControl.READ,
    )
    resoult = []
    for record in records:
        resoult.append(record['g.ID'])

    print(resoult)
    return resoult

def add_prac(driver, ID, name):

    try:
        driver.execute_query(
        "Create (p:Pracownik {ID: $ID, name: $name}) ",
        ID=ID,name=name,database_="neo4j",
    )
    except neo4j.exceptions.ConstraintError:
        print(f"Pracownik {name} jest juz w bazie danych")

def print_prac(driver):
    records, _, _ = driver.execute_query(
        "MATCH (p:Pracownik)"
        "RETURN p",
         database_="neo4j", routing_=RoutingControl.READ,
    )
    resoult = {}
    print(records[0].data())
    for record in records:
        data = record.data()
        resoult[data['p']['ID']] = data['p']['name']
    return resoult

def add_date(driver,all,group):

    for date in all.keys():

        for blok in all[date].keys():
            display = all[date][blok][0]
            full = all[date][blok][1]
            id_prow = display[3].split("[")[0]
            place = display[2]
            form = display[1]
            short = display[0]
            # print(id_prow, place, form, short, full)

            try:
                driver.execute_query(
                """Merge (b:Blok {id_prow: $id_prow, place: $place, date: $date, nr: $blok})

                On Create
                    SET
                    b.id_prow = $id_prow,
                    b.place = $place,
                    b.date = $date,
                    b.nr = $blok,
                    b.grups = [$group],
                    b.form = $form,
                    b.short = $short,
                    b.full = $full

                On match
                    SET b.grups = CASE
                        WHEN NOT $group IN b.grups THEN b.grups + $group
                        ELSE b.grups
               END
                """,
                date=date,group=group,blok=blok,id_prow=id_prow,place=place,form=form,short=short,full=full,database_="neo4j",
                )

            except neo4j.exceptions.ConstraintError:
                print(f"Zajecia dnia: {date} sa juz dodane do bloku {blok}")

            try:
                driver.execute_query(
                """match (b:Blok), (p:Pracownik)
                Where p.ID = b.id_prow
                and b.nr = $blok
                and b.date = $date
                MERGE (p)-[pz:prowadz_zajecia]->(b)
                """,
                date=date,group=group,blok=blok,database_="neo4j",
            )

                driver.execute_query(
                """match (g:Group), (b:Blok)
                Where  g.ID in b.grups
                and b.date = $date
                and b.nr = $blok
                MERGE (g)-[bz:blok_zajec]->(b)""",
                date=date,group=group,blok=blok,database_="neo4j",
            )
            except neo4j.exceptions.ConstraintError:
                print(f"Powiazania sa juz wykonane")
            except e as Exception:
                print(e)

            print(f"Zakonczono dodawanie dnia: {date} do bloku {blok}")

    return ("Zakonczono dodawanie")

class group_name(View):

    def get(self,request, **kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")
        with GraphDatabase.driver(uri,auth=auth) as driver:
            records = print_group(driver)
            driver.close()

        return JsonResponse(records, safe=False)

    def post(self,request,**kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        url = 'https://old.wcy.wat.edu.pl/pl/rozklad'
        response = requests.get(url, verify=False)
        soup = BeautifulSoup(response.text, 'html.parser')
        options = soup.find('select', {"class": "ctools-jump-menu-select form-select"}).find_all('option')
        with GraphDatabase.driver(uri, auth=auth) as driver:
            for option in options:
                try:
                    add_group(driver, option.text.strip())
                except Exception as e:
                    print(e)
                finally:
                    driver.close()
        print("pomyslnie zaaktualizowano nazwy grup")
        return HttpResponse(["pomyslnie zaaktualizowano nazwy grup"])

class prowadzacy(View):

    def get(self,request, **kwargs):

        try:
            key = str(request.read(),encoding='ascii')
            key = key.split("=")[1]
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        with GraphDatabase.driver(uri,auth=auth) as driver:
            records = print_prac(driver)

        return JsonResponse(records, safe=False)

    def post(self,request,**kwargs):

        try:
            key = str(request.read(),encoding='ascii')
            key = key.split("=")[1]
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        options = webdriver.ChromeOptions()
        options.add_argument('headless')
        driver = webdriver.Chrome(options)

        driver.get(f"https://old.wcy.wat.edu.pl/pl/planzajec")
        options = driver.find_element(By.TAG_NAME, "select").find_elements(By.TAG_NAME, "option")

        with GraphDatabase.driver(uri, auth=auth) as driver:
            for option in options:
                try:
                    name = option.get_property("label")
                    try:
                        id = option.get_property("value").split("id=")[1]
                    except IndexError:
                        id = 'brak'
                    add_prac(driver,id,name)

                except Exception as e:
                    print(e)
                finally:
                    driver.close()

        print("pomyslnie zaaktualizowano prowadzacych")
        return HttpResponse(["pomyslnie zaaktualizowano prowadzacych"])

class actuality_stud(View):
    def get(self, request, **kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        try:
            user_group = request.META['HTTP_GRP']
        except KeyError:
            return JsonResponse({"Podaj grupe idioto, group = nazwa_grupy"})

        if user_group != "":
            url = f'https://old.wcy.wat.edu.pl/pl/rozklad?grupa_id={user_group}'
            response = requests.get(url, verify=False)
            soup = BeautifulSoup(response.text, 'html.parser')

            date = soup.find('span', class_="head_info").text

            return JsonResponse(date, safe=False)
        else:
            return HttpResponse("Wpisz nazwe grupy")

class days(View):
    def get(self, request, **kwargs):

        try:
            key = kwargs.get('key')
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")
        url = f'https://old.wcy.wat.edu.pl/pl/rozklad?grupa_id=WCY20IK1S0'
        response = requests.get(url, verify=False)
        soup = BeautifulSoup(response.text, 'html.parser')

        plan = soup.find('div', class_="rozklad_container")
        week = {}
        day_name = 1
        for days in plan.find_all('div', class_="day_v1"):
            dates = []
            for day in days.find_all('div', class_="date"):
                dates.append([day.find('span', class_="date1").text, day.find('span', class_="date2").text])
            week[day_name] = dates
            day_name += 1
        return JsonResponse(week)

class plan_stud(View):
    def get(self,request,**kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        try:
            user_group = request.META['HTTP_GRP']
        except KeyError:
            return JsonResponse({"Podaj grupe idioto, group = nazwa_grupy"})

        confirm = request.META['HTTP_CONFIRM']
        if confirm != "tak":
            return HttpResponse["uzyj get"]

        options = webdriver.ChromeOptions()
        options.add_argument('headless')
        driver = webdriver.Chrome(options)
        driver.implicitly_wait(1)
        resoult = {}

        driver.get(f"https://old.wcy.wat.edu.pl/pl/rozklad?grupa_id={user_group}")
        lessons = driver.find_elements(By.CLASS_NAME, "lesson")
        current_date = driver.find_element(By.CLASS_NAME, "head_info").get_attribute("innerHTML").split("-")[1]

        if current_date in sem_zim:
            sem = sem_zim
        else:
            sem = sem_let

        for index in lessons:
            date = index.find_element(By.CLASS_NAME, "date").get_attribute("innerHTML")

            month = date.split("_")[1]

            if month in sem:
                full_info = index.find_element(By.CLASS_NAME, "info").get_attribute("innerHTML")
                display = index.find_element(By.CLASS_NAME, "name").get_attribute("innerHTML").split("
")
                block = index.find_element(By.CLASS_NAME,"block_id").get_attribute("innerHTML")[-1:]
            else:
                continue

            if date not in resoult:
                resoult[date] = {}

            resoult[date][block] = [display, full_info]
        driver.quit()

        return JsonResponse(resoult)

    def post(self,request,**kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        try:
            user_group = request.META['HTTP_GRP']
        except KeyError:
            return JsonResponse({"Podaj grupe idioto, group = nazwa_grupy"})

        confirm = request.META['HTTP_CONFIRM']
        if confirm != "tak":
            return HttpResponse["uzyj get"]

        options = webdriver.ChromeOptions()
        options.add_argument('headless')
        options.add_argument('--remote-debugging-port=443')

        driver = webdriver.Chrome(options)
        driver.implicitly_wait(1)
        resoult = {}

        driver.get(f"https://old.wcy.wat.edu.pl/pl/rozklad?grupa_id={user_group}")
        lessons = driver.find_elements(By.CLASS_NAME, "lesson")
        current_date = driver.find_element(By.CLASS_NAME, "head_info").get_attribute("innerHTML").split("-")[1]

        if current_date in sem_zim:
            sem = sem_zim
        else:
            sem = sem_let

        for index in lessons:
            date = index.find_element(By.CLASS_NAME, "date").get_attribute("innerHTML")

            month = date.split("_")[1]

            if month in sem:
                full_info = index.find_element(By.CLASS_NAME, "info").get_attribute("innerHTML")
                display = index.find_element(By.CLASS_NAME, "name").get_attribute("innerHTML").split("
")
                block = index.find_element(By.CLASS_NAME,"block_id").get_attribute("innerHTML")[-1:]

            else:
                continue

            if date not in resoult:
                resoult[date] = {}

            resoult[date][block] = [display, full_info]

        driver.quit()

        with GraphDatabase.driver(uri,auth=auth) as bd:
                add_date(bd,resoult,user_group)

        return HttpResponse("zakonczono")

class plan_prow(View):
    def get(self,request,**kwargs):

        try:
            key = request.META['HTTP_KEY']
        except KeyError:
            return JsonResponse({"Podaj klucz"})
        if key!="karzel":
            return HttpResponse("bledny klucz")

        try:
            prow = request.META['HTTP_PROW']
        except KeyError:
            return JsonResponse({"Podaj prowadzacego cepie, prow = nazwa_grupy zazwyczaj 2 pierwsze litery PS BZYKU CHUJ"})

        options = webdriver.ChromeOptions()
        options.add_argument('headless')
        driver = webdriver.Chrome(options)
        driver.implicitly_wait(1)
        resoult = {}

        driver.get(f"https://old.wcy.wat.edu.pl/pl/rozklad?grupa_id={prow}")
        lessons = driver.find_elements(By.CLASS_NAME, "lesson")
        current_date = driver.find_element(By.CLASS_NAME, "head_info").get_attribute("innerHTML").split("-")[1]

        if current_date in sem_zim:
            sem = sem_zim
        else:
            sem = sem_let

        for index in lessons:
            date = index.find_element(By.CLASS_NAME, "date").get_attribute("innerHTML")
            month = date.split("_")[1]

            if month in sem:
                full_info = index.find_element(By.CLASS_NAME, "info").get_attribute("innerHTML")
                display = index.find_element(By.CLASS_NAME, "name").get_attribute("innerHTML").split("
")
                block = index.find_element(By.CLASS_NAME,"block_id").get_attribute("innerHTML")[-1:]
            else:
                continue

            if date not in resoult:
                resoult[date] = {}

            resoult[date][block] = [display, full_info]
        driver.quit()

        return JsonResponse(resoult)

class help(View):
    def get(self,request):

    #     try:
    #         with GraphDatabase.driver(uri, auth) as driver:
    #             driver.execute_query(
    #     "MERGE (a:Person {name: $name}) "
    #     "MERGE (friend:Person {name: $friend_name}) "
    #     "MERGE (a)-[:KNOWS]->(friend)",
    #     name="name", friend_name="friend_name", database_="Plan",
    # )

    #     finally:
    #         driver.close()

        help = """zawsze i wszedzie bzyku jebany bedzie

 DO KAZDEGO ZAPYTANIA GET, POST ITP POTRZEBNY JEST KLUCZ DOSTEPU

 taki klucz to bedzie karzel

 przyklad: response = request.get(url, key="karzel")

 spis wszystkich URL i szybkie objasnienie:

                       <dfn>https://planwat.azurewebsites.net/polls/ </dfn>
 -- to jest strona domyslna dalej bede pisal tylko rozszerzenia

<dfn>/prowadzacy </dfn>                              -- spis wszystkich prowadzacych w formacie:

 {"Ad.P.": "Adamczyk Piotr"}, gdzie kluczem jest id prowadzacego a wartoscia imie i nazwisko

                            Zeby byly polskie znaki musicie przy kazdym odbiorze JSON dopisac:

 with open(wasz_request, encoding="UTF-8):

 Nie, nie moglem tego zrobic tutaj

<dfn>/actuality/stud                          </dfn> -- wypluwa ostatnia aktualizacje planu danej grupy

 "Data aktualizacji:2023-10-25 00:25:35" </p

<dfn>/days                                    </dfn> -- zwraca dni kalendarzowe jakby sie wam nie chcialo kalendarza wrzucac

 {"1": [["25", "IX"], ["02", "X"],...], "2": [...]} zwraca slownik gdzie id to 1,2,3... dni tygodnia od poniedzialku a wartosci to [dzien,miesiac]

<dfn>/plan/stud                              </dfn>  -- zwraca plan grupy zajec, przy GET wymaga podania grupy w formie group=nazwa_grupy i KLUCZA key=karzel

 zwraca slownik gdzie kluczem jest data, ktory zawiera slownik gdzie kluczem jest blok, ktory zawiera przedmiot. Dane o przedmiocie to lista gdzie 0 index to display a 1 to pelne info

 {"dd-mm-rrrr": {"1": [display,full_info ], "2":...}, ...} max jest do 7 bloku </p/

<dfn>/grp                                     </dfn> -- zwraca wszystkie grupy szkoleniowe, prosta lista

 ["CHUJ", "chuj", "ChUj", "Bzyku", "To", "Kurwa"]

<dfn>/plan/prow                               </dfn> -- dziala i zwraca to samo co plan dla studentow

tyle ze trzeba dac id prowadzacego przy GET, prow=id_prow

jeszcze nie ma zapisywania do BD wiec wszystko robcie na requestach

zrobie tak zeby zawsze JSON byly takie same to bez pierdolenia ze sie zmieni

jak skonczycie front to podeslijcie i wstawie gotutaj

formas czytaj juz o testach jednostkowych, dam ci dostep do serwera jak nauczysz sie cos robic

zebys przypadkiem czegos nie rozjebala

                """
        return HttpResponse(help)


# Basic libs
import os
import time
import requests

# Load tokens
from pathlib import Path

# Models
from trending_tweets_app.models import TwitterArtist, MediaTweet, MediaAttachment
from django.core.management.base import CommandError

# Datetime conversion
from datetime import datetime as dt # distinguish btwn datetime and datetime.datetime
from django.utils import timezone
from django.utils.timezone import make_aware

from mangastyleapp.settings import BASE_DIR

# logging
import logging

log = logging.getLogger(__name__)

dt_fmt = "%Y-%m-%dT%H:%M:%S.%fZ"

bearer_token = os.environ.get("BEARER_TOKEN")
mangastylebot_id = os.environ.get("MANGASTYLEBOT_ID")

def bearer_oauth(r):
    r.headers["Authorization"] = "Bearer {}".format(bearer_token)
    return r

def get_request(url):
    response = requests.get(url, auth=bearer_oauth)
    log.warning(f"Remaining Requests: {response.headers.get('x-rate-limit-remaining')}")
    if response.status_code == 200:
        return response.json()
    elif response.status_code == 429:
        reset_time = int(response.headers["x-rate-limit-reset"])
        sleep_time = reset_time - int(time.time()) + 1
        if sleep_time > 0:
            log.warning(
                "Rate limit exceeded. "
                f"Sleeping for {sleep_time} seconds."
            )
            time.sleep(sleep_time)
        return get_request(url)
    elif response.status_code == 503:
        log.warning("Service Unavailable. Sleeping for 3 seconds.")
        time.sleep(3)
        return get_request(url)
    else:
        raise ValueError("API call did not succeed, http response: {}".format(response.json()))

def create_get_following_url(user_id, next_token=None):
    url = ("https://api.twitter.com/2/users/{}/following"
           "?max_results=1000"
           "&user.fields=id,profile_image_url,public_metrics,username"
           .format(user_id))
    if next_token:
        url += "&pagination_token={}".format(next_token)
    return url

def get_who_user_is_following(user_id):
    url = create_get_following_url(user_id)
    json_resp = get_request(url)

    total_json = json_resp['data']
    next_token = json_resp['meta'].get('next_token', None)
    while next_token:
        url = create_get_following_url(user_id, next_token=next_token)
        json_resp = get_request(url)
        total_json += json_resp['data']
        next_token = json_resp['meta'].get('next_token', None)

    return total_json

def create_search_query(usernames: list):
    query = f"(from%3A{usernames[0]}" # `%3A` is url encoding for `:` and `%20` is ` `
    for username in usernames[1:]:
        query += f"%20OR%20from%3A{username}" # ` OR from:{username}`
    query += ")%20-is%3Aretweet%20-is%3Areply%20-is%3Aquote%20has%3Aimages"

    return query

def get_query_length(query):
    return len(query) - query.count('%')*2 # url encodings only count for 1 char

def create_get_search_url(usernames, next_token=None):
    url = ("https://api.twitter.com/2/tweets/search/recent"
           f"?query={create_search_query(usernames)}"
           "&max_results=100"
           "&sort_order=recency"
           "&tweet.fields=id,text,public_metrics,created_at,lang,attachments,possibly_sensitive"
           "&expansions=attachments.media_keys,author_id"
           "&media.fields=media_key,type,url"
           "&user.fields=id,profile_image_url,public_metrics,username")
    if next_token:
        url += "&pagination_token={}".format(next_token)
    return url

def get_recent_media_tweets(usernames):
    # 1. Get initial response
    url = create_get_search_url(usernames)
    json_resp = get_request(url)

    # 2. Initialize total
    total_json = json_resp.copy()

    # 3. Iterate for each page and add its data to total
    next_token = json_resp['meta'].get('next_token', None)
    while next_token:
        url = create_get_search_url(usernames, next_token=next_token)
        json_resp = get_request(url)
        total_json['data'] += json_resp['data']
        total_json['includes']['media'] += json_resp['includes']['media']
        total_json['includes']['users'] += json_resp['includes']['users']
        next_token = json_resp['meta'].get('next_token', None)

    return total_json

def save_artist_data(user_data):
    try:
        artist, created = TwitterArtist.objects.update_or_create(
            user_id=user_data['id'],
            defaults={
                'username': user_data['username'],
                'name': user_data['name'],
                'followers_count': user_data['public_metrics']['followers_count'],
                'profile_image_url': user_data['profile_image_url'],
            }
        )
        return artist
    except Exception as e:
        raise CommandError('Something went wrong at user {}: {}'.format(user_data['username'], e))

def save_tweet_data(tweet_data, artist):
    try:
        created_at = make_aware(dt.strptime(tweet_data['created_at'], dt_fmt))
        tweet, created = MediaTweet.objects.update_or_create(
            tweet_id=tweet_data['id'],
            defaults={
                'text': tweet_data['text'],
                'likes_count': tweet_data['public_metrics']['like_count'],
                'retweets_count': tweet_data['public_metrics']['retweet_count'],
                'created_at': created_at,
                'lang': tweet_data['lang'],
                'possibly_sensitive': tweet_data['possibly_sensitive'],
                'author': artist,
            }
        )
        return tweet
    except Exception as e:
        raise CommandError('Something went wrong for tweet {}: {}'.format(tweet_data['id'], e))

def save_media_data(media_data, tweet):
    try:
        media = MediaAttachment.objects.update_or_create(
            media_id=media_data['media_key'],
            defaults={
                'media_url': media_data['url'],
                'media_type': media_data['type'],
                'parent_tweet': tweet,
                'training_data': False,
            }
        )
        return media
    except Exception as e:
        raise CommandError('Something went wrong for media {}: {}'.format(media_data['url'], e))


import logging

import sys
import os
from dragonfly import Alternative, Sequence, Literal, Grammar, Rule
if os.name == 'nt':
    sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/grammar_parser/src/")
from grammar_parser.cfgparser import CFGParser

FORMAT = '%(asctime)s %(module)s [%(levelname)s] %(message)s'
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger(__name__)

RULES = {}

def flatten(seq):
    l = []
    for elt in seq:
        if type(elt) in [tuple, list]:
            for elt2 in flatten(elt):
                l.append(elt2)
        else:
            l.append(elt)
    return l

def _get_dragonfly_rule_element(target, parser, depth=0):
    global RULES
    if target not in parser.rules:
        raise Exception("Target {} not in parser rules".format(target))

    # If already present in RULES, return it
    if target in RULES:
        return RULES[target]

    # Get the rule
    rule = parser.rules[target]

    # Iterate over all options
    option_alternative_list = []
    for opt in rule.options:

        # Iterate over all conjunctions
        conjunctions_list = []
        for conj in opt.conjuncts:
            # If the conjunction is already present
            if conj.name in RULES:
                conjunctions_list.append(RULES[conj.name])
                continue

            # If variable: go one level deeper
            if conj.is_variable:
                result = _get_dragonfly_rule_element(conj.name, parser, depth + 1)
                if result:
                    conjunctions_list.append(result)
            else:
                # Add a new literal to the list
                RULES[conj.name] = Literal(conj.name)
                conjunctions_list.append(RULES[conj.name])
                logger.debug("Adding literal rule: %s",  conj.name)

        # ToDo: apply caching?
        if len(conjunctions_list) == 1:
            option_alternative_list.append(conjunctions_list[0])
        else:
            option_alternative_list.append(Sequence(conjunctions_list))

    if len(option_alternative_list) == 1:
        RULES[target] = option_alternative_list[0]
    else:
        RULES[target] = Alternative(option_alternative_list)

    logger.debug("Adding alternative rule: %s", target)
    return RULES[target]

def get_dragonfly_grammar(engine, grammar, target, result_queue):
    global RULES
    RULES = {}

    parser = CFGParser.fromstring(grammar)
    dragonfly_rule_element = _get_dragonfly_rule_element(target, parser)

    dragonfly_grammar = Grammar("G", engine=engine)
    with result_queue.mutex:
        result_queue.queue.clear()

    class GrammarRule(Rule):
        def process_recognition(self, node):
            logger.info('Dragonfly node: %s', str(node))
            result = node.value()
            logger.info('Dragonfly result: %s', str(result))

            flattened_string = result
            if isinstance(result, list):
                flattened_string = " ".join(flatten(result))

            logger.info('Dragonfly flattened result: %s', str(flattened_string))

            if not result_queue.empty():
                logger.warning('There is already a message in the queue! %s', result_queue)
            result_queue.put_nowait(flattened_string)

            logger.debug("Dragonfly thread recognition Q [id=%s, qsize=%d]", id(result_queue), result_queue.qsize())

    rule = GrammarRule(element=dragonfly_rule_element, exported=True)
    dragonfly_grammar.add_rule(rule)

    return dragonfly_grammar


# Shao Depeng 20150813 v0.1
# c_dshao@qti.qualcomm.com

#! /usr/bin/env python
import sys
import getopt

in_file = ""
out_file = ""
split_num = 10

def Usage():
#	print("%s -i input -o output -n split_num" %sys.argv[0])
	print("%s -i input -n split_num" %sys.argv[0])
	print("via '%s -h' to get help" %sys.argv[0])

def ParseParms():
    global in_file
    global out_file
    global split_num

    #opts, args = getopt.getopt(sys.argv[1:], "hi:o:n:")
    opts, args = getopt.getopt(sys.argv[1:], "hi:n:")
    for op, value in opts:
        if (op == "-i"):
            in_file = value
#       elif (op == "-o"):
#            out_file = value
        elif (op == "-n"):
            split_num = int(value)
        elif (op == "-h"):
            Usage()
            sys.exit()

    if (in_file == ""):
        print ("input file is null")
        Usage()
        sys.exit()
#    elif (out_file == ""):
#        print ("output file is null")
        Usage()
        sys.exit()
    else:
#       print ("input:%s, output:%s, split_num:%d" %(in_file, out_file, split_num))
        print ("input:%s, split_num:%d" %(in_file, split_num))

def LineCount(file_object):
    count = 0
    while True:
        buf = file_object.read(8192*1024)
        if (not buf):
            break
        count += buf.count('\n')

    return count

def Main():
    global in_file
    global out_file
    global split_num
    loop = 0
    ref_line = 0

    ParseParms()

    in_object = open(in_file)

    in_count = LineCount(in_object)

    ecah_count = int(in_count/split_num)

    print("in_count=%d, each_count=%d" %(in_count, ecah_count))

    in_object.seek(0)

    out_file = in_file + "_split_" + "0"
    out_object = open(out_file, 'w')

#    while (loop <= split_num):
    while (True):
        line = in_object.readline()
        if (not line):
            print("Read EOF")
            break

        out_object.write(line)
        ref_line += 1
        if (ref_line > ecah_count * loop):
            out_object.close()
            loop += 1
            index = '%d' %loop
            out_file = in_file + "_split_" + index
            out_object = open(out_file, 'w')
            print("another file, filename:%s" %out_file)

    out_object.close()
    in_object.close()
    print("Split Finished!")

if __name__ == "__main__":
    Main()