import threading
import json
import os
import sys
import signal
import time
import pycdp
import traceback
from config_helper import *

class CDPInterface(threading.Thread):
    def __init__(self):
        super(CDPInterface, self).__init__()
        cdp_config = self.get_cdp_config()
        self._term_event = threading.Event()

        self.daemon = True # so we can kill it with CTRL-C
        # Create the CDP object and a registry entry for a REDIS transport
        self.cdp = pycdp.pycdp(cdp_config, log_level=pycdp.log_debug, log_id="test")
        self.in_channels = []

    def publish(self, out_channel, json_obj):
        json_str = json.dumps(json_obj)
        self.cdp.pub(out_channel, json_str)

    def subscribe(self,channels_list):
        for chan in channels_list:
            self.cdp.sub(chan, self.sub_handler)

    def set(self, key, json_obj):
        json_str = json.dumps(json_obj)
        self.cdp.set(key, json_str)

    def get(self, key):
        (status, m) = self.cdp.get(key)
        if status != 200:
            return None

        try:
            json_obj = json.loads(m)
            return(json_obj)
        except:
            print ("INFO: CDP Error Get() exception:", traceback.format_exc())
        return None

    def exists(self, key):
        (s, m) = self.cdp.get("/exists?key=%s" % (key))
        if s != 200:
            print("INFO: CDP query exists not successful--code: [%d]  key: %s" % (s, key))
            return False
        else:
            #print >>sys.stderr, "CDP: query exists code: [%d],  key: %s, message %s" % (s, key, m)
            if int(m) == 1:
                return True
            return False

    def sets(self, key, strval):
        self.cdp.set(key, strval)

    def gets(self, key):
        (s, m) = self.cdp.get(key)
        return m

    def keys(self, key_pattern):
        ## Returns of matching keys
        (s,m) = self.cdp.get("/keys?pattern=%s" %(key_pattern))
        key_list = json.loads(m)
        return key_list

    def append_in_channel(self, channel):
        self.in_channels.append(channel)
        print("INFO: Subscription to %s was added" % channel)
        s = self.cdp.sub(channel, self.sub_handler)
        print("INFO: Channel subsrciption code ", s)

    def add_subscription(self, channel):
        self.append_in_channel(channel)

    def cancel_cdp(self):
        self._term_event.set()

    def cdp_is_terminated(self):
        return self._term_event.is_set()

    def sub_handler(self, source, channel, raw_message):
        try:
            # Message accidently encodes the b' as a byte string, strip it
            msg = raw_message[2:-1]
            json_obj = json.loads(msg)  # need better error handling
        except:
            print("ERROR: On channel %s json.loads %s" % (channel, traceback.format_exc()))
            return

        try:
            print("DEBUG", channel, json_obj)
            # Notice Calls the derived class version of work
            self.work(channel, json_obj)
        except:
            print("DEBUG: Exception calling work", traceback.format_exc())

    def run(self):
        # This gets called by a call to threads start routine
        while not self.cdp_is_terminated():
            time.sleep(0.01)  # be nice to the system :)
        print("INFO: run thread terminated")

    def external_cdp_config(self):
        # check Environment variable CDP_CONFIG first
        # if not exist, try to load config.json file
        # if not successful, return None for get_cdp_config() processing
        cdp_config = os.getenv('CDP_CONFIG', None)
        if cdp_config == None:
            try:
                cdp_config_path = os.getenv("CDP_CONFIG_PATH", 'future_config.json')
                #TODOLIST: need to be able to handle future_config
                cdp_config_path = get_config_path(cdp_config_path)
                with open (cdp_config_path) as cdp_config_file:
                    cdp_config = json.load(cdp_config_file)
            except:
                print("INFO: could not load CDP_CONFIG from %s, use default cdp_config." % cdp_config_path)
                return None
        return cdp_config

    def get_cdp_config(self):
        cdp_config = self.external_cdp_config()
        if cdp_config == None:
            cdp_config = json.dumps({
                  "info":{
                    "file_version":1.0
                  },
                  "config":{
                    "plane1":{
                    "transport_addr":"redis://edgeservices_cdp_redis"
                    },
                    "plane2":{
                    "transport_addr":"mqtt-tcp://edgeservices_cdp_mqtt"
                    }
                  },
                  "mapping":[
                    ["plane1",12, ".*", ""],
                    ["plane2",3, ".*", ""]
                  ]
            })
        return cdp_config


import bpy
from . util import GetAssetType

class UE4_PT_Panel(bpy.types.Panel):
    bl_idname = "UE4_PT_Panel"
    bl_label = "UE4 Tools"
    bl_category = "UE4 Tools"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        row = layout.row()
        row.operator('ue4tools.set_origin_to_base', text="Set Origin", icon='EMPTY_AXIS')
        row = layout.row()
        row.scale_y = 1.5
        row.operator('ue4tools.export_armature', text="Export Armature", icon='EXPORT')
        row = layout.row()
        row.scale_y = 1.5
        row.operator('ue4tools.export_mesh', text="Export Mesh", icon='EXPORT')


def solution(answers):
    answer = {i:0 for i in range(1,4)}
    result = []

    supoza1 = [1,2,3,4,5]
    supoza2 = [2,1,2,3,2,4,2,5]
    supoza3 = [3,3,1,1,2,2,4,4,5,5]

    for i in range(len(answers)) :
        if answers[i] == supoza1[i%len(supoza1)] :
            answer[1]+=1
        if answers[i] == supoza2[i%len(supoza2)] :
            answer[2]+=1
        if answers[i] == supoza3[i%len(supoza3)] :
            answer[3]+=1

    for person,score in answer.items() :
        if score == max(answer.values()) :
            result.append(person)

    return result # 가장 높은 점수를 받은 사람이 여럿일 경우, 오름차순 정렬

# answers = [1,2,3,4,5]
answers = [1,3,2,4,2]

print(solution(answers))


import streamlit as st
from skyfield.api import load, wgs84, EarthSatellite
import satellite_utils as st_utils
from datetime import (datetime as dt, timedelta)
from pytz import timezone
import requests
import constellation_configs as cc
import plotly.graph_objects as go
import pandas as pd
import numpy as np

DT_FORMAT = '%b %d, %Y %H:%M:%S'

def compare_sats(sat1df, sat2df):

    def unit_vector(vec):
        return vec / np.linalg.norm(vec)

    def r_transform(state):
        if len(state) == 6:
            # Inertial to RIC transformation matrix
            r_vec, v_vec = state[0:3], state[-3:]
            h_vec = np.cross(r_vec, v_vec)
            r_hat = unit_vector(r_vec)
            c_hat = unit_vector(h_vec)
            i_hat = np.cross(c_hat, r_hat)
            return np.array([r_hat, i_hat, c_hat])
        else:
            raise Exception("Cannot do RIC transform with len(state) is not 6!")

    def get_ric_vectors():
        r_list, i_list, c_list = [], [], []
        for (idxRow, s1), (_, s2) in zip(sat1df.iterrows(), sat2df.iterrows()):
                state1 = np.array([s1['x'], s1['y'], s1['z'], s1['vx'], s1['vy'], s1['vz']])
                state2 = np.array([s2['x'], s2['y'], s2['z'], s2['vx'], s2['vy'], s2['vz']])
                diff =  state2 - state1
                delta_ric = np.dot(r_transform(state1), diff[:3])
                r_list.append(delta_ric[0])
                i_list.append(delta_ric[1])
                c_list.append(delta_ric[2])
        return r_list, i_list, c_list

    # # Get RIC state difference w.r.t to primary (model-1) object
    r_list, i_list, c_list = get_ric_vectors()
    st1df['r_miss'], st1df['i_miss'], st1df['c_miss'] = r_list, i_list, c_list
    miss_mags = [np.linalg.norm(np.array([r_miss, i_miss, c_miss])) for r_miss, i_miss, c_miss in zip(r_list, i_list, c_list)]
    st1df['miss_mags'] = miss_mags
    subtitle_str = f"
{satObject2.satrec_object.name} ({satObject2.satrec_object.model.satnum}) w.r.t {satObject1.satrec_object.name} ({satObject1.satrec_object.model.satnum})"

    st.info(f"The RIC differences are with respect to the primary satellite: {option1}", icon="ℹ")
    tab1, tab2 = st.tabs(["Miss Distances (RIC)", "Planar Projections (RIC)"])

    with tab1:
        # # Plot RIC - 3D
        fig_3d_ric = go.Figure()
        fig_3d_ric.add_trace(go.Scatter3d(x = st1df['r_miss'], y = st1df['i_miss'], z = st1df['c_miss'], mode="lines"))
        scene_dict = {'xaxis': {'title': 'Radial (km)'},
                        'yaxis': {'title': 'In-track (km)'},
                        'zaxis': {'title': 'Cross-track (km)'}}
        margin_dict = {'l': 0, 'r': 0, 'b': 0, 't': 0}
        camera_dict = dict(
            up=dict(x=0, y=0, z=1),
            center=dict(x=0, y=0, z=0),
            eye=dict(x=-1.25, y=-1.25, z=1.25))
        fig_3d_ric_title = f"Overall Miss Distance {subtitle_str}"
        fig_3d_ric_layout = go.Layout(title_text = fig_3d_ric_title,
                                        margin=margin_dict, scene=scene_dict, scene_camera=camera_dict)
        fig_3d_ric.update_layout(fig_3d_ric_layout)
        st.plotly_chart(fig_3d_ric, theme="streamlit")

        # # Plot RIC miss magnitude
        fig_miss_mag = go.Figure()
        fig_miss_mag.add_trace(go.Scatter(x = st1df['epoch'], y = st1df['miss_mags'], name='Overall Miss (km)', mode="lines"))
        fig_miss_mag.update_layout(title_text = f"Overall Miss Distance {subtitle_str}", xaxis = {'title': 'Date'}, yaxis = {'title': 'Overall Miss (km)'}, hovermode = 'x unified')
        st.plotly_chart(fig_miss_mag, theme="streamlit")

        # # Plot RCI state differences
        fig_ric = go.Figure()
        fig_ric.add_trace(go.Scatter(x = st1df['epoch'], y = st1df['r_miss'], name='Radial Miss (km)', mode='lines', yaxis="y1", hovertemplate = '%{y:.2f}'))
        fig_ric.add_trace(go.Scatter(x = st1df['epoch'], y = st1df['i_miss'], name='In-track Miss (km)', mode='lines', yaxis="y2", hovertemplate = '%{y:.2f}'))
        fig_ric.add_trace(go.Scatter(x = st1df['epoch'], y = st1df['c_miss'], name='Cross-track Miss (km)', mode='lines', yaxis="y3", hovertemplate = '%{y:.2f}'))
        fig_ric.update_layout(
            # xaxis = {'title': 'Date', 'domain': [0.25, 0.9], 'tickformat' : '%Y-%m-%dT%H:%M:%SZ'},
            xaxis = {'title': 'Date', 'tickformat' : '%Y-%m-%dT%H:%M:%SZ'},
            yaxis1  = {'title': {'text': 'Radial Miss (km)', 'standoff': 1}, 'side': 'left', 'autoshift': True, 'anchor': 'free', 'position': 0.1},
            yaxis2 = {'title': {'text': 'In-track Miss (km)', 'standoff': 1}, 'side': 'left', 'overlaying': 'y1', 'autoshift': True, 'position': 0.2},
            yaxis3 = {'title': {'text': 'Cross-track Miss (km)', 'standoff': 15} , 'side': 'right', 'overlaying': 'y1', 'autoshift': True, 'position': 0.9},
            hovermode = 'x unified',
            legend = {'orientation' : 'h', 'xanchor': 'right', 'x': 1, 'yanchor': 'bottom', 'y': -1},
            title_text=f"RIC State Difference {subtitle_str}")
        st.plotly_chart(fig_ric, theme="streamlit")

    with tab2:
        # # 2. Plot In-Track-Cross-Track Plane
        fig_r_i = go.Figure()
        fig_r_i.add_trace(go.Scatter(x = st1df['i_miss'], y = st1df['c_miss'], name='R', mode="lines"))
        # fig_r_i.add_vline(x=5000, line_width=1, line_dash="dash", line_color="red")
        fig_r_i.update_layout(title_text = f"In-Track-Cross-Track Plane {subtitle_str}", xaxis = {'title': 'In-track (km)'}, yaxis = {'title': 'Cross-track (km)'}, hovermode = 'x unified')
        st.plotly_chart(fig_r_i, theme="streamlit")
        # # 3. Plot Radial-Cross-Track Plane
        fig_r_i = go.Figure()
        fig_r_i.add_trace(go.Scatter(x = st1df['r_miss'], y = st1df['c_miss'], name='R', mode="lines"))
        # fig_r_i.add_vline(x=5000, line_width=1, line_dash="dash", line_color="red")
        fig_r_i.update_layout(title_text = f"Radial-Cross-Track Plane {subtitle_str}", xaxis = {'title': 'Radial (km)'}, yaxis = {'title': 'Cross-track (km)'}, hovermode = 'x unified')
        st.plotly_chart(fig_r_i, theme="streamlit")
        # # 4. Plot In-Track-Radial Plane
        fig_r_i = go.Figure()
        fig_r_i.add_trace(go.Scatter(x = st1df['i_miss'], y = st1df['r_miss'], name='R', mode="lines"))
        # fig_r_i.add_vline(x=5000, line_width=1, line_dash="dash", line_color="red")
        fig_r_i.update_layout(title_text = f"In-Track-Radial Plane {subtitle_str}", xaxis = {'title': 'In-track (km)'}, yaxis = {'title': 'Radial (km)'}, hovermode = 'x unified')
        st.plotly_chart(fig_r_i, theme="streamlit")

# UI Elements
# ------------------------- Sidebar panel
# Get user input:
st.sidebar.write('Begin here 👇')

# 1. Select a group of satellites
@st.cache_resource(ttl=21600)
def query_sats(sat_group):
    _URL = 'http://celestrak.com/NORAD/elements/'
    url = f'{_URL}{cc.TLE_GROUP_URL[sat_group]}.txt'
    return load.tle_file(url)

def get_satellites():
    satellite_group_type = st.sidebar.selectbox('Select a satellite group:', tuple(cc.TLE_GROUP_URL))
    satellites = query_sats(satellite_group_type)
    return satellites

satellites = get_satellites()
st.sidebar.success(f"Loaded {len(satellites)} satellites.",icon="✅")

# 2. Get Date Range
start_time = dt.utcnow().replace(tzinfo=timezone('UTC'))
dateOptStart = dt(start_time.year, start_time.month, start_time.day, start_time.hour, 0, 0, 0, timezone('UTC'))
dateChoice = st.sidebar.slider(
    f"Select time range (UTC):",
    min_value = dateOptStart,
    max_value = dateOptStart + timedelta(days=2),
    value=(dateOptStart, dateOptStart + timedelta(hours=18)),
    step = (timedelta(minutes=120)),
    format = "MM/DD HH:mm")

# 3. Select satellite of interest
by_name = {f"{sat.model.satnum:<6} | {sat.name}": sat for sat in satellites}
col1, col2 = st.columns([1,1])
with col1:
    option1 = st.selectbox('Select primary satellite:', tuple(by_name), index=3)
    satObject1 = st_utils.Satellite(by_name[str(option1)])
    satObject1.results_for_rpo(dateChoice)
    st1df = satObject1.ephemeris.get_df_with_fields()
    st.caption(f'''{satObject1.tle_epoch_str}
                {satObject1.tle_age_str}''')
with col2:
    option2 = st.selectbox('Select secondary satellite:', tuple(by_name))
    satObject2 = st_utils.Satellite(by_name[str(option2)])
    satObject2.results_for_rpo(dateChoice)
    st2df = satObject2.ephemeris.get_df_with_fields()
    st.caption(f'''{satObject2.tle_epoch_str}
                {satObject2.tle_age_str}''')

# # show results
compare_sats(st1df, st2df)


import tkinter as tk

from tkinter import ttk
from tkinter import filedialog as fd

class AppView(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Image to Video Generator')
        self.geometry('600x300')
        self.minsize(600, 300)
        self.resizable(False, False)

        # self.columnconfigure(0, weight=1)
        # self.rowconfigure(0, weight=1)

        self.frame = ttk.Frame(self, relief=tk.GROOVE)
        self.frame.pack(padx=10, pady=10)

        for i in range(3):
            self.frame.columnconfigure(i, weight=1)
        for j in range(5):
            self.frame.rowconfigure(j, weight=1)

        self.label1 = tk.Label(self.frame, text='Base Folder')
        self.label2 = tk.Label(self.frame, text='Dest Folder')
        self.label3 = tk.Label(self.frame, text='Base Name')
        self.label4 = tk.Label(self.frame, text='FPS')

        self.basefolderVar = tk.StringVar()
        self.destfolderVar = tk.StringVar()

        self.basefolder_text = tk.Text(self.frame, height=1, width=50)
        self.basefolder_text.config(xscrollcommand=True)
        self.destfolder_text = tk.Text(self.frame, height=1, width=50)
        self.destfolder_text.config(xscrollcommand=True)
        self.fps_text = tk.Spinbox(self.frame, width=5)
        self.basename_text = tk.Text(self.frame, height=1, width=30)

        self.basefolderbtn = ttk.Button(self.frame, text='Select')
        self.destfolderbtn = ttk.Button(self.frame, text='Select')
        self.runSingle_btn = ttk.Button(self.frame, text='Generate Single', width=15)
        self.runMulti_btn  = ttk.Button(self.frame, text='Generate Multi', width=15)

        self.label1.grid(column=0, row=0, padx=(10, 0), pady=10, sticky='E')
        self.label2.grid(column=0, row=1, padx=(10, 0), pady=10, sticky='E')
        self.label3.grid(column=0, row=2, padx=(10, 0), pady=10, sticky='E')
        self.label4.grid(column=0, row=3, padx=(10, 0), pady=10, sticky='E')

        self.basefolder_text.grid(column=1, row=0, padx=5, pady=10, sticky='W')
        self.destfolder_text.grid(column=1, row=1, padx=5, pady=10, sticky='W')
        self.basename_text.grid(column=1, row=2, padx=5, pady=10, sticky='W')
        self.fps_text.grid(column=1, row=3, padx=5, pady=10, sticky='W')

        self.basefolderbtn.grid(column=2, row=0, padx=(0, 10), pady=10, sticky='W')
        self.destfolderbtn.grid(column=2, row=1, padx=(0, 10), pady=10, sticky='W')
        self.runSingle_btn.grid(column=1, row=4, padx=(100, 0), pady=10, sticky='W')
        self.runMulti_btn.grid(column=1, row=4, padx=(0, 100), pady=10, sticky='E')


# 월간 코드 챌린지 시즌2 > 약수의 개수와 덧셈
# 링크: https://programmers.co.kr/learn/courses/30/lessons/77884?language=python3

# left부터 right까지 약수의 개수가 짝수인 수는 더하고
# 홀수인 수는 빼기

def solution(left, right):
    answer = 0
    for i in range(left, right+1):
        count = 0
        for j in range(1, i+1):
            if i % j == 0: count += 1

        if count % 2 == 0:
            answer += i
        else:
            answer -= i
    return answer

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print(solution(13, 17))    # 43

# 문제 풀이 시간: 7분

''' 더 좋은 코드
def solution(left, right):
    answer = 0
    for i in range(left,right+1):
        if int(i**0.5)==i**0.5:
            answer -= i
        else:
            answer += i
    return answer
'''

Programmers/월간 코드 챌린지 시즌2/약수의 개수와 덧셈.py

약수의 개수와 덧셈.py


"""@package NMF-visualization

Compare different spectrogram normalization methods for NMF.
"""

import numpy as np

from tabulate import tabulate

from lib.NMF import betaDivergence
from lib.NMF import frobenius
from lib.NMF import KLD
from lib.NMF import NMF
from lib.normalize import maxNormalize
from lib.normalize import RMSnormalize
from lib.normalize import sumNormalize
from lib.spectrogram import magnitudeSpectrogram
from lib.utils import createDir

AUDIO_PATH = "data/audio/"
SPECTROGRAM_PATH = "data/spectrograms/"

if (__name__ == "__main__"):

    excerpt = "bassoon-solo"
    hopLen = 10   # ms
    Fs = 44100

    try:
        print ("Loading spectrogram.")
        S = np.load(SPECTROGRAM_PATH + excerpt + ".npy")
    except:
        print ("Spectrogram not found. Calcualting from audio.")
        path = AUDIO_PATH + excerpt + ".wav"
        x, Fs = librosa.load(path, sr = Fs, mono = True)
        S = magnitudeSpectrogram(x, Fs)
        path = SPECTROGRAM_PATH + excerpt + ".npy"
        createDir(path)
        np.save(path, S)

    table = []
    headers = ["Normalization", "Frobenius norm", "Beta divergence", "KLD"]
    fmt = ".3g"

    # No normalization.
    print ("Calculating NMF with no normalization.")
    V = S
    H, W = NMF(V, verbose = True)
    Vestimate = W*H
    frobeniusError = frobenius(V, Vestimate)
    betaError = betaDivergence(V, Vestimate)
    kldError = KLD(V, Vestimate)
    table += [["None", frobeniusError, betaError, kldError]]

    # Sum normalization.
    print ("Calculating NMF with sum normalization.")
    V = sumNormalize(S, axis = 1)
    H, W = NMF(V, verbose = True)
    Vestimate = W*H
    frobeniusError = frobenius(V, Vestimate)
    betaError = betaDivergence(V, Vestimate)
    kldError = KLD(V, Vestimate)
    table += [["Sum", frobeniusError, betaError, kldError]]

    # Max normalization.
    print ("Calculating NMF with max normalization.")
    V = maxNormalize(S, axis = 1)
    H, W = NMF(V, verbose = True)
    Vestimate = W*H
    frobeniusError = frobenius(V, Vestimate)
    betaError = betaDivergence(V, Vestimate)
    kldError = KLD(V, Vestimate)
    table += [["Max", frobeniusError, betaError, kldError]]

    # RMS normalization.
    print ("Calculating NMF with RMS normalization.")
    V = RMSnormalize(S, axis = 1)
    H, W = NMF(V, verbose = True)
    Vestimate = W*H
    frobeniusError = frobenius(V, Vestimate)
    betaError = betaDivergence(V, Vestimate)
    kldError = KLD(V, Vestimate)
    table += [["RMS", frobeniusError, betaError, kldError]]

    print(tabulate(table, headers, tablefmt = "github", floatfmt = fmt))


from django import forms
from topics.models import topic

class CreateTopicForm(forms.ModelForm):

    class Meta:
        model = topic
        fields = ['topic_name', 'research_direction', 'type', 'content']

class EditTopicForUserForm(forms.ModelForm):

    class Meta:
        model = topic
        fields = ['topic_name', 'research_direction', 'type', 'content']

    def save(self, commit=True):
        topic_post = self.instance
        topic_post.topic_name = self.cleaned_data['topic_name']
        topic_post.content = self.cleaned_data['content']
        topic_post.research_direction = self.cleaned_data['research_direction']
        topic_post.type = self.cleaned_data['type']

        if commit:
            topic_post.save()
        return topic_post

class EditTopicForSuperUserForm(forms.ModelForm):

    class Meta:
        model = topic
        fields = ['rate', 'review_date', 'process']

    def save(self, commit=True):
        topic_post = self.instance
        topic_post.rate = self.cleaned_data['rate']
        topic_post.review_date = self.cleaned_data['review_date']
        topic_post.process = self.cleaned_data['process']

        if commit:
            topic_post.save()
        return topic_post


#Lambda is an anonymous function. It does not have a name

# Normal function
# def greet(day):
#     print("Good", day)

# greet("morning")

# # Lambda equivalent
(lambda day:print("Good", day))("Afternoon")

# greet("Morning")

#filter(functin(), iterable)

list2=[45,32,4,65,23,45]
filter(lambda x:x<30,list2)

#scaler
# print((lambda x:x*3)(6))

#filter

#example2
# list2=[45,32,4,65,23,45]
# result=list(filter(lambda x:x<30,list2))
# print(result)


a = int(input())
b = []
for i in range(a):
	b.append(input().split(" "))
c = int(input())
for i in range(c):
	d,e,f = input().split(" ")
	d,e = int(d), int(e)
	if f == "A":
		if d < e:
			print(b[d-1][e-1])
		else:
			print(b[e-1][d-1])
	else:
		if d > e:
			print(b[d-1][e-1])
		else:
			print(b[e-1][d-1])


import urllib
from unittest.mock import MagicMock, create_autospec, Mock

import pytest

from server_administrator.server_administrator import ServerAdministrator, ServerConfiguration

class Fixtures:

    @pytest.fixture
    def mock_seconds_of_sleep(self):
        return Mock()

    @pytest.fixture
    def mock_urlretrieve(self, mocker):
        mock = MagicMock()
        mocker.patch("urllib.request.urlretrieve", mock)

    @pytest.fixture
    def mock_subprocess_run(self, mocker):
        mock = MagicMock()
        mocker.patch("subprocess.run", mock)
        return mock

    @pytest.fixture
    def mock_directory_builder(self):
        from server_administrator.directory_builder import DirectoryBuilder
        mock_builder = create_autospec(DirectoryBuilder)
        return mock_builder("")

    @pytest.fixture
    def mock_eula_editor(self):
        from server_administrator.editor import EulaEditor
        mock_editor = create_autospec(EulaEditor)
        return mock_editor("")

    @pytest.fixture(autouse=True)
    def administrator(self, mock_directory_builder, server_configuration, mock_eula_editor, mock_seconds_of_sleep):
        return ServerAdministrator(
            config=server_configuration,
            builder=mock_directory_builder,
            editor=mock_eula_editor,
            sleeper=mock_seconds_of_sleep
        )

    @pytest.fixture
    def server_configuration(self):
        return ServerConfiguration(
            directory="/opt/minecraft/server",
            port=25565,
            max_memory=1024,
            min_memory=1024,
        )

class TestRunner(Fixtures):

    # Todo: consider abstracting
    def test_can_download_the_server_jar(self, administrator, mock_urlretrieve, mock_eula_editor, mock_subprocess_run):
        administrator.build_and_launch_server()

        urllib.request.urlretrieve.assert_called_with(
            url="https://piston-data.mojang.com/v1/objects/c9df48efed58511cdd0213c56b9013a7b5c9ac1f/server.jar",
            filename="/opt/minecraft/server/server.jar"
        )

    def test_can_correctly_launch_minecraft_jar(self, mock_directory_builder, mock_eula_editor,
                                                mock_subprocess_run, mock_seconds_of_sleep, mock_urlretrieve):
        config = ServerConfiguration(
            max_memory=4096,
            min_memory=512,
            directory="/test/directory",
            port=5555,
        )
        administrator = ServerAdministrator(config, mock_directory_builder, mock_eula_editor, mock_seconds_of_sleep)

        administrator.build_and_launch_server()

        mock_subprocess_run.assert_called_with(
            [
                "/usr/bin/java",
                f'-Xmx4096M',
                f'-Xms512M',
                "-jar", "server.jar",
                "nogui"
            ],
            cwd="/test/directory"
        )

    # * I'm not actually sure the best way of spelling out this logic in a test other than this.
    # * the basic idea is that with the minecraft server we launch the jar once, the jar creates (amongst other things)
    # * the end user license agreement (the eula we need to modify) and then the jar's process exits asking you to
    # * update the eula. once it's updated we can launch the jar again and the server will run.
    # * It feels like in the spirit of "tests as documentation" we'd do something like assert the first call, then the
    # * eula update, then the second call in that order, but I don't know if that's actually possible.
    def test_can_update_eula(self, administrator, mock_eula_editor, mock_urlretrieve, mock_subprocess_run):
        administrator.build_and_launch_server()

        assert mock_subprocess_run.call_count == 2
        mock_eula_editor.update_state.assert_called_with("/opt/minecraft/server/eula.txt")


from django.urls import path
from . import views
from .views import *

urlpatterns = [
    path('api/todo/', views.TodoView.as_view()),
    path('api/todo/delete/', views.TodoDeleteView),
    path('api/event/', views.EventView.as_view()),
    path('api/event/delete/', views.EventDeleteView),
    path('api/add-members/<event_id>/', views.add_members),
    path('api/invite/<event_id>/', views.invite),
]


import argparse
import os
import shutil
import time

import pixmix_utils as utils
import numpy as np

import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
from torchvision import datasets
from torchvision import transforms
from tqdm import tqdm
import torchvision.transforms.functional as TF

from PIL import Image
import cv2

k = 4
beta = 4
all_ops = True
aug_severity = 1

IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]

to_tensor = transforms.ToTensor()
normalize = transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
standard_preprocess = {'tensorize': to_tensor, 'normalize': normalize}

def pixmix(orig, mixing_pic, preprocess):

    mixings = utils.mixings
    tensorize, normalize = preprocess['tensorize'], preprocess['normalize']
    if np.random.random() < 0.5:
        mixed = tensorize(augment_input(orig))
    else:
        mixed = tensorize(orig)

    for _ in range(np.random.randint(k + 1)):

        if np.random.random() < 0.5:
            aug_image_copy = tensorize(augment_input(orig))
        else:
            aug_image_copy = tensorize(mixing_pic)

        mixed_op = np.random.choice(mixings)
        mixed = mixed_op(mixed, aug_image_copy, beta)
        mixed = torch.clip(mixed, 0, 1)

    # return normalize(mixed)
    return mixed

def augment_input(image):
    aug_list = utils.augmentations_all if all_ops else utils.augmentations
    op = np.random.choice(aug_list)
    return op(image.copy(), aug_severity)

# Load the image
# sample_image = Image.open('panda.jpg')
mixing_image = Image.open("fractal.jpg")

# sample_image = sample_image.resize((256,256))
mixing_image = mixing_image.resize((256,256))

# # Apply the pixmix
# pixed = pixmix(sample_image, mixing_image, standard_preprocess)

# image = TF.to_pil_image(pixed)
# image.save('pixed_panda.jpg')

# a = to_tensor(sample_image)
# b = to_tensor(mixing_image)
# res = utils.multiply(a,b, beta)
# res = torch.clip(res, 0, 1)
# res = TF.to_pil_image(res)
# res.save('pixed_panda.jpg')

# apply pixmix to a video
video_path = "v_Archery_g01_c04.avi"
output_path = "output.avi"

cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))

for i in tqdm(range(frame_count)):
    ret, frame = cap.read()
    if ret == True:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = Image.fromarray(frame)
        frame = frame.resize((256,256))
        # pixed = pixmix(frame, mixing_image, standard_preprocess)

        a = to_tensor(frame)
        b = to_tensor(mixing_image)
        if np.random.random() < 0.5:
            pixed = utils.add(a,b, beta)
        else:
            pixed = utils.multiply(a,b, beta)
        pixed = torch.clip(pixed, 0, 1)

        pixed = TF.to_pil_image(pixed)
        pixed = cv2.cvtColor(np.array(pixed), cv2.COLOR_RGB2BGR)
        pixed = cv2.resize(pixed, (frame_width, frame_height))
        out.write(pixed)
    else:
        break

cap.release()
out.release()


# This problem was asked by Google.

# The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.

# Hint: The basic equation of a circle is x2 + y2 = r2

import random

INTERVAL= 1000

def solution():
  circle_points= 0
  square_points= 0

  for i in range(INTERVAL**2):

      rand_x= random.uniform(-1, 1)
      rand_y= random.uniform(-1, 1)

      origin_dist= rand_x**2 + rand_y**2

      if origin_dist<= 1:
          circle_points+= 1

      square_points+= 1
      pi = 4* circle_points/ square_points

  return pi

print(solution())


# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['HororSnake.py'],
             pathex=['C:/Users/Freeman.Autobot/Desktop/PythonExamples/HororSnake5'],
             binaries=[],
             datas=[('python_icon.ico','.'),('sounds/.','sounds')],
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='SnakeHoror',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,
          icon='python_icon.ico',
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None,
          version='version.rc')


from flask import Flask, request

app = Flask(__name__)

visitors = {}

def get_hit_count(remote_ip):
    if remote_ip in visitors:
        visitors[remote_ip] = visitors[remote_ip] + 1
    else:
        visitors[remote_ip] = 1
    hits = visitors[remote_ip]
    return hits

@app.route('/',  methods=["GET"])
def get_ip():
    hits = get_hit_count(str(request.environ.get('HTTP_X_REAL_IP', request.remote_addr)))
    ip_res = "User Address: " + request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
    hit_count = "\nHits: " + str(hits)
    return ip_res+hit_count

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0', port=80)


"""
__author__ = "Nisarg Pate"
__version__ = "1.0.1"
__date__ = "02/06/2016"
"""

from node import Node
from problem import Problem
import sys
from problemSolver import ProblemSolver

class Controller:
    def __init__(self):
        self.algorithm = 0 #algorithm type: 1 - best first, 2 - minimax, 3 - alpha-beta
        self.player = 'X' #Your player: X or O [X goes first]
        self.cuttingDepth = 0  #Cut-off depth starting from root
        self.player1Algo = 1
        self.player2Algo = 1
        self.player2CuttingDepth = 1
        self.boardValue = []  #value of each block in 5x5 board
        self.currentBoard = [] #Current board state, indicating position of each players territory; * -> unoccupied

    def readInput(self):
        with open(sys.argv[2]) as inputFile:
            content = inputFile.readlines()

        self.algorithm = content[0]
        if int(self.algorithm) != 4:
            self.player = content[1].strip()
            self.cuttingDepth = content[2]
            self.boardValue = []
            self.currentBoard = []

            for x in range (3,8):
                self.boardValue.append(map(int, content[x].split()))
            for x in range(8,13):
                self.currentBoard.append(list(content[x].strip()))

        else:
            self.player = content[1].strip()
            self.player1Algo = content[2].strip()
            self.cuttingDepth = content[3].strip()
            self.opponent = content[4].strip()
            self.player2Algo = content[5].strip()
            self.player2CuttingDepth = content[6].strip()
            for x in range (7,12):
                self.boardValue.append(map(int, content[x].split()))
            for x in range(12,17):
                self.currentBoard.append(list(content[x].strip()))

    def solveProblem(self):
        problemSolverObj = ProblemSolver(self.currentBoard, self.boardValue, self.player, self.cuttingDepth)
        if int(self.algorithm) == 1:
            problemSolverObj.greedyBFS()
        elif int(self.algorithm) == 2:
            problemSolverObj.initMinMax()
        elif int(self.algorithm) == 3:
            problemSolverObj.initAlphaBeta()
        elif int(self.algorithm) == 4:
            problemSolverObj.initBattleSimulation(self.player1Algo, self.player2Algo, self.opponent, self.player2CuttingDepth)

def main():
    # read Input
    controllerObj = Controller()
    controllerObj.readInput()
    controllerObj.solveProblem()

if __name__ == "__main__":
    main()


from run_agent import run_agent
import numpy as np

def find_hyperparams(agent):
    """Grid Search to find optimal hyperparameter values
        -initial exploration rate
        -initial learning rate
    """

    explore_list = list(np.linspace(0.1, 1.0, 10))
    learn_list = list(np.linspace(0.1, 1.0, 10))
    best = 0, 0, 0

    for explore in explore_list:
        for learn in learn_list:
            times = run_agent(agent, False, False, explore, learn)
            time = np.mean(times)
            if time > best[0]:
                best = time, explore, learn
    return best

if __name__ == '__main__':
    best = find_hyperparams('InvPend')
    print("Best time: %2f, best initial expl: %2f, best init learning: %2f" % best)


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

# for DEB build-essential:native python3-setuptools python3-all debhelper

import re
from setuptools import setup
from setuptools import find_packages
from os import path

__author__ = "__author__"
__email__ = "__email__"

for line in open("src/seguid_calculator/calculator.py"):
    if line.startswith("__author__") or line.startswith("__email__"):
        exec(line.strip())

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

install_requires = []
with open("requirements.txt", encoding="utf-8") as f:
    for line in f.readlines():
        install_requires.append(re.split(r"(<|>|=)=", line)[0])

setup(name='seguid_calculator',
      author          =__author__,
      author_email    =__email__,
      zip_safe = False,
      packages=find_packages("src"),
      package_dir={"": "src"},
      url='https://github.com/BjornFJohansson/seguid_calculator#seguid_calculator',
      license='LICENSE.txt',
      description='''Calculates SEGUID, cSEGUID & lSEGUID checksums for biological sequences''',
      long_description=long_description,
      long_description_content_type='text/markdown',
      setup_requires=["setuptools_scm"],
      install_requires=install_requires,
      use_scm_version={"write_to": "src/seguid_calculator/_version.py"},
      entry_points={'gui_scripts': ['seguid_calculator = seguid_calculator.calculator:main']},
      keywords="bioinformatics",
      classifiers=['Development Status :: 4 - Beta',
                   'Environment :: Console',
                   'Intended Audience :: Education',
                   'Intended Audience :: Science/Research',
                   'License :: OSI Approved :: BSD License',
                   'Programming Language :: Python :: 3.7',
                   'Programming Language :: Python :: 3.8',
                   'Programming Language :: Python :: 3.9',
                   'Programming Language :: Python :: 3.10',
                   'Programming Language :: Python :: 3.11',
                   'Topic :: Education',
                   'Topic :: Scientific/Engineering :: Bio-Informatics'])


###Integrate an ordinary differential equation
#in MATLAB that's using the function ode45.
#in Python we're going to use the Scipy toolbox and odeint
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as I
import control as C
import scipy.signal as S
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

kps = np.linspace(-100,100,400)
kds = np.linspace(-100,100,400)

kp, kd = np.meshgrid(kps,kds, sparse=False, indexing='ij')

eqn1 = (3-4*kd)

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection='3d')
ax1.plot_wireframe(kp,kd,eqn1/abs(eqn1))
plt.xlabel('kp')
plt.ylabel('kd')

eqn2 = (24-28*kd-4*kp)

fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
ax2.plot_wireframe(kp,kd,eqn2/abs(eqn2))
plt.xlabel('kp')
plt.ylabel('kd')

eqn3 = ((24-28*kd-4*kp)*(48+8*kd-28*kp) - (3-4*kd)*(8*kp+168*kd))/(24-28*kd-4*kp)

fig3 = plt.figure()
ax3 = fig3.add_subplot(111, projection='3d')
ax3.plot_wireframe(kp,kd,eqn3/abs(eqn3))
plt.xlabel('kp')
plt.ylabel('kd')

eqn4 = (24-28*kd-4*kp)*(-(168*kp)*(24-28*kd-4*kp) + (8*kp+168*kd)*((24-28*kd-4*kp)*(48+8*kd-28*kp) - (3-4*kd)*(8*kp+168*kd))/(24-28*kd-4*kp))/((24-28*kd-4*kp)*(48+8*kd-28*kp) - (3-4*kd)*(8*kp+168*kd))

fig4 = plt.figure()
ax4 = fig4.add_subplot(111, projection='3d')
ax4.plot_wireframe(kp,kd,eqn4/abs(eqn4))
plt.xlabel('kp')
plt.ylabel('kd')

eqn5 = (168*kp)

fig5 = plt.figure()
ax5 = fig5.add_subplot(111, projection='3d')
ax5.plot_wireframe(kp,kd,eqn5/abs(eqn5))
plt.xlabel('kp')
plt.ylabel('kd')

##Final result. Add everything together. Whereever the function is equal to 6 is stable
fig6 = plt.figure()
ax6 = fig6.add_subplot(111, projection='3d')
ax6.plot_wireframe(kp,kd,eqn1/abs(eqn1)+eqn2/abs(eqn2)+eqn3/abs(eqn3)+eqn4/abs(eqn4)+eqn5/abs(eqn5))
plt.xlabel('kp')
plt.ylabel('kd')

plt.show()


# Python is sequential

#Class
class Matrix:
  def __init__(self, rows=1, cols=1, default_value=0):
    self.rows = rows
    self.cols = cols
    self.default_value = default_value
    self.matrix = [0] * rows
    # initialize the matrix with default value
    for i in range(rows):
      self.matrix[i] = [0] * cols

  def __add__(self, other):
    # initialize and declare a return matrix

    # Check to see if the size of the matrices are equal.
    if  (self.rows == other.rows) & (self.cols == other.cols):
      # return matrix declared only if the addition is valid
      returnMatrix = Matrix(self.rows,self.cols)
      # add the matrices together
      for i in range(len(self.rows)):
        for j in range(len(self.cols)):
          returnMatrix[i][j] = self.matrix[i][j] + other.matrix[i][j]
    else:
      # report invalid addition
      print("invalid addition")

  def print(self):
    for i in range(self.rows):
      for j in range(self.cols):
        print(self.matrix[i][j],end=' ')
      print('\n')

# test  main
def main():
  m = Matrix(5,5,0)
  m.print()

if __name__ == "__main__":
  main()


"""LEA 128-bit cipher."""
import itertools

from cascada.bitvector.core import Constant
from cascada.bitvector.operation import RotateLeft as ROL
from cascada.bitvector.operation import RotateRight as ROR

from cascada.bitvector.ssa import RoundBasedFunction
from cascada.primitives.blockcipher import Encryption, Cipher

class LEAKeySchedule(RoundBasedFunction):
    """Key schedule function."""

    num_rounds = 24
    input_widths = [32, 32, 32, 32]
    output_widths = [32 for i in range(24 * 6)]

    @classmethod
    def set_num_rounds(cls, new_num_rounds):
        cls.num_rounds = new_num_rounds
        cls.output_widths = [32 for _ in range(new_num_rounds * 6)]

    @classmethod
    def eval(cls, k0, k1, k2, k3):
        round_keys = [None for _ in range(cls.num_rounds)]
        T0, T1, T2, T3 = k0, k1, k2, k3

        delta = [
            Constant(0xc3efe9db, 32), Constant(0x44626b02, 32),
            Constant(0x79e27c8a, 32), Constant(0x78df30ec, 32),
        ]

        for i in range(cls.num_rounds):
            T0 = ROL(T0 + ROL(delta[i % 4], i), 1)
            T1 = ROL(T1 + ROL(delta[i % 4], (i + 1)), 3)
            T2 = ROL(T2 + ROL(delta[i % 4], (i + 2)), 6)
            T3 = ROL(T3 + ROL(delta[i % 4], (i + 3)), 11)
            round_keys[i] = T0, T1, T2, T1, T3, T1
            cls.add_round_outputs(*round_keys[i])

        return tuple(itertools.chain(*round_keys))  # flatten

class LEAEncryption(Encryption, RoundBasedFunction):
    """Encryption function."""

    num_rounds = 24
    input_widths = [32, 32, 32, 32]
    output_widths = [32, 32, 32, 32]
    round_keys = None

    @classmethod
    def set_num_rounds(cls, new_num_rounds):
        cls.num_rounds = new_num_rounds

    @classmethod
    def eval(cls, p0, p1, p2, p3):
        x0, x1, x2, x3 = p0, p1, p2, p3

        rk = [[None for _ in range(6)] for _ in range(cls.num_rounds)]
        for i, k in enumerate(cls.round_keys):
            rk[i // 6][i % 6] = k

        for i in range(cls.num_rounds):
            k0, k1, k2, k3, k4, k5 = rk[i]
            tmp = x0
            x0 = ROL((x0 ^ k0) + (x1 ^ k1), 9)
            x1 = ROR((x1 ^ k2) + (x2 ^ k3), 5)
            x2 = ROR((x2 ^ k4) + (x3 ^ k5), 3)
            x3 = tmp
            cls.add_round_outputs(x0, x1, x2, x3)

        return x0, x1, x2, x3

class LEACipher(Cipher):
    """LEA-128 cipher."""
    key_schedule = LEAKeySchedule
    encryption = LEAEncryption

    @classmethod
    def set_num_rounds(cls, new_num_rounds):
        cls.key_schedule.set_num_rounds(new_num_rounds)
        cls.encryption.set_num_rounds(new_num_rounds)

    @classmethod
    def test(cls):
        old_num_rounds = cls.num_rounds
        cls.set_num_rounds(24)
        key = [
            Constant(0x3c2d1e0f, 32),
            Constant(0x78695a4b, 32),
            Constant(0xb4a59687, 32),
            Constant(0xf0e1d2c3, 32)
        ]
        pt = [
            Constant(0x13121110, 32),
            Constant(0x17161514, 32),
            Constant(0x1b1a1918, 32),
            Constant(0x1f1e1d1c, 32)
        ]
        ct = [
            Constant(0x354ec89f, 32),
            Constant(0x18c6c628, 32),
            Constant(0xa7c73255, 32),
            Constant(0xfd8b6404, 32)
        ]
        assert cls(pt, key) == tuple(ct)
        cls.set_num_rounds(old_num_rounds)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 18:30:27 2022

@author: patrick
"""
import argparse
import numpy as np
import pandas as pd
import os, zipfile
from datetime import datetime, timedelta
from tqdm import tqdm

################### FUnctions for dealing with zips and files and stuff ################3

### ENSURES WE SAVE THE ZIPS AS FEATHERS - TO SAVE SPACE ####
def save_in_feather(file_name, delete_csv=False):
    #Read in data - and create time stamp
    print('Reading in:' ,file_name[:-4]+'.csv')
    data=pd.read_csv(file_name[:-4]+'.csv')
    data.columns=[ 'tradeId' 	,'price' 	,'qty' 	,'quoteQty' 	,'time' 	,'isBuyerMaker' 	,'isBestMatch']
    data['date_time']=pd.to_datetime(data['time'],unit='ms' )
    if delete_csv:
        print("Dleting CSV:", file_name[:-4]+'.csv')
        os.remove(file_name[:-4]+'.csv') # delete csv
    #Save as a feather file
    data.to_feather(file_name[:-4])
    print("Saved: ", file_name[:-4])

#Unpack all zips
def unpack_zips(base_folder, coin):
    #Finds the files files we care about within the base_folder subdirectories and move the ZIP file contents to base_folder and create Feather Files for them.
    for subdir, dirs, files in os.walk(base_folder):
        for file in files:
            if file.endswith('.zip') and coin in file:
                print (os.path.join(subdir, file))
                zip_ref = zipfile.ZipFile(os.path.join(subdir, file)) # create zipfile object
                zip_ref.extractall(base_folder) # extract file to dir
                zip_ref.close() # close file
                save_in_feather(os.path.join(base_folder, file), delete_csv=True) #Save files as feather to be used later
                print (os.path.join(base_folder, file))

################################# Functions for candle creation   ############################

## Creates time-stamps that we will use later #####
def create_timings(start_time, end_time, interval=5):
    delta = timedelta(minutes=interval)
    times=[]
    while start_time < end_time:
        times.append(start_time)
        start_time += delta
    times.append(end_time)
    return times

#Creates all timings for the given data frame - and time_interval - in minutes
def all_timings(df, interval):
    start_time=df['date_time'].min().replace(second=0, microsecond=0, nanosecond=0 )
    end_time= df['date_time'].max().replace(hour=23, minute=59, second=59, microsecond=59, nanosecond=59 )
    times = create_timings(start_time, end_time, interval=interval)  #Interval in minutes
    return times

#Calculates the volume weighted price
def calculate_vwap(relevant_ticks):
    price_volume=0 ; volume=0
    for index, tick in relevant_ticks.iterrows():
        #If tick is a BUY
        price_volume+=(tick[2]*tick[1])  #Volume * Price
        volume+=tick[2]  #Volue
        return price_volume/volume, volume

def create_candles(df, times):
    candels=[]
    for period in tqdm(range(len(times)-1)):
        #relevant_ticks=sample_ticks[sample_ticks['date_time']]
        relevant_ticks=df[(df['date_time']>= times[period]) & (df['date_time']< times[period+1])]
        if  len(relevant_ticks)>0:
            #Calculate the volume weighted price
            vwap, volume = calculate_vwap(relevant_ticks)
            #Output list
            #['Start_time' , 'End_Time', 'VWAP', 'High', 'Low', 'Open', 'Close']
            candels.append([times[period],times[period+1],vwap, relevant_ticks['price'].max(),relevant_ticks['price'].min(), relevant_ticks['price'].iloc[0], relevant_ticks['price'].iloc[-1],volume  ])
        else:
            candels.append([times[period],times[period+1],0, 0,0, 0, 0, 0  ])
    return candels
        #tick=relevant_ticks.iloc[0]

################################# Functions to output Candle information - all    ############################

#Combine all Same currencies in the base directory - into a single file
def create_all_candles(base_folder, coin, candle_size, remove_temp_files, remove_intermediate_files):
    all_candels=[]; base_folder=base_folder+'/'
    for file in os.listdir(base_folder):
        if coin in file and "Candles" not in file:
            print(file)
            ticks=pd.read_feather(base_folder+file)
            times = all_timings(ticks, int(candle_size))
            canedls= create_candles(ticks, times)
            all_candels.append(canedls)
            canedls=pd.DataFrame(canedls)
            canedls.to_csv(base_folder+file+'_Candles_%s_Back_up.csv' %candle_size, index=False)
            del canedls
            os.remove(base_folder+file)
    #Combine all files into single file
    flat_list = [item for sublist in all_candels for item in sublist]
    all_candels_df=pd.DataFrame(flat_list )
    all_candels_df.columns=['Opened', 'Closed', 'vwap','High', 'Low', 'Open', 'Close','Volume']
    all_candels_df.to_csv(base_folder+'%s_Candles_%s.csv' %(coin,candle_size), index=False)

    #Remove temp_files
    if remove_temp_files==True:
        for file in os.listdir(base_folder):
            if coin in file and "Back_up" in file:
                os.remove(base_folder+file)

################################# Function to compile all Candles correctly - and clean up   ############################

#Compiles all intermediate candle files
def order_and_interpolate(base_folder,coin,candle_size,remove_intermediate_files,remove_temp_files):
    base_folder=base_folder+'/'
    data=pd.read_csv(base_folder+'%s_Candles_%s.csv' %(coin,candle_size))

    #Sort values
    new_data= data.sort_values(by="Opened")
    #Interpolate Values - So we don't get any weird Zeros for pirces and stuff
    cols = [ 'vwap', 'High', 'Low', 'Open', 'Close']
    new_data[cols] = new_data[cols].replace({'0':np.nan, 0:np.nan})
    new_data = new_data.dropna(axis=1, how='all').interpolate(limit=20, limit_direction='both')
    #Save the new/final csv
    new_data.to_csv(base_folder+'%s_Candles_%s_Final.csv' %(coin,candle_size), index=False)
    print("Final %s Candles %s are found at: base_folder+'%s_Candles_%s_Ordered_Interpolated.csv" %(coin,candle_size,coin,candle_size))

    #Remove Intermediate Candle files (BUt not the back_up files)
    if remove_intermediate_files:
        for file in os.listdir(base_folder):
            if coin in file and "Final" not in file and "Back_up" not in file:
                os.remove(base_folder+file)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):
    dependencies = [
        ('alibaba', '0004_auto_20150928_2017'),
    ]

    operations = [
        migrations.AlterField(
            model_name='city',
            name='chart',
            field=models.CharField(max_length=200, null=True, verbose_name='url\u6807\u5fd7', blank=True),
        ),
        migrations.AlterField(
            model_name='city',
            name='ename',
            field=models.CharField(max_length=200, null=True, verbose_name='\u82f1\u6587\u7b80\u79f0', blank=True),
        ),
    ]


'''
Creates test data.
Steps:
1. Read the given mapped file row wise and based on the threshold decide if it is part of test (year > threshold_year).
2. Create edgelists for train and test (raw files) : Read the mapped file and write all edges in raw files, both for train and test data
3. Remove edges from test_raw which are in train_raw
4. Create set of nodes present in train, store as embedded_nodes
5. Write edges to test where both nodes are in embedded_nodes

Command: python test_data.py -d <dataset>
'''

import csv
import argparse
import os

# threshold year to divide the dataset into train and test
threshold_year_start = 1970
threshold_year = 2013

def create_raw(dataset):

	f = open( dataset + '/train_raw.csv', 'w')
	g = open(  dataset + '/test_raw.csv', 'w')

		# write all edges both for train and test data
	reader = csv.reader(open(dataset+'/PPI.csv'), delimiter=',')
	#next(reader, None)  # skip the headers
	for row in reader:
		[p1, p2, year,month,day] = [row[2], row[3],int(row[4]),row[5], row[6]]
		if year <= threshold_year and year >= threshold_year_start:
			f.write( p1 + "," +  p2 +"," +str(year) +","+ month+"," +day + "\n")
		elif year > threshold_year:
			g.write(p1 + "," +  p2 +"," +str(year) +","+ month+","+ day +"\n")
	f.close()
	g.close()
# create test dataset
def create_test(dataset):
	# remove edges from test_raw which are in train_raw

	command = 'sort ' + dataset + '/test_raw.csv ' +  dataset + '/train_raw.csv '+ dataset + '/train_raw.csv | uniq -u > ' + dataset+'/diff.csv'
	os.system(command)
	embedded_nodes = set()
	reader = csv.reader(open(dataset+'/PPI.csv'), delimiter=',')
	#next(reader, None)  # skip the headers
	# print dataset
	for row in reader:
		[p1, p2, year] = [row[2], row[3],int(row[4])]
		if year <= threshold_year and year >= threshold_year_start:
			embedded_nodes.add(p1)
			embedded_nodes.add(p2)

	# write edges to test where both nodes are in embedded_nodes
	out_file = dataset+'/test_edges.csv'
	f = open(dataset+'/diff.csv', 'r')
	g = open(out_file, 'w')

	reader = csv.reader(f, delimiter=',')
	#print(embedded_nodes)
	for row in reader:
		[p1, p2] = [row[0], row[1]]
		if p1 in embedded_nodes and p2 in embedded_nodes and p1<p2:
			g.write( p1 + "," +  p2 +"\n")

	f.close()
	g.close()
	command = 'rm ' + dataset+'/diff.csv'
	os.system(command)

	print("Created test data for " + dataset + " dataset: " + out_file)
	print('Number of edges: ')
	os.system('wc -l ' + out_file)

if __name__ == '__main__':

	parser = argparse.ArgumentParser(description='Creating test data')
	parser.add_argument('-d','--dataset', dest='dataset', required=True,
			    help='Here the Dataset is PPI-master')

	args = parser.parse_args()

	dataset = args.dataset

	print("Creating test data for " + dataset + " dataset")

	create_raw(dataset)
	create_test(dataset)


hours = input('Enter hours worked\n')
rate = input('Enter rate worked \n')

hrs = float(hours)
r = float(rate)

if hrs > 40:
    ogp = hrs * r
    egp = (hrs - 40) * (r * 0.5)
    gp = (ogp + egp)
    print("Your new pay is: ")

else:
    gp = (hrs * r)
print("Your pay is:  ", gp)


import layers
import keras
import soundfile as sf
import librosa
import resampy
import os
import numpy as np
import sys
from typing import NamedTuple

class MelGenerator(keras.utils.Sequence):
    def __init__(self, n_ids, batch_size, DATA_ROOT):
        self.n_ids = n_ids
        self.batch_size = batch_size
        self.mel_basedir = os.path.join(DATA_ROOT, 'melspec/')
        with np.load(os.path.join(DATA_ROOT, 'openmic-2018.npz')) as OPENMIC:
            self.Y_true, self.Y_mask, self.sample_key = OPENMIC['Y_true'], OPENMIC['Y_mask'], OPENMIC['sample_key']

    def __len__(self):
        return int(np.ceil(len(self.n_ids) / float(self.batch_size)))

    def __getitem__(self, idx):
        batch_n_ids = self.n_ids[idx * self.batch_size:(idx + 1) * self.batch_size]
        return self._data_from_ids(batch_n_ids)

    def _data_from_ids(self, n_ids):
        x_ids = self.sample_key[n_ids]
        x_batch = []
        y_batch = np.multiply(self.Y_true[n_ids], self.Y_mask[n_ids]) > 0.5
        for x_id in x_ids:
            mel_subdir = os.path.join(self.mel_basedir, x_id[:3])
            melpath = os.path.join(mel_subdir, x_id+'.npz')
            with np.load(melpath) as f:
                x_batch.append(f['melspec'].T)
        x_batch = np.array(x_batch)
        x_batch = np.expand_dims(x_batch, axis=-1)

        return x_batch.astype('float32'), y_batch

    def on_epoch_end(self):
        np.random.shuffle(self.n_ids)

class MelParam(NamedTuple):
    sr: int
    n_fft: int
    hop_length: int

def save_melspec(x_id,
                 audio_basedir='/beegfs/qx244/ds/openmic-2018/audio/',
                 mel_basedir='/beegfs/qx244/ds/openmic-2018/melspec/',
                 recompute_if_exist=False,
                 param=MelParam(sr=44100,n_fft=2048,hop_length=512)):
    # create corresponding .npz files that stores the mel_spectrogram

    # check if feature dir exist
    audio_subdir = os.path.join(audio_basedir, x_id[:3])
    mel_subdir = os.path.join(mel_basedir, x_id[:3])
    if not os.path.isdir(mel_subdir):
        os.mkdir(mel_subdir)
    excerpt_path = os.path.join(audio_subdir, x_id+'.ogg')
    mel_path = os.path.join(mel_subdir, x_id+'.npz')

    if (not recompute_if_exist) and os.path.isfile(mel_path):
        print('{} already exist.'.format(mel_path))

    else:
        y, sr = sf.read(excerpt_path)
        # Make Mono
        if y.ndim == 2:
            y = y.mean(axis=-1)
        if sr != param.sr:
#             print('resampling excerpt {} from original sr {}'.format(x_id, sr))
            y = resampy.resample(y, sr, param.sr)
        melspec = librosa.feature.melspectrogram(y=y, sr=param.sr,
                                                 n_fft=param.n_fft, hop_length=param.hop_length)
        if melspec.shape != (128,862):
            print("bad shape! ID:{}, shape: {}".format(x_id, melspec.shape))
        np.savez(mel_path, x_id=x_id, melspec=melspec)

def construct_crnnL3_smp_tom(input_shape=(None, 128, 1), n_classes=20):
    '''
    Modified from Openmic's original models.py file
    CRNN with L3-style conv encoder
    Parameters
    ----------
    input_shape (defaults to (862, 128, 1).... but is this even what I think it is?)
    alpha (for softmax... large => max pooling)

    Returns
    -------
    Keras model
    '''

    input_feature = keras.Input(shape=input_shape)

    # Apply batch normalization
    x_bn = keras.layers.BatchNormalization()(input_feature)

    # BLOCK 1
    conv1 = keras.layers.Convolution2D(64, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(x_bn)
    bn1 = keras.layers.BatchNormalization()(conv1)
    conv2 = keras.layers.Convolution2D(64, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(bn1)
    bn2 = keras.layers.BatchNormalization()(conv2)
    pool2 = keras.layers.MaxPooling2D((2,2), padding='valid')(bn2)

    # BLOCK 2
    conv3 = keras.layers.Convolution2D(128, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(pool2)
    bn3 = keras.layers.BatchNormalization()(conv3)
    conv4 = keras.layers.Convolution2D(128, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(bn3)
    bn4 = keras.layers.BatchNormalization()(conv4)
    pool4 = keras.layers.MaxPooling2D((2, 2), padding='valid')(bn4)

    # BLOCK 3
    conv5 = keras.layers.Convolution2D(256, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(pool4)
    bn5 = keras.layers.BatchNormalization()(conv5)
    conv6 = keras.layers.Convolution2D(256, (3, 3),
                                   padding='same',
                                   activation='relu',
                                   kernel_initializer='he_normal')(bn5)
    bn6 = keras.layers.BatchNormalization()(conv6)
    pool6 = keras.layers.MaxPooling2D((2, 2), padding='valid')(bn6)

    # BLOCK 4
    conv7 = keras.layers.Convolution2D(512, (3, 3),
                                       padding='same',
                                       activation='relu',
                                       kernel_initializer='he_normal')(pool6)
    bn7 = keras.layers.BatchNormalization()(conv7)
    conv8 = keras.layers.Convolution2D(512, (3, 3),
                                       padding='same',
                                       activation='relu',
                                       kernel_initializer='he_normal')(bn7)
    bn8 = keras.layers.BatchNormalization()(conv8)
    pool8 = keras.layers.MaxPooling2D((2, 2), padding='valid')(bn8)

    # CONV SQUEEZE
    conv_sq = keras.layers.Convolution2D(1024, (1, 8),
                                         padding='valid',
                                         activation='relu',
                                         kernel_initializer='he_normal')(pool8)
    bn8 = keras.layers.BatchNormalization()(conv_sq)
    sq2 = layers.SqueezeLayer(axis=-2)(bn8)

    # RNN
    # First recurrent layer: a 128-dim bidirectional gru
#     rnn1 = keras.layers.Bidirectional(keras.layers.GRU(128,
#                                                        return_sequences=True))(sq2)

    p0 = keras.layers.Dense(n_classes, activation='sigmoid')

    p_dynamic = keras.layers.TimeDistributed(p0, name='dynamic/tags')(sq2) #rnn1

    p_static = layers.AutoPool(axis=1,
                               name='static/tags')(p_dynamic)

    model = keras.models.Model(input_feature,
                               p_static)

    return model


#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter07/client.py
# Simple Zen-of-Python client that asks three questions then disconnects.

import argparse, random, socket, zen_utils, time, datetime

def client(address, cause_error=False):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(address)
    f = open("output.txt",'wb')
    aphorisms = list(zen_utils.aphorisms)
    time.sleep(10)
    a=datetime.datetime.now();
    if cause_error:
        sock.sendall(aphorisms[0][:-1])
        return
    message = sock.recv(65535)
    while (message):
        #print('hello ',message)
        f.write(message)
        message=sock.recv(65535)

    f.close()
    b=datetime.datetime.now()
    c=b-a
    print(c.seconds)
    sock.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Example client')
    parser.add_argument('host', help='IP or hostname')
    parser.add_argument('-e', action='store_true', help='cause an error')
    parser.add_argument('-p', metavar='port', type=int, default=1060,
                        help='TCP port (default 1060)')
    args = parser.parse_args()
    address = (args.host, args.p)
    client(address, args.e)


from peewee import DoesNotExist
from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CommandHandler, Filters, MessageHandler, CallbackQueryHandler

from filters import PermittedChatFilter
from model import BlockedStickerpack
from utils import is_user_group_admin, get_username_or_name, grouper
from utils import set_callback_data, process_callback_query, get_callback_data

class BlockStickerpack:
    _STICKER_ADD_URL = 'https://t.me/addstickers/'
    _ADMIN_RESTRICTION_MESSAGE = 'Эта функция предназначена только для админов!'
    _NO_STICKERPACKS_BLOCKED_MESSAGE = 'Заблокированных стикерпаков *нет*.'

    def __init__(self, chat_id, admin_id):
        self._chat_id = chat_id
        self._admin_id = admin_id

        self.permitted_chat_filter = PermittedChatFilter([self._admin_id, self._chat_id])

    def add_handlers(self, add_handler):
        add_handler(MessageHandler(
            self.permitted_chat_filter & ~Filters.private & Filters.sticker,
            self._watchdog))
        add_handler(CommandHandler('block_stickerpack', callback=self._block, filters=self.permitted_chat_filter))
        add_handler(CommandHandler('unblock_stickerpack', callback=self._unblock, filters=self.permitted_chat_filter))
        add_handler(CallbackQueryHandler(self._unblock_stickerpack_button))

    def _get_stickers_link(self, stickerpack_name):
        return self._STICKER_ADD_URL + stickerpack_name

    @staticmethod
    def _get_blocked_stickerpacks():
        return BlockedStickerpack.select()

    def _block(self, bot, update):
        message = update.message

        if message.from_user.id != self._admin_id and not is_user_group_admin(bot, message.from_user.id,
                                                                              message.chat_id, self._admin_id):
            message.reply_text(text=self._ADMIN_RESTRICTION_MESSAGE, quote=False)
            return

        if message.reply_to_message is None or message.reply_to_message.sticker is None:
            message.reply_text(text='Команда предназначена только для ответа на сообщения со стикером!', quote=False)
            return

        sticker = message.reply_to_message.sticker

        stickerpack, created = BlockedStickerpack.get_or_create(name=sticker.set_name)

        pack_name = stickerpack.name
        pack_link = self._get_stickers_link(pack_name)

        if not created:
            response_text = f'Стикерпак [{pack_name}]({pack_link}) *уже заблокирован*.'
        else:
            # mention_markdown позволяет кликнуть по пользователю и глянуть кто это, вместо простого имени / юзернейма
            sticker_from = message.reply_to_message.from_user.mention_markdown()
            who_block = message.from_user.mention_markdown()

            response_text = f'Стикерпак [{pack_name}]({pack_link}) успешно *заблокирован*!\n' \
                            f'Стикер отправил: {sticker_from} | Заблокировал: {who_block}'

        message.reply_text(text=response_text, parse_mode=ParseMode.MARKDOWN, quote=False)

    def _unblock(self, bot, update):
        message = update.message

        if message.from_user.id != self._admin_id and not is_user_group_admin(bot, message.from_user.id,
                                                                              message.chat_id, self._admin_id):
            message.reply_text(text=self._ADMIN_RESTRICTION_MESSAGE, quote=False)
            return

        blocked_stickerpacks = self._get_blocked_stickerpacks()

        packs_list = []
        buttons = []

        if blocked_stickerpacks:
            for index, stickerpack in enumerate(blocked_stickerpacks, start=1):
                packs_list.append(f'{index}. [{stickerpack.name}]({self._get_stickers_link(stickerpack.name)})')
                buttons.append(
                    InlineKeyboardButton(
                        text=str(index),
                        callback_data=set_callback_data(stickerpack.id))
                )

            response_text = '*Заблокированные стикерпаки:*\n{}\n\nКакой *разблокировать*?'.format("\n".join(packs_list))

            keyboard = grouper(buttons, 5)  # в одном ряду будет 5 кнопок, так как текст на каждой из них короткий
            reply_markup = InlineKeyboardMarkup(keyboard, one_time_keyboard=True)
        else:
            response_text = self._NO_STICKERPACKS_BLOCKED_MESSAGE
            reply_markup = None

        message.reply_text(text=response_text, parse_mode=ParseMode.MARKDOWN, reply_markup=reply_markup, quote=False)

    @process_callback_query
    def _unblock_stickerpack_button(self, bot, update):
        query = update.callback_query

        if query.from_user.id != self._admin_id and not is_user_group_admin(bot, query.from_user.id,
                                                                            query.message.chat_id, self._admin_id):
            bot.answer_callback_query(query.id, self._ADMIN_RESTRICTION_MESSAGE, show_alert=True)
            return

        message = query.message

        pack_id = get_callback_data(query.data)

        try:
            stickerpack = BlockedStickerpack.get(BlockedStickerpack.id == pack_id)
        except DoesNotExist:
            response_text = f'Выбранный стикерпак *не был найден* в списке заблокированных.'
        else:
            pack_name = stickerpack.name
            pack_link = self._get_stickers_link(pack_name)

            stickerpack.delete_instance()

            response_text = f'Стикерпак [{pack_name}]({pack_link}) успешно *разблокирован*.'

        message.edit_text(text=response_text, parse_mode=ParseMode.MARKDOWN)

    @staticmethod
    def _watchdog(bot, update):
        message = update.message
        sticker = message.sticker

        try:
            BlockedStickerpack.get(BlockedStickerpack.name == sticker.set_name)
        except DoesNotExist:
            return

        message.delete()


from flask_restx import Resource, reqparse, fields
from namespaces import get_namespace, NamespaceEnum
from collectionsDataset import (
    get_all_collections,
    upload_new_names
)

from errorHandlers import notBadRequestException

api = get_namespace(NamespaceEnum.COLLECTIONS)

organizationCollections = api.model('collectionDataset', {
    'id': fields.String(required=True, descripion="The id of collection"),
    'name': fields.String(required=True, description="Collection name")
})

@api.route("/")
class organizationCollections(Resource):
    collections_parser = reqparse.RequestParser(bundle_errors=True)
    collections_parser.add_argument('selected_account', type=str, required=True, help="The target account to work with.", location="args")

    #the response object
    @api.marshal_with(organizationCollections, False, 200)
    #response types/codes
    @api.doc(responses={500: 'There was an internal error', 400: 'Bad request'})
    #the request parameters
    @api.expect(collections_parser)
    #get the self, account from the request object
    def get(self):
        account = self.collections_parser.parse_args().get('selected_account')

        try:
            return get_all_collections(account)
        except Exception as e:
            if notBadRequestException(e):
                api.abort(500, str(e))
            raise e

    parser_new_names = reqparse.RequestParser(bundle_errors=True)
    parser_new_names.add_argument('selected_account', type=str, required=True, help="The target account to work with.", location="args")
    parser_new_names.add_argument('selected_dataset', type=str, required=True, help="The target dataset to work with.", location="args")
    parser_new_names.add_argument('collection', type=list, required=True, help='List of the collection tag ids', location="json")

    # @api.marshal_with(model_new_collection_names, False, 200)
    @api.doc(description="Method for creating new collection names on Pennsieve", responses={200: 'Success', 403: 'User is not owner or manager to dataset', 500: 'There was an internal server error', 400: 'Bad request'})
    @api.expect(parser_new_names)

    def post(self):
        data = self.parser_new_names.parse_args()
        account = data.get('selected_account')
        dataset_name = data.get('selected_dataset')
        collection_names = data.get('collection')

        try:
            return upload_new_names(account, dataset_name, collection_names)
        except Exception as e:
                if notBadRequestException(e):
                    api.abort(500, str(e))
                raise e


import torch.nn as nn
from torchsummary import summary

class ResNet_FeatureExtractor(nn.Module):

    def __init__(self, input_channel, output_channel=512):
        super(ResNet_FeatureExtractor, self).__init__()
        self.ConvNet = ResNet(input_channel, output_channel, BasicBlock, [1, 2, 5, 3])

    def forward(self, input):
        return self.ConvNet(input)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = self._conv3x3(inplanes, planes)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = self._conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def _conv3x3(self, in_planes, out_planes, stride=1):
        "3x3 convolution with padding"
        return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                         padding=1, bias=False)

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)

        return out

class ResNet(nn.Module):

    def __init__(self, input_channel, output_channel, block, layers):
        super(ResNet, self).__init__()

        self.output_channel_block = [int(output_channel / 4), int(output_channel / 2), output_channel, output_channel]

        self.inplanes = int(output_channel / 8)
        self.conv0_1 = nn.Conv2d(input_channel, int(output_channel / 16),
                                 kernel_size=3, stride=1, padding=1, bias=False)
        self.bn0_1 = nn.BatchNorm2d(int(output_channel / 16))
        self.conv0_2 = nn.Conv2d(int(output_channel / 16), self.inplanes,
                                 kernel_size=3, stride=1, padding=1, bias=False)
        self.bn0_2 = nn.BatchNorm2d(self.inplanes)
        self.relu = nn.ReLU(inplace=True)

        self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        self.layer1 = self._make_layer(block, self.output_channel_block[0], layers[0])
        self.conv1 = nn.Conv2d(self.output_channel_block[0], self.output_channel_block[
                               0], kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(self.output_channel_block[0])

        self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        self.layer2 = self._make_layer(block, self.output_channel_block[1], layers[1], stride=1)
        self.conv2 = nn.Conv2d(self.output_channel_block[1], self.output_channel_block[
                               1], kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(self.output_channel_block[1])

        self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=(2, 1), padding=(0, 1))
        self.layer3 = self._make_layer(block, self.output_channel_block[2], layers[2], stride=1)
        self.conv3 = nn.Conv2d(self.output_channel_block[2], self.output_channel_block[
                               2], kernel_size=3, stride=1, padding=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.output_channel_block[2])

        self.layer4 = self._make_layer(block, self.output_channel_block[3], layers[3], stride=1)
        self.conv4_1 = nn.Conv2d(self.output_channel_block[3], self.output_channel_block[
                                 3], kernel_size=2, stride=(2, 1), padding=(0, 1), bias=False)
        self.bn4_1 = nn.BatchNorm2d(self.output_channel_block[3])
        self.conv4_2 = nn.Conv2d(self.output_channel_block[3], self.output_channel_block[
                                 3], kernel_size=2, stride=1, padding=0, bias=False)
        self.bn4_2 = nn.BatchNorm2d(self.output_channel_block[3])

    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv0_1(x)
        x = self.bn0_1(x)
        x = self.relu(x)
        x = self.conv0_2(x)
        x = self.bn0_2(x)
        x = self.relu(x)

        x = self.maxpool1(x)
        x = self.layer1(x)
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)

        x = self.maxpool2(x)
        x = self.layer2(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)

        x = self.maxpool3(x)
        x = self.layer3(x)
        x = self.conv3(x)
        x = self.bn3(x)
        x = self.relu(x)

        x = self.layer4(x)
        x = self.conv4_1(x)
        x = self.bn4_1(x)
        x = self.relu(x)
        x = self.conv4_2(x)
        x = self.bn4_2(x)
        x = self.relu(x)

        return x

class BidirectionalLSTM(nn.Module):

    def __init__(self, nIn, nHidden, nOut):
        super(BidirectionalLSTM, self).__init__()

        self.rnn = nn.LSTM(nIn, nHidden, bidirectional=True)
        self.embedding = nn.Linear(nHidden * 2, nOut)

    def forward(self, input):
        recurrent, _ = self.rnn(input)
        T, b, h = recurrent.size()
        t_rec = recurrent.view(T * b, h)

        output = self.embedding(t_rec)  # [T * b, nOut]
        output = output.view(T, b, -1)

        return output

class CRNN(nn.Module):

    def __init__(self, imgH, nc, nclass, nh, n_rnn=2, leakyRelu=False):
        super(CRNN, self).__init__()
        assert imgH % 16 == 0, 'imgH has to be a multiple of 16'

        cnn = ResNet_FeatureExtractor(1, 512)
        self.cnn = cnn
        self.rnn = nn.Sequential(
            BidirectionalLSTM(512, nh, nh),
            BidirectionalLSTM(nh, nh, nclass))

    def forward(self, input):
        # feature extraction
        conv = self.cnn(input)
        b, c, h, w = conv.size()
        assert h == 1, "the height of conv must be 1"
        conv = conv.squeeze(2)
        conv = conv.permute(2, 0, 1)  # [w, b, c]

        # sequence modelings
        output = self.rnn(conv)

        return output

if __name__ == "__main__":
    model = CRNN(32, 1, 92, 256)
    summary(model, (1, 32, 100))


import io
import re

import apache_beam as beam
import tensorflow as tf
from PIL import Image
from google.cloud.storage import Client

class DecodeFromTextDoFn(beam.DoFn):

    def process(self, element, *args, **kwargs):
        path, label = element.split(",")
        path = re.split(rf"(gs://.+?/)", path)[2]
        return [[path, label]]

class ReadImagesDoFn(beam.DoFn):
    gcs_client = None
    bucket = None

    def __init__(self, bucket_name):
        super().__init__()
        self.bucket_name = bucket_name

    def setup(self):
        self.gcs_client = Client()
        self.bucket = self.gcs_client.bucket(self.bucket_name)

    def process(self, element, *args, **kwargs):
        path, label = element
        blob = self.bucket.blob(path)
        img_bytes = blob.download_as_bytes()
        yield img_bytes, int(label)

    def teardown(self):
        self.gcs_client.close()

class ImageToTfExampleDoFn(beam.DoFn):

    @staticmethod
    def _bytes_feature(value):
        return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

    @staticmethod
    def _int64_feature(value):
        return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

    def process(self, element, *args, **kwargs):
        img_raw, label = element
        image_shape = Image.open(io.BytesIO(img_raw)).size
        example = tf.train.Example(
            features=tf.train.Features(
                feature={'height': self._int64_feature(image_shape[1]),
                         'width': self._int64_feature(image_shape[0]),
                         'image_raw': self._bytes_feature(img_raw),
                         'label': self._int64_feature(label)}
            )
        )
        yield example


import sys
from Qt import QtCore, QtWidgets, QtGui

class Ui_MainWindow(object):
	def setupUi(self, MainWindow):
		MainWindow.setObjectName("MainWindow")
		MainWindow.resize(639, 426)
		MainWindow.setStyleSheet("background-color: rgb(50, 50, 50);\n"
		                         "color:rgb(255, 255, 255);")
		MainWindow.setAnimated(False)
		MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
		self.centralwidget = QtWidgets.QWidget(MainWindow)
		self.centralwidget.setObjectName("centralwidget")
		self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
		self.verticalLayout.setContentsMargins(0, 0, 0, 0)
		self.verticalLayout.setSpacing(0)
		self.verticalLayout.setObjectName("verticalLayout")
		self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
		self.tabWidget.setStyleSheet("QTabWidget::pane {\n"
		                             "border-top: 2px solid rgb(28,28,28);\n"
		                             "}\n"
		                             "QTabBar::tab {\n"
		                             "    border-top: 2px solid rgb(28,28,28);\n"
		                             "     border-right: 2px solid rgb(28,28,28);\n"
		                             "    padding: 10px 25px 10px 25px;\n"
		                             "}\n"
		                             "QTabBar::tab:selected {\n"
		                             "    background-color: rgb(255, 66, 3)\n"
		                             "}\n"
		                             "QTabBar::tab:hover {\n"
		                             "    background-color:rgb(80, 80, 80)\n"
		                             "}\n"
		                             "QTabBar::tab:selected:hover {\n"
		                             "    background-color: rgb(254, 92, 33)\n"
		                             "}\n"
		                             "")
		self.tabWidget.setObjectName("tabWidget")
		self.tab = QtWidgets.QWidget()
		self.tab.setObjectName("tab")
		self.horizontalLayout = QtWidgets.QHBoxLayout(self.tab)
		self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
		self.horizontalLayout.setSpacing(0)
		self.horizontalLayout.setObjectName("horizontalLayout")
		self.widget = QtWidgets.QWidget(self.tab)
		self.widget.setMinimumSize(QtCore.QSize(260, 0))
		self.widget.setMaximumSize(QtCore.QSize(260, 16777215))
		self.widget.setObjectName("widget")
		self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget)
		self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
		self.verticalLayout_2.setSpacing(0)
		self.verticalLayout_2.setObjectName("verticalLayout_2")
		self.lineEdit = QtWidgets.QLineEdit(self.widget)
		self.lineEdit.setMinimumSize(QtCore.QSize(0, 25))
		self.lineEdit.setMaximumSize(QtCore.QSize(16777215, 25))
		self.lineEdit.setStyleSheet("background-color:rgb(25, 25, 25);\n"
		                            "border:0;")
		self.lineEdit.setText("")
		self.lineEdit.setFrame(True)
		self.lineEdit.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
		self.lineEdit.setReadOnly(False)
		self.lineEdit.setObjectName("lineEdit")
		self.verticalLayout_2.addWidget(self.lineEdit)
		self.listWidget = QtWidgets.QListWidget(self.widget)
		self.listWidget.setStyleSheet("QListView {\n"
		                              "border: 1px solid rgb(28,28,28);\n"
		                              "background-color: rgb(42,42,42);\n"
		                              "alternate-background-color: rgb(38,38,38);\n"
		                              "}\n"
		                              "\n"
		                              "QListView::item {\n"
		                              "height:30px;\n"
		                              "}\n"
		                              "QListView::item:selected {\n"
		                              "    background-color: rgb(255, 66, 3);\n"
		                              "    color:white;\n"
		                              "}\n"
		                              "QListView::item:hover {\n"
		                              "    background-color:rgb(80, 80, 80)\n"
		                              "}\n"
		                              "QListView::item:selected:hover {\n"
		                              "    background-color: rgb(254, 92, 33)\n"
		                              "}\n"
		                              "QScrollBar:vertical {\n"
		                              " width: 10px; \n"
		                              "}\n"
		                              " QScrollBar::handle:vertical {\n"
		                              "     background: rgb(80, 80, 80);\n"
		                              "     min-height: 20px;\n"
		                              " }\n"
		                              " QScrollBar::add-line:vertical {\n"
		                              "    border: none;\n"
		                              "      background: none;\n"
		                              " }\n"
		                              "\n"
		                              " QScrollBar::sub-line:vertical {\n"
		                              "     border: none;\n"
		                              "      background: none;\n"
		                              " }\n"
		                              "\n"
		                              " QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {\n"
		                              "     background: none;\n"
		                              " }\n"
		                              "\n"
		                              "")
		self.listWidget.setProperty("showDropIndicator", False)
		self.listWidget.setAlternatingRowColors(True)
		self.listWidget.setObjectName("listWidget")
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		item = QtWidgets.QListWidgetItem()
		self.listWidget.addItem(item)
		self.verticalLayout_2.addWidget(self.listWidget)
		self.applyButton = QtWidgets.QPushButton(self.widget)
		self.applyButton.setStyleSheet("background-color:rgb(28, 28, 28);")
		self.applyButton.setAutoDefault(False)
		self.applyButton.setDefault(False)
		self.applyButton.setFlat(False)
		self.applyButton.setObjectName("applyButton")
		self.verticalLayout_2.addWidget(self.applyButton)
		self.horizontalLayout.addWidget(self.widget)
		self.widget_2 = QtWidgets.QWidget(self.tab)
		self.widget_2.setObjectName("widget_2")
		self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.widget_2)
		self.verticalLayout_3.setContentsMargins(9, 9, 9, 0)
		self.verticalLayout_3.setSpacing(0)
		self.verticalLayout_3.setObjectName("verticalLayout_3")
		self.headerText = QtWidgets.QLabel(self.widget_2)
		self.headerText.setMinimumSize(QtCore.QSize(0, 25))
		self.headerText.setMaximumSize(QtCore.QSize(16777215, 25))
		font = QtGui.QFont()
		font.setPointSize(13)
		font.setBold(True)
		font.setUnderline(False)
		font.setWeight(75)
		font.setKerning(True)
		self.headerText.setFont(font)
		self.headerText.setStyleSheet("color:rgb(250, 70, 1)")
		self.headerText.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
		self.headerText.setObjectName("headerText")
		self.verticalLayout_3.addWidget(self.headerText)
		self.infoText = QtWidgets.QLabel(self.widget_2)
		sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
		sizePolicy.setHorizontalStretch(0)
		sizePolicy.setVerticalStretch(0)
		sizePolicy.setHeightForWidth(self.infoText.sizePolicy().hasHeightForWidth())
		self.infoText.setSizePolicy(sizePolicy)
		self.infoText.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
		self.infoText.setWordWrap(True)
		self.infoText.setObjectName("infoText")
		self.verticalLayout_3.addWidget(self.infoText)
		self.groupBox = QtWidgets.QGroupBox(self.widget_2)
		self.groupBox.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
		self.groupBox.setFlat(True)
		self.groupBox.setCheckable(False)
		self.groupBox.setObjectName("groupBox")
		self.verticalLayout_3.addWidget(self.groupBox)
		self.horizontalLayout.addWidget(self.widget_2)
		self.tabWidget.addTab(self.tab, "")
		self.tab_2 = QtWidgets.QWidget()
		self.tab_2.setObjectName("tab_2")
		self.tabWidget.addTab(self.tab_2, "")
		self.tab_3 = QtWidgets.QWidget()
		self.tab_3.setObjectName("tab_3")
		self.tabWidget.addTab(self.tab_3, "")
		self.verticalLayout.addWidget(self.tabWidget)
		MainWindow.setCentralWidget(self.centralwidget)
		self.statusbar = QtWidgets.QStatusBar(MainWindow)
		self.statusbar.setObjectName("statusbar")
		MainWindow.setStatusBar(self.statusbar)
		self.actionNew = QtWidgets.QAction(MainWindow)
		self.actionNew.setObjectName("actionNew")
		self.actionModidy = QtWidgets.QAction(MainWindow)
		self.actionModidy.setObjectName("actionModidy")

		self.retranslateUi(MainWindow)
		self.tabWidget.setCurrentIndex(0)
		QtCore.QMetaObject.connectSlotsByName(MainWindow)

	def retranslateUi(self, MainWindow):
		_translate = QtCore.QCoreApplication.translate
		MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
		self.lineEdit.setPlaceholderText(_translate("MainWindow", "Search..."))
		self.listWidget.setSortingEnabled(False)
		__sortingEnabled = self.listWidget.isSortingEnabled()
		self.listWidget.setSortingEnabled(False)
		item = self.listWidget.item(0)
		item.setText(_translate("MainWindow", "CA Soft IK"))
		item = self.listWidget.item(1)
		item.setText(_translate("MainWindow", "Noise"))
		item = self.listWidget.item(2)
		item.setText(_translate("MainWindow", "Reduce"))
		item = self.listWidget.item(3)
		item.setText(_translate("MainWindow", "Ribbon"))
		item = self.listWidget.item(4)
		item.setText(_translate("MainWindow", "Simple Doft IK"))
		item = self.listWidget.item(5)
		item.setText(_translate("MainWindow", "Sine"))
		item = self.listWidget.item(6)
		item.setText(_translate("MainWindow", "Spline Sine"))
		item = self.listWidget.item(7)
		item.setText(_translate("MainWindow", "Spring"))
		item = self.listWidget.item(8)
		item.setText(_translate("MainWindow", "Tail Dynamics"))
		item = self.listWidget.item(9)
		item.setText(_translate("MainWindow", "Transform Noise"))
		self.listWidget.setSortingEnabled(__sortingEnabled)
		self.applyButton.setText(_translate("MainWindow", "Apply"))
		self.headerText.setText(_translate("MainWindow", "TextLabel"))
		self.infoText.setText(_translate("MainWindow", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "))
		self.groupBox.setTitle(_translate("MainWindow", "Options"))
		self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "New"))
		self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Applied"))
		self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Help"))
		self.actionNew.setText(_translate("MainWindow", "New"))
		self.actionModidy.setText(_translate("MainWindow", "Modify"))

if __name__ == "__main__":
	app = QtWidgets.QApplication(sys.argv)
	MainWindow = QtWidgets.QMainWindow()
	ui = Ui_MainWindow()
	ui.setupUi(MainWindow)
	MainWindow.show()
	sys.exit(app.exec_())


import pandas as pd
import numpy as np

from sklearn.preprocessing import StandardScaler, LabelBinarizer, LabelEncoder
from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.model_selection import RandomizedSearchCV
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.utils.class_weight import compute_class_weight

# set a seed for random, so results are reproducible
seed = np.random.seed(seed=200)

# input file name
input_file = "./input/ted.xml"

# constants
lines = []
talks = {}
relevant = False
talk = ""
key = ""
talk_count = 0

with open(input_file, 'r', encoding='utf-8') as freader:
    lines = freader.readlines()

for line in lines:
    # determine according to the keywords if the talk is relevant
    if '<keywords' in line:
        key = ""

        if 'technology' in line:
            key += "T"
        else:
            key += 'x'

        if 'entertainment' in line:
            key += "E"
        else:
            key += 'x'

        if 'design' in line:
            key += 'D'
        else:
            key += 'x'

        if key != 'xxx':
            relevant = True
            continue
        else:
            relevant = False
            continue

    if not relevant:
        continue

    # start reading the content
    if '<transcription>' in line:
        talk = ""

    # append each line of the transcript
    elif '<seekvideo' in line:
        start = line.find('>') + 1
        end = line.rfind('<')
        talk += line[start:end] + " "

    # end of a talk
    elif '</transcription>' in line:
        # store each talk with key and content
        talks[talk_count] = [key, talk]
        talk_count += 1
        relevant = False

# transform the dict into a dataframe
df = pd.DataFrame.from_dict(talks, orient='index', columns=['label', 'talk'])
df = df.reset_index().drop(columns=['index'])

# show dataframe info
print(df.describe())

# compute the class weight
class_weights = compute_class_weight('balanced', np.unique(df['label']), df['label'])

for label, weight in zip(np.unique(df['label']), class_weights):
    print("label: {} -> weight: {}".format(label, weight))

# split in training and test set
train = df.sample(frac=0.8, random_state=seed)
test = df.drop(train.index)

# for training
y_train = train['label']
x_train = train.drop('label', axis=1)
x_train = x_train['talk'].values

# for testing
y_test = test['label']
x_test = test.drop('label', axis=1)
x_test = x_test['talk'].values

print('Training samples shape: ', x_train.shape)
print('Training labels shape: ', y_train.shape)
print('Test samples shape: ', x_test.shape)
print('Test labels shape: ', y_test.shape)

# encode the label
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_test = label_encoder.transform(y_test)
print(label_encoder.classes_)

# transform the talks
tfvect = TfidfVectorizer(ngram_range=(1,4), max_df=0.99, max_features=3000)
tfvect.fit(x_train)
x_train = tfvect.transform(x_train)
x_test = tfvect.transform(x_test)

# standardize the data
stand = StandardScaler(with_mean=False, with_std=True)
stand.fit(x_train)
x_train = stand.transform(x_train)
x_test = stand.transform(x_test)

# setup base mlp classifier
mlp = MLPClassifier(early_stopping=True, validation_fraction=0.2, random_state=seed, batch_size='auto',
                    max_iter=200, n_iter_no_change=15, learning_rate='adaptive', verbose=True)

# set up parameter grid to evaluate over
param_grid = dict(hidden_layer_sizes=[(100,100,100), (100,100), (100,)],
                  solver = ['adam', 'sgd'], activation=['tanh', 'relu'], alpha=[0.001, 0.0001])

# train mlp classifier using randomized grid search
gs_mlp = RandomizedSearchCV(mlp, param_grid, n_iter=5, cv=5, n_jobs=4, verbose=True, refit=True)
gs_mlp.fit(x_train, y_train)

# print the best parameters of the evaluation
print(gs_mlp.best_params_)
print(gs_mlp.best_score_)

# predict the test label
y_pred = gs_mlp.predict(x_test)

# print the accuracy and the confusion matrix
print(accuracy_score(y_pred, y_test))
print(confusion_matrix(y_pred, y_test))


from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

class Callback(StreamCallback):
    def __init__(self):
        pass

    @staticmethod
    def get_lines(data):
        return data.split('\n')

    def process(self, inputStream, outputStream):
        # read the flowfile
        data = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
        # write the contents of data to flowfile
        outputStream.write(bytearray(data.encode('utf-8')))

flowFile = session.get()

if flowFile != None:
    # write the output of Callback to flowfile
    flowFile = session.write(
        flowFile, Callback()
    )
    # add an attribute to the flowfile
    flowFile = session.putAttribute(
        flowFile,
        "filename",
        'flowfile_' + flowFile.getAttribute('uuid') + '.txt'
    )
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()


from django.test import TestCase

from unittest.mock import MagicMock, patch

from corehq.apps.export.models import CaseExportInstance, FormExportInstance
from corehq.apps.export.tasks import saved_exports

class TestDailySavedExports(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestDailySavedExports, cls).setUpClass()
        cls.exports = [
            FormExportInstance(is_daily_saved_export=True),
            FormExportInstance(is_daily_saved_export=False),
            CaseExportInstance(is_daily_saved_export=True),
        ]
        for export in cls.exports:
            export.save()

    @classmethod
    def tearDownClass(cls):
        for export in cls.exports:
            export.delete()
        super(TestDailySavedExports, cls).tearDownClass()

    def test_saved_exports_task(self):

        class MockExportFile(object):

            def __enter__(self):
                return ""

            def __exit__(self, exc_type, exc_val, exc_tb):
                pass

        magic_mock = MagicMock(return_value=MockExportFile())
        with patch("corehq.apps.export.tasks.rebuild_export", new=magic_mock):
            saved_exports()
            self.assertEqual(magic_mock.call_count, 2)


import json
import pathlib

from pgevents import data_access, App, cli
from pgevents.event import Event
from tests.integration import DSN

BASE_DIRECTORY = pathlib.Path(__file__).parent.absolute()

app = App(DSN, None)

def test_init_db(connection):

    with data_access.cursor(connection) as cursor:
        data_access.drop_table(cursor, "pgmigrations")
        data_access.drop_table(cursor, "events")
        data_access.drop_type(cursor, "event_status")

    cli.init_db("tests.integration.test_cli")

    event = Event(topic="hello")
    with data_access.cursor(connection) as cursor:
        data_access.create_event(cursor, event)

def test_create_event(connection):
    topic = "hello"
    payload = json.dumps(dict(hello="world"))
    created = cli.create_event("tests.integration.test_cli", topic, payload)

    with data_access.cursor(connection) as cursor:
        retrieved = data_access.get_event_by_id(cursor, created.id)

    assert retrieved.topic == topic
    assert retrieved.payload == json.loads(payload)