"""
.. _spectral_connectivity:

====================================
Compute connectivity on sensor space
====================================
The connectivity pipeline performs connectivity analysis in
sensor or source space.

The **input** data should be a time series matrix in **npy** or **mat** format.
"""

# Authors: Annalisa Pascarella
# License: BSD (3-clause)

# sphinx_gallery_thumbnail_number = 2
import os.path as op
import numpy as np
import nipype.pipeline.engine as pe

import ephypype
from ephypype.nodes import create_iterator, create_datagrabber
from ephypype.nodes import get_frequency_band
from ephypype.datasets import fetch_omega_dataset
import nipype.interfaces.io as nio
import sys

###############################################################################
# Let us fetch the data first. It is around 675 MB download.

data_path = op.join('/scratch/hyruuk/saflow_data/saflow_bids')

###############################################################################
# then read the parameters for experiment and connectivity from a
# :download:`json <https://github.com/neuropycon/ephypype/blob/master/examples/params.json>`
# file and print it

import json  # noqa
import pprint  # noqa
params = json.load(open("params.json"))

pprint.pprint({'experiment parameters': params["general"]})
#subject_ids = params["general"]["subject_ids"]  # sub-003
subject_ids = sys.argv[1:][0]
subject_ids = ['sub-' + str(subject_ids)]
session_ids = params["general"]["session_ids"]  # ses-0001
run_ids = params["general"]["run_ids"]  # ses-0001
cond_ids = params["general"]["cond_ids"]
cond_ids = [str(sys.argv[1:][1])]
NJOBS = params["general"]["NJOBS"]

pprint.pprint({'connectivity parameters': params["connectivity"]})
freq_band_names = params["connectivity"]['freq_band_names']
freq_bands = params["connectivity"]['freq_bands']
con_method = params["connectivity"]['method']
epoch_window_length = params["connectivity"]['epoch_window_length']
sfreq = 1200

###############################################################################
# Then, we create our workflow and specify the `base_dir` which tells
# nipype the directory in which to store the outputs.

# workflow directory within the `base_dir`
correl_analysis_name = 'spectral_connectivity_' + con_method

main_workflow = pe.Workflow(name=correl_analysis_name)
main_workflow.base_dir = data_path

###############################################################################
# Then we create a node to pass input filenames to DataGrabber from nipype

infosource = create_iterator(['subject_id', 'session_id', 'run_id', 'cond_id', 'freq_band_name'],
                             [subject_ids, session_ids, run_ids, cond_ids, freq_band_names])

###############################################################################
# and a node to grab data. The template_args in this node iterate upon
# the values in the infosource node
sources_fp = '/scratch/hyruuk/saflow_data/saflow_bids/source_reconstruction_MNE_aparca2009s/inv_sol_pipeline/'
template_path = sources_fp + '_run_id_%s_session_id_%s_subject_id_%s/inv_solution/%s_%s_task-gradCPT_%s_meg_%s_-epo_stc.hdf5'
template_args = [['run_id', 'session_id', 'subject_id', 'subject_id', 'session_id', 'run_id', 'cond_id']]

datasource = pe.Node(
    interface=nio.DataGrabber(infields=['subject_id', 'session_id', 'run_id', 'cond_id'], outfields=['ts_file']),
    name='datasource')

datasource.inputs.base_directory = data_path
datasource.inputs.template = template_path

datasource.inputs.template_args = dict(ts_file=template_args)
datasource.inputs.sort_filelist = True

###############################################################################
# Ephypype creates for us a pipeline which can be connected to these
# nodes we created. The connectivity pipeline is implemented by the function
# :func:`ephypype.pipelines.ts_to_conmat.create_pipeline_time_series_to_spectral_connectivity`,
# thus to instantiate this connectivity pipeline node, we import it and pass
# our parameters to it.
# The connectivity pipeline contains two nodes and is based on the MNE Python
# functions computing frequency- and time-frequency-domain connectivity
# measures. A list of the different connectivity measures implemented by MNE
# can be found in the description of :func:`mne.viz.plot_connectivity_circle`
#
# In particular, the two nodes are:
#
# * :class:`ephypype.interfaces.mne.spectral.SpectralConn` computes spectral connectivity in a given frequency bands
# * :class:`ephypype.interfaces.mne.spectral.PlotSpectralConn` plot connectivity matrix using the |plot_connectivity_circle| function
#
# .. |plot_connectivity_circle| raw:: html
#
#   spectral_connectivity function

from ephypype.pipelines import create_pipeline_time_series_to_spectral_connectivity # noqa
spectral_workflow = create_pipeline_time_series_to_spectral_connectivity(
    data_path, con_method=con_method,
    epoch_window_length=None, is_sensor_space=False)

###############################################################################
# The connectivity node needs two auxiliary nodes: one node reads the raw data
# file in .fif format and extract the data and the channel information; the
# other node get information on the frequency band we are interested on.

frequency_node = get_frequency_band(freq_band_names, freq_bands)

###############################################################################
# We then connect the nodes two at a time. First, we connect two outputs
# (subject_id and session_id) of the infosource node to the datasource node.
# So, these two nodes taken together can grab data.
# The third output of infosource (freq_band_name) is connected to the
# frequency node

main_workflow.connect(infosource, 'subject_id', datasource, 'subject_id')
main_workflow.connect(infosource, 'session_id', datasource, 'session_id')
main_workflow.connect(infosource, 'run_id', datasource, 'run_id')
main_workflow.connect(infosource, 'cond_id', datasource, 'cond_id')
main_workflow.connect(infosource, 'freq_band_name',
                      frequency_node, 'freq_band_name')

###############################################################################
# Similarly, for the inputnode of create_array_node and spectral_workflow.
# Things will become clearer in a moment when we plot the graph of the workflow

main_workflow.connect(datasource, 'ts_file',
                      spectral_workflow, 'inputnode.ts_file')

main_workflow.connect(frequency_node, 'freq_bands',
                      spectral_workflow, 'inputnode.freq_band')
spectral_workflow.inputs.inputnode.sfreq = sfreq

###############################################################################
# main_workflow.write_graph(graph2use='colored')  # colored

###############################################################################
# and visualize it. Take a moment to pause and notice how the connections
# here correspond to how we connected the nodes.

#import matplotlib.pyplot as plt  # noqa
#img = plt.imread(op.join(data_path, correl_analysis_name, 'graph.png'))
#plt.figure(figsize=(8, 8))
#plt.imshow(img)
#plt.axis('off')

###############################################################################
# Finally, we are now ready to execute our workflow.

main_workflow.config['execution'] = {'remove_unnecessary_outputs': 'false'}

# Run workflow locally on 1 CPU
main_workflow.run(plugin='MultiProc', plugin_args={'n_procs': NJOBS})


import pygame as pg
from settings import *
from spritesList import *
from keybindings import *
from eventHandling import *
from mapDicts import *
import sprites
import player
import unitSprites

class Camera():
    def __init__( self, aGame, aPosX, aPosY ):
        self.mGame = aGame
        self.xShift = 0
        self.yShift = 0
        self.xDisplacement = 0
        self.yDisplacement = 0
        self.vX = 0
        self.vY = 0
        self.mZoomFactor = 1.
        self.mZoomChange = 1.

    def apply( self, aObjectRect, aObject = None ):
        self.applyZoom( aObject )
        vRv = aObjectRect.move( self.xShift, self.yShift)
        return vRv

    def applyZoom( self, aObject ):
        if aObject:
            vWidth = self.mGame.mCurrentTileSize
            vHeight = self.mGame.mCurrentTileSize
            aObject.image = pg.transform.scale( aObject.mOrigImage, (vWidth, vHeight) )
            # aObject.rect = aObject.image.get_rect()
            aObject.rect.x = (aObject.x * self.mGame.mCurrentTileSize)
            aObject.rect.y = (aObject.y * self.mGame.mCurrentTileSize)
            aObject.rect.width = vWidth
            aObject.rect.height = vHeight

    def applyShift( self ):
        vDeltaXFromScreen, vDeltaYFromScreen = pg.mouse.get_pos()

        vMouseXRelativePos = vDeltaXFromScreen - self.xShift
        vMouseYRelativePos = vDeltaYFromScreen - self.yShift

        self.xShift += -((self.mZoomChange - 1) * vMouseXRelativePos)
        self.yShift += -((self.mZoomChange - 1) * vMouseYRelativePos)

        if self.mGame.mVerbosity == 5:
            print('\nZoomFactor: ', self.mZoomFactor)
            print('ZoomChange: ', self.mZoomChange)
            print('MousePos: ', vDeltaXFromScreen/self.mGame.mCurrentTileSize, ' , ', vDeltaYFromScreen/self.mGame.mCurrentTileSize)
            print('ShiftedPos: ', vMouseXRelativePos/self.mGame.mCurrentTileSize, ' , ', vMouseYRelativePos/self.mGame.mCurrentTileSize)
            print('Shift: ', self.xShift/self.mGame.mCurrentTileSize, ' , ', self.yShift/self.mGame.mCurrentTileSize)
        elif self.mGame.mVerbosity >= 6:
            print('\nZoomFactor: ', self.mZoomFactor)
            print('ZoomChange: ', self.mZoomChange)
            print('MousePos: ', vDeltaXFromScreen, ' , ', vDeltaYFromScreen)
            print('ShiftedPos: ', vMouseXRelativePos, ' , ', vMouseYRelativePos)
            print('Shift: ', self.xShift, ' , ', self.yShift)

    def getSpeed( self ):
        self.vX = 0
        self.vY = 0
        vPressedKeys = pg.key.get_pressed()
        if vPressedKeys[pg.K_a]:
            self.vX = Settings.CAMERASPEED
        if vPressedKeys[pg.K_d]:
            self.vX = -Settings.CAMERASPEED
        if vPressedKeys[pg.K_w]:
            self.vY = Settings.CAMERASPEED
        if vPressedKeys[pg.K_s]:
            self.vY = -Settings.CAMERASPEED
        if self.vX != 0 and self.vY != 0:
            self.vX *= 0.7071
            self.vY *= 0.7071
        self.vX *= int( self.mZoomFactor ** Settings.ZOOMCAMERASPEEDEXPONENT )
        self.vY *= int( self.mZoomFactor ** Settings.ZOOMCAMERASPEEDEXPONENT )

    def update( self ):
        self.getSpeed()
        self.xShift += self.vX
        self.yShift += self.vY

        # border collision code
        # math to calculate the far edge
        vFarXEdge = -((self.mGame.mCurrentTileSize * self.mGame.mMap.mapWidth) + Settings.BORDERWIDTH) + Settings.WIDTH
        # the signs need to change depending on how big the screen is
        if vFarXEdge - Settings.BORDERWIDTH > 0:
            if self.xShift > vFarXEdge:
                self.xShift = vFarXEdge

            elif self.xShift < Settings.BORDERWIDTH:
                self.xShift = Settings.BORDERWIDTH

        else:
            if self.xShift < vFarXEdge:
                self.xShift = vFarXEdge

            elif self.xShift > Settings.BORDERWIDTH:
                self.xShift = Settings.BORDERWIDTH

        vFarYEdge = -((self.mGame.mCurrentTileSize * self.mGame.mMap.mapHeight) + Settings.BORDERWIDTH) + Settings.HEIGHT
        if vFarYEdge - Settings.BORDERWIDTH > 0:
            if self.yShift < vFarYEdge:
                self.yShift = vFarYEdge

            elif self.yShift > Settings.BORDERWIDTH:
                self.yShift = Settings.BORDERWIDTH

        else:
            if self.yShift < vFarYEdge:
                self.yShift = vFarYEdge

            elif self.yShift > Settings.BORDERWIDTH:
                self.yShift = Settings.BORDERWIDTH
class Map:
    def __init__(self, aGame):
        self.mGame = aGame

    def update(self):
        pass

class TerrainMap(Map):
    def __init__(self, aGame, aMapData, aStartingPlayer, aUnitInfoList, aPlayerInfoList, aCityInfo ):
        # when creating a new map, clear the old player objects so new ones can be instantiated
        aGame.mPlayers.clear()

        self.mGame = aGame
        self.mapHeight = 0
        self.mapWidth = 0
        self.mMapData = []
        self.mTileDict = {}
        vY = 0

        self.mGame.mSelector = sprites.Selector( self.mGame )
        self.mGame.mSelected = sprites.Selected( self.mGame )

        aGame.mCurrentPlayerTurn = aStartingPlayer

        # add the player obj to the current players dictionary
        for vPlayer in aPlayerInfoList:
            vPlayerId = vPlayer[0]
            vPlayerMoney = vPlayer[1]
            vPlayerObj = player.Player( self.mGame, vPlayerId, vPlayerMoney )
            aGame.mPlayers[vPlayerId] = vPlayerObj

        aGame.mNumPlayers = int( len( aPlayerInfoList ) )

        # have to open the file twice as we run through it twice
        for vRow in aMapData :
            for vTile in vRow :
                if self.mapWidth < len( vRow ):
                    self.mapWidth = len( vRow )
            self.mapHeight += 1

        if aGame.mVerbosity >= 1:
            print('Mapsize: ', self.mapWidth, 'x', self.mapHeight)
        for vRow in aMapData:
            vMapRowData = []
            vX = 0
            for vTileChar in vRow:
                if len( vRow ) > 0 :
                    import tileDict
                    vTileConstructor = tileDict.tileDict[vTileChar]
                    if vTileChar in 'zxcvb':
                        try:
                            vCityOwner = aCityInfo[(vX, vY)]
                        except:
                            vCityOwner = None
                        vTile = self.createTile( vTileConstructor, vX, vY, vCityOwner )
                    else:
                        vTile = self.createTile( vTileConstructor, vX, vY )

                    vMapRowData.append( vTileChar )
                    self.mTileDict[ ( vX, vY ) ] = vTile
                    vX += 1
            self.mMapData.append( vMapRowData )
            vY += 1

        for vUnit in aUnitInfoList:
            # unit info : ( self.mName, ( self.x, self.y ), self.mPlayerId, self.mHealth, self.mLevel )
            vUnitName = vUnit[0]
            vUnitXPos = vUnit[1][0]
            vUnitYPos = vUnit[1][1]
            vPlayerId = vUnit[2]
            vUnitHealth = vUnit[3]
            vUnitLevel = vUnit[4]
            vUnitConstructor = getattr( sprites, vUnitName )
            vTile = self.mTileDict[(vUnitXPos, vUnitYPos)]
            vUnitConstructor( self.mGame, vTile, vPlayerId, vUnitHealth, vUnitLevel )
            if aGame.mVerbosity >= 5:
                print('Units being constructed', vUnitName)

        if aGame.mVerbosity >= 2:
            print('vnumplayers', aGame.mNumPlayers)

        # leave these for debug

        # unitSprites.LightInfantry(self.mGame, self.mTileDict[ ( 13, 10 ) ], 0, 98, 1)
        # unitSprites.LightInfantry( self.mGame, self.mTileDict[(13, 12)], 1, 27, 1 )
        # unitSprites.LightInfantry( self.mGame, self.mTileDict[(10, 14)], 0, 76, 1 )
        # unitSprites.LightInfantry( self.mGame, self.mTileDict[(10, 16)], 1, 100, 1 )
        # unitSprites.Battleship( self.mGame, self.mTileDict[(5, 5)], 0, 100, 1 )
        # unitSprites.Battleship( self.mGame, self.mTileDict[(5, 2)], 1, 100, 1 )

        #assuming all is well and generated, create the camera obj
        self.mXCenter = ( self.mapWidth * Settings.TILESIZE ) / 2
        self.mYCenter = ( self.mapHeight * Settings.TILESIZE ) / 2
        # TODO currently the centers dont acutlaly do anything, yes I spelled that wrong thanks Andrew
        self.mGame.mCamera = Camera( self.mGame, self.mXCenter, self.mYCenter )

        # after everything has finished, change game state last to avoid bugs
        self.mGame.mGameState = GameState.HUMANPLAYER

    def closeMap( self ):
        for vSpriteGroup in self.mGame.mAllSpriteGroups:
            vSpriteGroup.empty()
        # TODO, we need to delete all sprites, including selector/selected, all units, all tiles and players
        # TODO, this should maybe be something different
        # self.mGUI.startMenu( self )

    def update(self):
        pass

    def createTile( self, aConstructor, aXPos, aYPos, aOwner = 'notpassed' ):
        # aOwner can be 0 or None or it can maybe not be passed
        # which means it needs a default value, however that default can not be None or 0, since those are valid passing values
        # this string is good enough
        # do not change this without asking dennyv
        if aOwner == 'notpassed':
            vRv = aConstructor( self.mGame, aXPos, aYPos )
        else:
            vRv = aConstructor( self.mGame, aXPos, aYPos, aOwner )
        return vRv


from os import system
import threading
import time
import sys
import collections
import random
import copy

# Napisz program do symulowania ethernetowej metody dostepu do
# medium transmisyjnego (CSMA/CD). Wspólne łącze realizowane jest
# za pomocą tablicy: propagacja sygnału symulowana jest za pomoca
# propagacji wartości do sąsiednich komórek.

timing = 0.3    # sek - czas pomiędzy iteracjami / szybkość propagacji sygnału
log = []        # log wydarzeń
spacesBetween = 20    # odległości między komputerami w sieci

class Medium(object):
    def __init__(self, numberOfComputers):
        self.medium = ['_' for i in range(1 + spacesBetween * (numberOfComputers-1))]
        self.bits = []
        self.jams = []
        self.collision = self.medium.copy()         # kolizje pzresyłane na innej amplitudzie np zwykły sygnał +- 5V, kolicje +- 15V
        self.col = []
        # transmission_delay = frame_size * timing         # czas potrzebny na wepchnięcie wszytskich bitów z ramki do kabla
        # propagation_delay = medium_length * timimg       # czas potrzebny na przejście początku ramki od nadawny do odbiorcy
        # transmission_delay > 2 * propagation_delay
        # frame_size > 2 * medium_length
        self.minFrameSize = 2 * len(self.medium)
        self.propagation_delay = 2 * len(self.medium) * timing

    def start(self):
        threading.Thread(target=self.propagate).start()

    def propagate(self):
        while True:
            # wyświetl medium
            m = ['_' for m in self.medium]
            for bit in self.bits:
                m[bit[1]] = bit[0]
            for j in self.jams:
                m[j[0]] = 'j'
            self.medium = m
            system('clear')
            stations = [' ' for m in self.medium]
            for i in range(numberOfComputers):
                stations[i * spacesBetween] = i+1
            for s in stations:
                print(s, end="")
            print()
            for m in self.medium:
                print(m, end="")
            print()
            # wyświetl kolizje
            cm = ['_' for c in self.collision]
            if len(self.col) == 2:
                [start, end] = self.col
                for i in range(start, end+1):
                    if 0 <= i < len(self.collision):
                        cm[i] = 'c'
            self.collision = cm
            for c in self.collision:
                print(c, end="")
            print()

            for l in log:
                print(l)

            threading.Thread(target=self.singlePropagate).start()
            time.sleep(timing)

    def singlePropagate(self):
        # sprawdz kolizje
        if len(self.col) == 0:
            self.checkCollisions()

        # przesuń medium
        newbits = []
        for bit in self.bits:
            bit[1] += bit[2]
            if 0 <= bit[1] < len(self.medium):
                newbits.append(bit)
        self.bits = copy.deepcopy(newbits)
        newjams = []
        for jam in self.jams:
            jam[0] += jam[1]
            if 0 <= jam[0] < len(self.medium):
                newjams.append(jam)
        self.jams = copy.deepcopy(newjams)
        jamspos = [i[0] for i in self.jams]
        b = []
        for bit in self.bits:
            if bit[1] not in jamspos:
                b.append(bit)
        self.bits = copy.deepcopy(b)
        # przesuń kolizje
        if len(self.col) == 2:
            [start, end] = self.col
            start -= 1
            end += 1
            if start >= 0 and end < len(self.collision):
                self.col = [start, end]
            elif start < 0 and end < len(self.collision):
                start = 0
                self.col = [start, end]
            elif start >= 0 and end >= len(self.collision):
                end = len(self.collision) - 1
                self.col = [start, end]
            else:
                self.col = []

    def checkCollisions(self):
        if len(self.jams) > 0:
            return False

        diff = {}
        for bit in self.bits:
            if bit[0] != 'j':
                if bit[0] not in diff:
                    diff[bit[0]] = set()
                diff[bit[0]].add(bit[1])
        pos = []
        for i in diff:
            pos += list(diff[i])
        rep = [i for i in pos if pos.count(i) > 1]
        if len(rep) > 5:
            i = rep[0]
            self.col = [i, i]

    def push(self, bit, position):
        # wartość, pozycja, kierunek
        self.bits.append([bit, position, +1])
        self.bits.append([bit, position, -1])

    def get(self, position):
        return self.medium[position]

    def isCollision(self, position):
        return self.collision[position] == 'c' or self.medium[position] == 'j'

    def jam(self, position, length):
        for i in range(length):
            self.jams.append([position, +1])
            self.jams.append([position, -1])
            time.sleep(timing)

class Computer(object):
    def __init__(self, name, position, medium):
        self.name = name
        self.position = position
        self.medium = medium

    def send(self, waiting=0, attempt=0):
        maxAttempt = 5
        frame = ''.join(str(self.name) for i in range(self.medium.minFrameSize))

        # wait until no other comp is transmiting
        while self.medium.get(self.position) != '_' or self.medium.isCollision(self.position):
            time.sleep(random.randint(0, 10) * timing)

        # wait given time (for collisions)
        time.sleep(waiting)

        # wait until no other comp is transmiting
        while self.medium.get(self.position) != '_' or self.medium.isCollision(self.position):
            time.sleep(random.randint(0, 10) * timing)

        # send
        log.append(f'{self.name} started sending message')
        leftToSend = len(frame)
        for bit in frame:
            self.medium.push(bit, self.position)
            if self.medium.isCollision(self.position):
                log.append(f'{self.name} seen collision')
                if attempt < maxAttempt:
                    attempt += 1
                    self.medium.jam(self.position, leftToSend)
                    t = self.medium.propagation_delay * random.randint(0, 2**attempt)
                    log.append(f'{self.name} waits {t} sec, attempt: {attempt}')
                    self.send(t, attempt)
                else:
                    log.append(f'{self.name} tried sending more then {maxAttempt} times, aborts')
                return
            time.sleep(timing)
            leftToSend -= 1
        log.append(f'{self.name} send message succesfully')

if __name__ == '__main__':
    # utworzenie komputerów
    numberOfComputers = 3
    medium = Medium(numberOfComputers)
    computers = []
    for i in range(numberOfComputers):
        position = i * spacesBetween
        name = i + 1
        computers.append(Computer(name, position, medium))
    # uruchomienie
    medium.start()
    time.sleep(1)
    # wysłanie
    threading.Thread(target=computers[0].send).start()
    threading.Thread(target=computers[1].send).start()
    time.sleep(5)
    threading.Thread(target=computers[2].send).start()


import enum
from flask import Flask

from graphql_ambassador import config
from graphql_ambassador.proxy import proxy

__version__ = '0.1.0'

@enum.unique
class Environment(enum.Enum):
    TESTING = enum.auto()
    DEVELOPMENT = enum.auto()
    PRODUCTION = enum.auto()

    @staticmethod
    def from_string(env: str) -> 'Environment':
        return getattr(Environment, env.upper())

CONFIGMAP = {
    Environment.TESTING: config.TestingConfig,
    Environment.DEVELOPMENT: config.DevelopmentConfig,
    Environment.PRODUCTION: config.ProductionConfig,
}

def create_app(environment: Environment) -> Flask:
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(CONFIGMAP[environment])
    app.config.from_pyfile('application.cfg', silent=True)
    app.register_blueprint(proxy)

    return app


import os, sys, json, struct, time
from pathlib import Path

#Inspired from spatialmedia: https://github.com/google/spatial-media

class Box():
    def __init__(self):
        self.header_size = 8
        self.content_size = 0
        self.name = b''
        self.entries = []
    def load(self, f, pos, size):
        pass
    def create(self):
        pass
    def get_data(self):
        pass
    def get_values(self):
        pass
    def get_json_values(self):
        return {
            'name': str(self.name, 'utf-8'),
            'header_size': self.header_size,
            'content_size': self.content_size,
            'entries': self.entries,
            'total_values': len(self.entries),
        }

class StcoBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stco'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        f.seek(pos)
        version_flags = f.read(4)
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            offset = f.read(4)
            offset = struct.unpack(">L", offset)[0]
            self.entries.append(offset)
            d_size += 4
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">L", i)
        return binary
    def get_values(self):
        pass

class Co64Box(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stco'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        version_flags = f.read(4)
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            offset = f.read(8)
            offset = struct.unpack(">Q", offset)[0]
            self.entries.append(offset)
            d_size += 8
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">Q", i)
        return binary
    def get_values(self):
        pass

class SttsBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stts'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        version_flags = f.read(4)
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            t1 = struct.unpack(">L", f.read(4))[0]
            t2 = struct.unpack(">L", f.read(4))[0]
            self.entries.append([t1, t2])
            d_size += 4
            d_size += 4
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">L", i[0])
            binary += struct.pack(">L", i[1])
        return binary
    def get_values(self):
        pass

class StszBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stsz'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        version_flags = f.read(4)
        block_size = f.read(4)
        self.block_size = struct.unpack(">L", block_size)[0]
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            t1 = struct.unpack(">L", f.read(4))[0]
            self.entries.append(t1)
            d_size += 4
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        inary += struct.pack(">L", data['block_size'])
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">L", i)
        return binary
    def get_values(self):
        pass

class StscBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stsc'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        version_flags = f.read(4)
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            t1 = struct.unpack(">L", f.read(4))[0]
            t2 = struct.unpack(">L", f.read(4))[0]
            t3 = struct.unpack(">L", f.read(4))[0]
            self.entries.append([t1, t2, t3])
            d_size += 4
            d_size += 4
            d_size += 4
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">L", i[0])
            binary += struct.pack(">L", i[1])
            binary += struct.pack(">L", i[2])
        return binary
    def get_values(self):
        pass

class StsdBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'stsd'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        version_flags = f.read(4)
        total = f.read(4)
        self.total = struct.unpack(">L", total)[0]
        d_size = self.header_size
        for i in range(0, self.total):
            size = struct.unpack(">L", f.read(4))[0]
            data_format = f.read(4)
            additional_data = f.read(size-8)
            if data_format == b'camm' or data_format == b'gpmd':
                self.entries.append([
                    size,
                    data_format.decode('utf-8', 'backslashreplace'),
                    additional_data.decode('utf-8', 'backslashreplace'),
                ])
            else:
                self.entries.append([
                    size,
                    data_format.decode('utf-8', 'backslashreplace'),
                    additional_data.decode('utf-8', 'backslashreplace'),
                ])
            d_size += 4
            d_size += 4
            d_size += size-8
            if d_size > size:
                break
        self.content_size = d_size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        return binary
    def get_values(self):
        pass

class DinfBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'dinf'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        f.seek(pos)
        d_size = self.header_size
        dsize = struct.unpack(">I", f.read(4))[0]
        name = f.read(4)
        version = struct.unpack(">b", f.read(1))[0]
        flags = f.read(3)
        self.total = struct.unpack(">L", f.read(4))[0]
        data = {
            'size': dsize,
            'name': str(name, 'utf-8'),
            'version': int(version),
            'flags': str(flags, 'utf-8'),
            'data': [],
        }
        for i in range(0, self.total):
            _size = struct.unpack(">I", f.read(4))[0]
            _name = f.read(4)
            version = struct.unpack(">b", f.read(1))[0]
            flags = f.read(3)
            _data = f.read(_size-12)
            if len(data['data']) < 1:
                data['data'] = {}
            data['data'][i] = {
                'size': _size,
                'name': str(_name, 'utf-8'),
                'version': int(version),
                'flags': str(flags, 'utf-8'),
            }
            if _data != b'':
                data['data'][i]['data'] = str(_data, 'utf-8')

        self.entries = data

        self.content_size = size - self.header_size
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        return binary
    def get_values(self):
        pass

class HdlrBox(Box):
    def __init__(self):
        Box.__init__(self)
        self.name = b'hdlr'
        self.total = 0
        self.entries = []
    def load(self, f, pos, size):
        f.seek(pos)
        version_flags = f.read(4)
        component_type = f.read(4)
        component_sub_type = f.read(4)
        component_manufacturer = struct.unpack(">I", f.read(4))[0]
        component_flags = struct.unpack(">I", f.read(4))[0]
        component_flags_mask = struct.unpack(">I", f.read(4))[0]
        component_length = size - self.header_size - (6*4)
        component_name = f.read(component_length)
        self.content_size = component_length + 24
        self.entries = {
            "component_type": str(component_type, 'utf-8'),
            "component_sub_type": str(component_sub_type, 'utf-8'),
            "component_manufacturer": component_manufacturer,
            "component_flags": component_flags,
            "component_flags_mask": component_flags_mask,
            "component_name": str(component_name, 'utf-8'),
        }
    def create(self):
        pass
    @staticmethod
    def get_data(data):
        binary = b''
        binary += struct.pack(">L", 0)
        binary += struct.pack(">L", data['total_values'])
        for i in data['entries']:
            binary += struct.pack(">L", i[0])
            binary += struct.pack(">L", i[1])
        return binary
    def get_values(self):
        pass

__containers = [
    b'moov',
    b'trak',
    b'mdia',
    b'minf',
    b'stbl',
    b'uuid',
    b'wave',
    b'udta',
]

def get_data(atom):
    if atom['name'] == 'stco':
        return StcoBox.get_data(atom['data'])
    elif atom['name'] == 'stts':
        return SttsBox.get_data(atom['data'])
    elif atom['name'] == 'co64':
        return Co64Box.get_data(atom['data'])
    else:
        return b''

def read_childrens(f, pos, fsize, atom):
    f.seek(pos)
    data = None
    if atom == b'stco':
        stco = StcoBox()
        stco.load(f, pos, fsize)
        data = stco.get_json_values()
    elif atom == b'co64':
        co64 = Co64Box()
        co64.load(f, pos, fsize)
        data = co64.get_json_values()
    elif atom == b'stts':
        stts = SttsBox()
        stts.load(f, pos, fsize)
        data = stts.get_json_values()
    elif atom == b'stsz':
        stsz = StszBox()
        stsz.load(f, pos, fsize)
        data = stsz.get_json_values()
    elif atom == b'stsc':
        stsc = StscBox()
        stsc.load(f, pos, fsize)
        data = stsc.get_json_values()
    elif atom == b'stsd':
        stsd = StsdBox()
        stsd.load(f, pos, fsize)
        data = stsd.get_json_values()
    elif atom == b'dinf':
        dinf = DinfBox()
        dinf.load(f, pos, fsize)
        data = dinf.get_json_values()
    elif atom == b'hdlr':
        hdlr = HdlrBox()
        hdlr.load(f, pos, fsize)
        data = hdlr.get_json_values()
    else:
        n = [b'mdat']
        if atom not in n:
            try:
                data = f.read(fsize-8).decode('utf-8', 'backslashreplace')
            except:
                data = None

    return data

def read_atoms(f, pos, fsize):
    childrens = []
    while(pos < fsize):
        box_data = {
            'header_size': 8,
            'name': '',
            'size': 0,
            'type': 'container',
            'position': pos,
            'childrens': [],
            'data': {},
        }
        f.seek(pos)
        size = struct.unpack(">I", f.read(4))[0]
        atom = f.read(4)
        if size < 1:
            break
        if size == 1:
            size = struct.unpack(">Q", f.read(8))[0]
            box_data['header_size'] = 16
        box_data['size'] = size
        try:
            box_data['name'] = str(atom, 'utf-8')
        except:
            box_data['name'] = str(atom)
        pos = f.tell()
        atom_offset = pos + size - box_data['header_size']
        if atom_offset > fsize:
            break
        if atom in __containers:
            try:
                atom = str(atom, 'utf-8')
            except:
                atom = str(atom)
            atom_childrens = read_atoms(f, pos, atom_offset)
            box_data['childrens'] = atom_childrens
            childrens.append({ atom: box_data})
        else:
            box_data['type'] = 'box'
            try:
                _atom = str(atom, 'utf-8')
            except:
                _atom = str(atom)
            data = read_childrens(f, pos, size, atom)
            box_data['data'] = data
            childrens.append({ _atom: box_data})
        f.seek(atom_offset)

        pos = f.tell()

    return childrens

def main():
    video = sys.argv[1]
    vname = Path(video).name.replace(Path(video).suffix, '')
    with open(video, 'rb') as f:
        f.seek(0, 2)
        pos = 0
        fsize = f.tell()
        mp4_st = read_atoms(f, pos, fsize)
        with open('./{}.json'.format(vname), 'w') as jf:
            jf.write(json.dumps(mp4_st, indent=2))

if __name__ == "__main__":
    main()


import datetime
import requests
'''
SEARCHES REDDIT COMMENT SECTIONS FOR STRING VARIBLES, AND RETURNS COMMENTS THAT CONTAIN SAID STRING
EXAMPLE CODE WILL SEARCH R/GUITAR FOR EVERY MENTION OF STRINGS AND PRINT RESULT, ADDITIONAL TERMS CAN BE ADDED TO NARROW SEARCH RESULTS
using pushshift.io api
----------------------------
q = Search term, string
ids = Get specific comments via their ids, Comma-delimited base36 ids
size = Number of results to return, Integer <= 500
fields = One return specific fields
sort = Sort results in a specific order,"asc", "desc"
sort_type = Sort by a specific attribute, "created_utc","score","num_comments", "created_utc"
aggs = Return aggregation summary,["author", "link_id", "created_utc", "subreddit"]
author = Restrict to a specific author, String
subreddit = Restrict to a specific subreddit, String
after = Return results after this date,Epoch value or Integer + "s,m,h,d" (i.e. 30d for 30 days)
before = Return results before this date, Epoch value or Integer + "s,m,h,d" (i.e. 30d for 30 days)
frequency = Used with the aggs parameter when set to created_utc,"second", "minute", "hour", "day"
metadata = display metadata about the
'''
class comment:
    def __init__(self, author, body, subreddit, score, created_utc, parent):
        self.author = author
        self.body = body
        #self.permalink = permalink
        self.subreddit = subreddit
        self.score = score
        self.timestamp = created_utc
        self.parent_id = parent
def get_list():
    global query, size, subreddit, sort_type, sort
    comment_list = []
    api_request = 'https://api.pushshift.io/reddit/search/comment/?q={0}&size={1}&subreddit={2}&sort_type={3}&sort={4}'.format((query),size,subreddit,sort_type,sort)
    client = requests.get(api_request)
    client.close()
    json_response = client.json()['data']

    for item in json_response:
        comment_list.append(comment(item['author'], str.lower(item['body']), item['subreddit'], item['score'], item['created_utc'], item['parent_id']))

    return comment_list
def print_list(list):
    counter = 1
    query_terms = query.split()

    for comment in list:
        timestamp = datetime.datetime.fromtimestamp(comment.timestamp)
        for term in query_terms: # make query terms bold
            if term in comment.body:
                string_start = comment.body.index(term)
                string_end = string_start + len(term)
                comment.body = comment.body[:string_start] + str('\033[1m' + term + '\033[0m') + comment.body[string_end:]

        print('╒═','#',counter,('═'*50),comment.score,'\\',timestamp.date(),sep='')
        print(' ',comment.body)
        counter = counter + 1

query = 'strings' # searched term, required

size = 250 # number of results to return, max 250
subreddit = 'guitar' # subreddit to search
sort = 'desc' # orders results
sort_type = '' # determins where to grab results from, newset,oldest, etc

print_list(get_list())


# -*- coding: utf-8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.chrome.options import Options

def csvGenerator():

    options = Options()
    options.add_argument("--headless")

    driver = webdriver.Chrome("Please insert chrome driver directory here",options=options)

    country=[]#List to store name of the country
    Total_Cases=[] #List to store name of the product
    New_Cases=[] #List to store price of the product
    Total_Death=[] #List to store rating of the product
    driver.get("https://www.worldometers.info/coronavirus/")
    content = driver.page_source
    soup = BeautifulSoup(content, features="lxml")

### GET COUNTRY NAME ####
    for i in soup.findAll('a',href=True, attrs={'class':'mt_a'}):
        soupy = BeautifulSoup(str(i),'html.parser')
        tag = soupy.a
        country.append(tag.string)

### GET Total Cases####

    Temp = []
    for tag in soup.findAll('td',href=False):
        print(tag.string)
        Temp.append(tag.string)

# any(x in a for x in b)
    counter = 0
    for i in range(0,len(Temp)):
        if(Temp[counter] in country):
            Total_Cases.append(Temp[counter+1])
            New_Cases.append(Temp[counter+2])
            Total_Death.append(Temp[counter+3])
        counter = counter+1

#     print(info)
    #print(tag.string)
    # soupy = BeautifulSoup(str(i),'html.parser',store_line_numbers=True)
    # print(str(soupy.sourceline))

# soup = str(soup)
# line_number = soup.split('\n')
# if any():


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 15 09:41:01 2017

@author: Polla
Michigan - Applied DS Course 1 week 2
"""
import pandas as pd
purchase_1 = pd.Series({'Name': 'Chris',
                        'Item Purchased': 'Dog Food',
                        'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
                        'Item Purchased': 'Kitty Litter',
                        'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
                        'Item Purchased': 'Bird Seed',
                        'Cost': 5.00})

df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])

print(df.Cost)
print(df['Item Purchased'])
#df.Cost=df.Cost*0.2
#print(df.Cost)

#df['Cost'] *= 0.8
#print(df)

print("")
print("Querying DF")
print("solution1")
df2=df.where(df['Cost']>3).dropna()
#df2=df2.dropna()
print(df2.Name)
print("solution2")
print(df['Name'][df['Cost']>3])

Michigan - Applied Data Science with python/course 1 - introduction/c1w2- dataFrames.py


#!/usr/bin/env python3

import os
import shutil
import psutil
import socket
import email.message
import smtplib
import sched, time

sch_timer = sched.scheduler(time.time, time.sleep)

def check_disk(disk, min_gb, min_percent):
    """Returns True if there isn't enough disk space, False otherwise."""
    du = shutil.disk_usage(disk)
    percent_free = 100 * du.free / du.total
    gigabytes_free = du.free / 2**30
    if gigabytes_free < min_gb or percent_free < min_percent:
        return True
    return False

def check_memory_reminder(min_mb, max_percent):
    """Returns True if there isn't enough memory space, False otherwise."""
    memory = psutil.virtual_memory()
    percent_load = memory.percent
    megabytes_free = memory.free / 2**20
    if megabytes_free < min_mb or percent_load > max_percent:
        return True
    return False

def check_root():
    """Returns True if the root partition is full, False otherwise."""
    return check_disk(disk="/", min_gb=2, min_percent=20)

def check_cpu():
    """Returns True if the the cpu is having too much usage, False otherwise."""
    return psutil.cpu_percent(1) > 80

def check_memory_overloaded():
    """Returns True if the the memory is having too much usage, False otherwise."""
    return check_memory_reminder(min_mb=500, max_percent=95)

def check_no_network():
    """Returns True if it fails to resolve Google's URL, False otherwise."""
    try:
        socket.gethostbyname("localhost")
        return False
    except:
        return True

def generate_error_report(sender, recipient, subject, body):
    """Creates an simple email"""
    message = email.message.EmailMessage()
    message["From"] = sender
    message["To"] = recipient
    message["Subject"] = subject
    message.set_content(body)
    return message

def send_email(message):
    """Sends the message to the configured SMTP server."""
    mail_server = smtplib.SMTP('localhost')
    mail_server.send_message(message)
    mail_server.quit()

def main(timer):
    checks = [
        (check_root, "Error - Available disk space is less than 20%"),
        (check_cpu, "Error - CPU usage is over 80%"),
        (check_memory_overloaded, "Error - Available memory is less than 500MB"),
        (check_no_network, "Error - localhost cannot be resolved to 127.0.0.1"),
    ]
    health = True
    for check, msg in checks:
        if check():
            sender = "automation@example.com"
            receiver = "{}@example.com".format(os.environ.get('USER'))
            health = False
            email_body = "Please check your system and resolve the issue as soon as possible."
            message = generate_error_report(sender, receiver, msg, email_body)
            send_email(message)

    if not health:
        print("Please check the system, something is wrong!")

    print("Everything ok.")
    timer.enter(60, 1, main, (timer,))

sch_timer.enter(10, 1, main, (sch_timer,))
sch_timer.run()


import os, sys
import slack_config
import json
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

# Slack 토큰 설정
SLACK_TOKEN = slack_config.SLACK_AUTH_CONFIG['token']
client = WebClient(token=SLACK_TOKEN)

# Check if enough arguments are provided
if len(sys.argv) < 6:
    print("Please provide Error Type and Usage(in digits) as arguments.")
    sys.exit(1)

error_type = sys.argv[1]
usage = sys.argv[2]
timestamp = sys.argv[3]
hostName = sys.argv[4]
serverName = sys.argv[5]

print(f"Server Name: {serverName}")
print(f"Error Type: {error_type}")
print(f"Memory Usage: {usage}%")
print(f"Time: {timestamp}")
print(f"Hostname: {hostName}")

# 메시지 내용
if error_type == "cpu" :
    mrkdwn_message = f"*Service :* {serverName} \n*Host :* {hostName}> \n*Display Name :* CPU Usage \n*발생시각 :* {timestamp} \n*장애구분 :* Critical \n*장애메시지 :* CPU 사용률이 {usage} % 입니다 \n"
elif error_type == "memory" :
    mrkdwn_message = f"*Service :* {serverName} \n*Host :* {hostName} \n*Display Name :* Memory Usage \n*발생시각 :* {timestamp} \n*장애구분 :* Critical \n*장애메시지 :* Memory 사용률이 {usage} % 입니다 \n"
elif error_type == "disk" :
    mrkdwn_message = f"*Service :* {serverName} \n*Host :* {hostName} \n*Display Name :* Disk Usage \n*발생시각 :* {timestamp} \n*장애구분 :* Minor \n*장애메시지 :* Disk 사용률 정보 : {usage} \n"

message = [
            {
                "type": "header",
                "text": {
                    "type": "plain_text",
                    "text": "Cloud Server Usage Monitoring",
                    "emoji": True
                }
            },
            {
                "type": "divider"
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": mrkdwn_message
                }
            },
            {
                "type": "divider"
            }
        ]

# 채널에 메시지 보내기
try:
    response = client.chat_postMessage(
        channel = slack_config.SLACK_AUTH_CONFIG['channel_name'],
        text = "alarm_notification",
        blocks = message
    )
except SlackApiError as e:
    print(f"Error: {e.response['error']}")


import os,time, json, subprocess, json
from core import *
import order,datetime, math
import traceback,urllib
from order import *
import ssl
import webserv
ssl._create_default_https_context = ssl._create_unverified_context
tstamp2date=datetime.datetime.fromtimestamp
now=datetime.datetime.now
t_last_oper=now()
#f_data='data.txt'
#f_tmp='tmp.txt'
f_setts='setts.txt'
#upd_last_check=now()

setts=restore(f_setts)
#pairs_pop_queue=[]

def get_float(f,defv=0.0):
    if isinstance(f,str):
        if f=='':
            return defv
        f=f.replace(',','.')
    return float(f)

def save_setts():
    save(f_setts,setts)

def upd_act_prices(p):
    _sell=1+p.profit/100.0
    _buy=1-p.rebuy/100.0

    p.sell_price=_sell*p.last_p
    p.buy_price=_buy*p.last_p

stables=('USDT','USDC','TUSD','BUSD','BTC','ETH','BNB','XRP','TRX')

def get_tok_base(symb, toks):
#    log('ahueli blyadi')
#    log(symb)
    if '/' in symb:
        return symb.split('/')
    if '_' in symb:
        return symb.split('_')

    for x in stables:
        if symb.endswith(x):
            return [symb[:-len(x)],x]

    for x in toks:
        if symb.endswith(x) and (symb[:-len(x)] in toks):
            return [symb[:-len(x)],x]

    #log('cannot split: '+symb)
    return None

def _custom_get(p,pars):
    global t_last_oper
#    line=data
    c=Core(**pars)
    ret=[]
    if (p=='info'):
        tot={}
        cur=setts[c.key]
        t_last_oper=now()
        try:
            cl=get_client(c)
            bal=cl.get_account()
            #log(str(order.prices.items()))
            for key in bal['balances']:
                token=key['asset']
                amo=float(key['free'])+float(key['locked'])
                t_usd=token+'USDT'
                t_busd=token+'BUSD'
                t_btc=token+'BTC'
                b_usd='BTCUSDT'
                usd=0
                if token=='USDT':
                    usd=amo
                elif t_busd in order.prices:
                    usd=amo*order.prices[t_busd]
                elif t_usd in order.prices:
                    usd=amo*order.prices[t_usd]
                else:
                    if t_btc in order.prices:
                        in_btc=amo*order.prices[t_btc]
                        usd=in_btc*order.prices[b_usd]
                    else:
                        usd=0
                tot[token]=Core(usd=usd,amo=amo,name=token)

            tok,bas=get_tok_base('BTCUSDT',tot.keys())
            for n in cur.pairs:
                v=cur.pairs[n]
                #log(str(v))
                if not (isinstance(v.symbol,str) and (v.symbol.strip()!='')):
                    continue
                v.price=order.prices[v.symbol]
                tok,bas=get_tok_base(n,tot.keys())
                v.token=tot[tok]
                v.base=tot[bas]
                ret.append(v)
        except:
            info('error')

        for (symb,price) in order.prices.items():
            if symb in cur.pairs:
                continue
            #_b=bal[symb]
            tbase=get_tok_base(symb,tot.keys())
            if tbase is None:
                continue
            tok,bas=tbase
            try:
                if (bas not in tot) or (tok not in tot):
                    t,b=Core(usd=0,amo=0,name=tok),Core(usd=0,amo=0,name=bas)
                else:
                    t,b=tot[tok],tot[bas]
                pars={'symbol':symb,'price':price,'mode':'none','token':t,'base':b}
                ret.append(pars)
            except:
                info('error')

    return bytes(str(ret),'utf-8')

def _custom_post(p,data):
    #global pairs_pop_queue
    line=data.decode("utf-8")
    log(line)
    pars=dict([r[0],urllib.parse.unquote(r[1])] for r in [x.split('=',1) for x in line.strip().split('&')])
    c=Core(**pars)
    if p=='trade':
        save_acc=order.acc
        save_pair=order.pair
        try:
            order.acc=c
            order.pair=c
            post_order(float(c.amo))
        finally:
            order.pair=save_pair
            order.acc=save_acc
    if p!='upd':
        return
    if str(c.key).strip()=='':
        return
    try:
        cur=setts[c.key]
        token=c.tok
        base=c.bas
        #c.symbol.split('_')
        symbol=c.symbol.replace('_','')
        if c.mode=='none':
            #pairs_pop_queue.append(symbol)
            cur.pairs.pop(symbol)
        else:
            cur.key=c.key
            cur.secret=c.secret
            cur.tg_token=c.tg_token
            cur.tg_chat_id=c.tg_chat_id
            c.pop('secret')
            c.pop('key')
            c.pop('tg_token')
            c.pop('tg_chat_id')
            v=cur.pairs[symbol]
            v.symbol=symbol
            v.token=Core(usd=0,amo=0,name=token)
            v.base=Core(usd=0,amo=0,name=base)
            v.mode='auto'
            v.profit=get_float(c.profit)
            v.rebuy=get_float(c.rebuy)
            v.act_amo=get_float(c.act_amo)
            #log(c.price)
            v.last_p=get_float(c.last_p)
            upd_act_prices(v)
        save_setts()
    except:
        error()
    return b''#bytes(p+data,'utf-8')

def as_secs(time_delta):
    return time_delta.seconds+(time_delta.microseconds/1e6)

def main():
    global stop_time,t_last_oper
#    while 1:
#        yield
#    return
    t_last_oper=now()
    t_last_req=now()
    _now=now()
    stop_time=_now+datetime.timedelta(minutes = 13)
    iter_cnt=0
    time_koef=2
    upd_prices()
    while _now<stop_time:
        iter_cnt+=1
        #if iter_cnt%10==0:
        #    log('iter=%d'%iter_cnt)
        while as_secs((now()-t_last_req))<time_koef:
            yield
        upd_prices()
        t_last_req=now()
        accs=[x for x in setts]
        for acc_name in accs:
            acc=setts[acc_name]
            order.acc=acc
            symbs=[x for x in acc.pairs]
            for symbol in symbs:
                try:
                    pair=acc.pairs[symbol]
                    order.pair=pair
                    p=price()
                    if not pair.last_p:
                        pair.last_p=p

                    #log(symbol+':secs='+str(as_secs((now()-t_last_oper))))
                    #log('mlya %s<%s<%s:'%(pair.buy_price,p,pair.sell_price))
                    if (p>pair.buy_price) and (p<pair.sell_price):
                        continue

                    #log(symbol+':try secs')
                    while as_secs((now()-t_last_oper))<1*time_koef:
                        yield

                    if symbol not in acc.pairs:
                        continue #probably was removed

                    #log(symbol+':anal')
                    t_last_oper=now()
                    p_koef=p/pair.last_p
                    std_base_amo=std_base_amount(pair.base.name)
                    std_amo=std_base_amo/p
                    bal=get_balances()
                    amo=bal.token.free*(pair.act_amo/100)
                    #amo=max(amo,std_amo)
                    #log('che za her:'+str(p)+';'+str(pair.buy_price))
                    #log(symbol+'cmp')
                    if (p>pair.sell_price):
                        _sell=1+pair.profit/100.0
                        amo=amo*math.log(p_koef,_sell)*max(pair.profit/pair.rebuy,1)/_sell
                        #log(symbol+': Free='+str(bal.token.free)+'Amo='+str(amo))
                        if bal.token.free<amo:
                            continue
                        amo=-amo
                    elif (p<pair.buy_price):
                        if bal.base.free<std_base_amo*1.5:
                            continue
                        _buy=1-pair.rebuy/100.0
                        amo=amo*math.log(p_koef,_buy)*max(pair.rebuy/pair.profit,1)/_buy
                        if bal.base.free<amo*p:
                            continue
                        #amo=max(min(bal.base.free/2,amo*p),std_base_amo)/p
                    else:
                        continue
                    #if bal.token.free<std_amo:
                    #    continue
                    #need to move this part (split small order <10$ on 2) into post_order
                    amo1=0
                    amo2=0

                    if abs(amo)<std_amo:
                        safe_koef=1.2
                        if amo<0:
                            if bal.base.free>std_base_amo*safe_koef:
                                #buy std
                                amo1=std_amo
                                #sell sum std+amo. reminder amo with minus
                                amo2=amo-std_amo
                            elif bal.token.free>(abs(amo)+std_amo)*safe_koef:
                                #sell sum std+amo. reminder amo with minus
                                amo1=amo-std_amo
                                #buy std
                                amo2=std_amo
                        elif amo>0:
                            if bal.base.free>(std_base_amo+amo*p)*safe_koef:
                                #buy sum
                                amo1=std_amo+amo
                                #sell std
                                amo2=-std_amo
                            elif bal.token.free>amo*safe_koef:
                                #sell std
                                amo1=-std_amo
                                #buy sum
                                amo2=std_amo+amo
                        else:
                            continue
                    if amo1 or amo2:
                        #small amo order, splitted on 2 orders. (min order size : limitation of exchange)
                        post_order(amo1)
                        post_order(amo2)
                    else:
                        post_order(amo)
                    sMsg='%s:%.2f$(%.8f); %s'%(symbol,amo*p,amo,acc_name[:10])
                    info(sMsg)
                    pair.last_p=p
                    upd_act_prices(pair)
                    t_last_oper=now()
                    save_setts()
                except:
                    error()

        _now=now()
        t_last_req=_now
        yield
        #time.sleep(1)
    webserv.shutd=True
#exit(0)
try:
    log('Init app.')
    webserv.StartServer(host='localhost',user_func=main,get_custom_handler=_custom_get,post_custom_handler=_custom_post)
    save_setts()
except:
    error()

p = subprocess.Popen(['python3','cat.py'], start_new_session=True)
#os.startfile('python3 cat.py &')


#!/usr/bin/env python3
# Written by Itai Ganot 2018
"""This script can:
1. List all AWS SSM parameters and their values in a given KMS id
(staging / production / integration)
Example:
./ssm_manage.py -r us-east-1 -e staging
2. Go through a file and get the values for all included keys
Example:
./ssm_manage.py -r us-east-1 -f some_creds.txt
3. Return a specific key per a given value
Example:
./ssm_manage.py -r us-east-1 -v some_keys_value
4. Replace single / multiple parameters with a given value
Example:
./ssm_manage.py -k cred_test_itai -r us-east-1 -p aaa
5. Save the results to file or print to screen
Example:
./ssm_manage.py -r us-east-1 -e production -w output-file
6. Count the number of parameters per given environment
Example:
./ssm_manage.py -r us-east-1 -e production -c
"""

import boto3
import argparse
import sys
import os
from subprocess import Popen, PIPE
from botocore.exceptions import ClientError

def count_params_per_env(env):
    final_keyid = 'alias/param_key_{}'.format(env)
    command = 'aws ssm describe-parameters | \
        jq -r \'.Parameters[] | ' \
        'select(.KeyId=="{}") | \
        .Name\' | wc -l'.format(final_keyid)
    proc = Popen(command, shell=True, stdout=PIPE, encoding='utf8')
    output = proc.communicate()[0]
    print('Number of params in environment {}: {}'.format(
        env,
        output.strip()
    ),end="")
    sys.exit()

def get_all_params_names(ssmclient, env, write_out):
    final_keyid = 'alias/param_key_{}'.format(env)
    print('Working on env: {}'.format(env))
    marker = None
    count = 0
    paginator = ssmclient.get_paginator('describe_parameters')
    page_iterator = paginator.paginate(Filters=
    [{
        'Key': 'KeyId',
        'Values': [final_keyid]
    }],
        PaginationConfig={
            'PageSize': 10,
            'NextToken': marker
        })

    for page in page_iterator:
        if not page['Parameters']:
            break
        else:
            for param in page['Parameters']:
                count += 1
                param_value = ssmclient.get_parameter(
                    Name=param['Name'],
                    WithDecryption=True)['Parameter']['Value']
                message = '#{} Name: {} | Value: {} | KeyId: {}'.format(
                    count,
                    param['Name'],
                    param_value,
                    final_keyid)
                if write_out != None:
                    with open(write_out, 'w+') as f:
                        f.write(message + '\n')
                else:
                    print(message)
    print('Finished processing {} parameters in KMS {}'.format(
        count,
        final_keyid))

def replace_parameter_value(ssmclient, key, put_value):
    try:
        ssmclient.put_parameter(Name=key,
                                Value=put_value,
                                Type='SecureString',
                                Overwrite=True)
    except Exception as e:
        print(e)
    else:
        print('Key {} successfully updated!'.format(key.strip()))

def get_key_of_value(ssmclient, value, write_out):
    marker = None
    count = 0
    param_keys = []
    paginator = ssmclient.get_paginator('describe_parameters')
    page_iterator = paginator.paginate(PaginationConfig={
        'PageSize': 10,
        'NextToken': marker
    })
    for page in page_iterator:
        if not page['Parameters']:
            break
        else:
            for param in page['Parameters']:
                count += 1
                param_name = param['Name']
                message = '#{} Now processing parameter: {}'.format(
                    count,
                    param_name)
                if write_out != None:
                   write_out_to_file(write_out, message)
                else:
                    print(message)
                    param_value = ssmclient.get_parameter(Name=param_name,
                                            WithDecryption=True)['Parameter']
                    if param_value['Value'] == value:
                        param_keys.extend(param_value['Name'])
                        print('Value {} found in parameter {}'.format(
                            value,
                            param_name))

def get_specific_param_value(ssmclient, search_value):
    try:
        specific_param_value = ssmclient.get_parameter(
            Name=search_value,
            WithDecryption=True)['Parameter']
        message = "Name: {} | Value: {} ".format(
            search_value,
            specific_param_value['Value'])
        print(message)
        return specific_param_value
    except Exception as e:
        print('No such parameter found: {}, Error: {}'.format(
            search_value,
            e))

def load_file(params_list):
    with open(params_list, 'r', encoding='ascii') as f:
        return f.readlines()

def create_resource_instance(region):
    ssmclient = boto3.client('ssm', region_name=region)
    return ssmclient

def write_out_to_file(write_out, text):
    if write_out != None:
        is_file = os.path.isfile('{}'.format(write_out))
        if is_file:
            try:
                with open(write_out, 'a+') as f:
                    f.writelines(text + '\n')
            except Exception as e:
                print(e)
        else:
            try:
                with open(write_out, 'x') as f:
                    f.writelines(text + '\n')
            except Exception as e:
                print(e)

def main(env,
         key,
         parameters_file,
         region,
         put_value,
         value,
         output_file,
         count_params):
    ssmc = create_resource_instance(region)
    if count_params and env != '':
        count_params_per_env(env)
    if env and output_file:
        get_key_of_value(ssmc, value, output_file)
    if parameters_file:
        try:
            a = load_file(parameters_file)
            for param in a:
                get_specific_param_value(ssmc, param.strip())
        except FileNotFoundError as e:
            e = str(e)
            print(e[10:])
    if value and output_file:
        get_key_of_value(ssmc, value, output_file)
    if value:
        get_key_of_value(ssmc, value, output_file)
    if env:
        try:
            get_all_params_names(ssmc, env, output_file)
        except ClientError as e:
            if e.response['Error']['Code'] == 'UnrecognizedClientException':
                print('No Access Key configured!')
    if put_value and key:
        replace_parameter_value(ssmc, key, put_value)
    if put_value and parameters_file:
        a = load_file(parameters_file)
        for param in a:
            replace_parameter_value(ssmc, param, put_value)
    if key:
        get_specific_param_value(ssmc, key)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='script to manage AWS SSM params')
    mutual_group = parser.add_argument_group('Parameters display options')
    mutually_exclusive = mutual_group.add_mutually_exclusive_group()
    mutually_exclusive.add_argument('-e',
                                    '--environment',
                                    help='environment')
    mutually_exclusive.add_argument('-k',
                                    '--key',
                                    help='key of value to search')
    mutually_exclusive.add_argument('-f',
                                    '--parameters-file',
                                    help='paramater list to search')
    mutually_exclusive.add_argument('-v',
                                    '--value',
                                    help='Key of value to search')
    parser.add_argument('-r',
                        '--region',
                        help='region',
                        required=True)
    parser.add_argument('-p',
                        '--put_value',
                        default='',
                        help='put parameter')
    parser.add_argument('-w',
                        '--write-out',
                        help='Save results to a local file')
    parser.add_argument('-c',
                        '--count-params',
                        action='store_true',
                        help='count params')
    args = parser.parse_args()
    try:
        main(args.environment,
             args.key,
             args.parameters_file,
             args.region,
             args.put_value,
             args.value,
             args.write_out,
             args.count_params)
    except KeyboardInterrupt as e:
        print('\rYou pressed ctrl c')
        sys.exit(1)


C,R = map(int,input().split())
K = int(input())
lst = [[0]*R for i in range(C)]
diry = [0,1,0,-1]
dirx = [1,0,-1,0]
now = 0
dy=0
dx=0
for i in range(1,C*R+1):
    lst[dy][dx]=i
    dx+=dirx[now]
    dy+=diry[now]
    if dx<0 or dy <0 or dx>=R or dy>=C or lst[dy][dx]!=0:
        dy -=diry[now]
        dx -=dirx[now]
        now+=1
        now = now%4
        dy += diry[now]
        dx += dirx[now]
def abc (lst,k):
    for i in range(C):
        for j in range(R):
            if lst[i][j] == k:
                return i+1,j+1
    return [0]

print(*abc(lst,K))


from fastapi import FastAPI, Request, HTTPException, WebSocket, Form, Query
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from typing import List
from fastapi.websockets import WebSocketDisconnect
import json
from urllib.parse import unquote
import asyncio
import uuid

app = FastAPI()
templates = Jinja2Templates(directory="web")
site_name = "Alex-website"
app.mount("/images", StaticFiles(directory="web/images"), name="images")
app.mount("/libs", StaticFiles(directory="web/libs"), name="libs")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

clients = []

# active_connections = []
websocket_connections: List[WebSocket] = []
websocket_connections_name = []

@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
    progress_bar = f'''

                50%

    '''
    img = f'''

            '''
    cards = [
        {"color": "#215577", "title": "Envoyer l'image à l'écran", "text": progress_bar, "button":"true", "button_text":"Go","href":""},
        {"color": "#217777", "title": "Envoyer l'image à l'écran", "text": img, "button":"true", "button_text":"Go","href":""},
        {"color": "#218877", "title": "Envoyer l'image à l'écran", "text": "This card has supporting text below as a natural lead-in to additional content.", "button":"true", "button_text":"Go","href":""},
        {"color": "#212e77", "title": "Com_connection", "text": "This card has supporting text below as a natural lead-in to additional content.", "button":"true", "button_text":"Go","href":"Com"},
        {"color": "#218877", "title": "Chatbox", "text": "This card has supporting text below as a natural lead-in to additional content.", "button":"true", "button_text":"Go","href":"chatwebsocket"},

    ]
    navbar_content = [
        {
            "type": "dropdown",
            "class": "nav-link dropdown-toggle",
            "text": "Menu principal",
            "items": [
                {"class": "dropdown-item", "text": "Sous-menu 1", "href": "#"},
                {"class": "dropdown-item", "text": "Sous-menu 2", "href": "#"},
            ],
        },
        {
            "type": "dropdown",
            "class": "nav-link dropdown-toggle",
            "text": "Autre menu",
            "items": [
                {"class": "dropdown-item", "text": "Sous-menu 3", "href": "#"},
            ],
        },
        {
            "type": "nav-link",
            "class": "nav-item",
            "text": "Autre menu",
         },
        {
            "type": "search"
        },
        {
            "type": "text",
            "text": "Texte"
        },
        {
            "type": "button",
            "class": "btn btn-danger",
            "text": "Autre menu",
            "onclick": "alert('ooo')",
        },
    ]

    disposition = [2,3]
    num_rows = len(disposition)
    total_elements = len(cards)

    if sum(disposition) != total_elements:
        raise ValueError("La somme de la disposition des éléments ne correspond pas au nombre total d'éléments.")

    index = 0
    data = {"title": "Accueil", "content": "Ceci est la page d'accueil.", "num_rows": num_rows,"navbar_content": navbar_content,"disposition":disposition}

    data["rows"] = []

    for row_size in disposition:
        row = []
        for _ in range(row_size):
            row.append(cards[index])
            index += 1
        data["rows"].append(row)

    # data = {"title": "Accueil", "content": "Ceci est la page d'accueil.", "cards": cards, "num_columns": num_columns, "num_rows": num_rows,"navbar_content": navbar_content}
    return templates.TemplateResponse("templates/index.html", {"request": request, **data})

@app.get("/chatwebsocket", response_class=HTMLResponse)
async def chatwebsocket(request: Request):
    return templates.TemplateResponse("templates/chatwebsocket.html", {"request": request })

@app.get("/cam", response_class=HTMLResponse)
async def cam(request: Request):
    return templates.TemplateResponse("templates/cam.html", {"request": request })

@app.get("/Com", response_class=HTMLResponse)
async def Com(request: Request):
    return templates.TemplateResponse("templates/Com_connection.html", {"request": request })

@app.get("/producer", response_class=HTMLResponse)
async def producer(request: Request):
    return templates.TemplateResponse("templates/producer.html", {"request": request })

@app.get("/client", response_class=HTMLResponse)
async def producer(request: Request):
    return templates.TemplateResponse("templates/client.html", {"request": request })

@app.get("/get_connections", response_model=dict)
async def get_connections():
    connections = await ws_list()
    return {"connections": connections}

@app.post("/send_message_to")
async def send_message_to(message: str = Form(...), id: str = Form(...), who: str = Form(...)):
    print(id)
    if id=='Broadcast':
        connections = await broadcast_message(message)
    else:
        connections = await send_message_to_id(message,int(id),who)
    return {"connections": connections}

@app.websocket("/stream")
async def stream(websocket: WebSocket):
    await websocket.accept()
    while True:
        blob = await websocket.receive_bytes()
        await websocket.send_bytes(blob)

@app.websocket("/ttt/{producer_id}")
async def websocket_endpoint(websocket: WebSocket, producer_id: str):
    await websocket.accept()
    producer_id = str(uuid.uuid4())
    active_connections[producer_id] = websocket
    try:
        while True:
            data = await websocket.receive_text()
            for connection in active_connections.values():
                if connection != websocket:
                    await connection.send_text(f"Producer {producer_id}: {data}")
    except Exception:
        pass
    finally:
        del active_connections[producer_id]

@app.websocket("/ws/{pseudo}")
async def websocket_endpoint(websocket: WebSocket, pseudo: str):
    await websocket.accept()
    websocket_connections.append(websocket)
    websocket_connections_name.append(pseudo)
    try:
        while True:
            data = await websocket.receive_text()
            connections = [str(connection) for connection in websocket_connections]
            elements = data.split('&')
            data_dict = {}
            for element in elements:
                key, value = element.split('=')
                if '?' in key:
                    url, key = key.split('?', 1)
                value = value.replace('%28', '(')
                value = value.replace('+',' ')
                value = unquote(value)
                data_dict[key] = value
            if data_dict['id'] in websocket_connections_name:
                index = websocket_connections_name.index(data_dict['id'])
                data_dict['id'] = index

            data_dict['message'] = data_dict['message'].replace('+',' ')
            data_dict['message'] = unquote(data_dict['message'] )
            if data_dict['id']=='Broadcast':
                connections = await broadcast_message(data_dict['message'])
            else:
                connections = await send_message_to_id(data_dict['message'],int(data_dict['id']),data_dict['who'])
    except WebSocketDisconnect:
        index = websocket_connections.index(websocket)
        websocket_connections_name.pop(index)
        websocket_connections.remove(websocket)

async def ws_list():
    connections = [str(connection) for connection in websocket_connections_name]
    return {"connections": connections}

async def broadcast_message(message: str):
    for connection in websocket_connections:
        await connection.send_text(message.replace('+',' '))

async def send_message_to_id(message, index, who):
    message = who + " say : " + message
    if 0 <= index < len(websocket_connections):
        try:
            await websocket_connections[index].send_text(message)
        except WebSocketDisconnect:
            print("WebSocket connection closed.")
    else:
        print(f"Invalid index: {index}")

@app.get("/{path:path}", response_class=HTMLResponse)
async def not_found(request: Request):
    try:
        raise HTTPException(status_code=404, detail="Page not found")
    except HTTPException as exc:
        error_code = exc.status_code

    data = {"title": "Erreur " + str(error_code),"status_code":error_code}
    return templates.TemplateResponse("templates/Error_page.html", {"request": request, **data})


'''
	Converts the .json file to .conll format
	Author: Sandesh Timilsina
	Date: 02/07/2019

	How to run:
	- Pass the all_channels.json file as command line argument.
    Example:
        >> python json_to_conll.py -file ../../brat/data/nepsa/all_channels.json
'''

import os
import sys
import argparse
import pandas as pd
import json

def read_jsonfile(file):
    with open(file) as f:
        data = json.load(f)
    return data

def re_label(comment, item, df):
    tags = item['tags']
    for tag in tags:
        if 'entity' in tag:
            entity = tag['entity']
            entity_from = tag['entity_from']
            entity_to = tag['entity_to']
            entity_cat = tag['entity_cat']
            entity_ls = entity.split()
            begin = True

            for item in entity_ls:
                idx = df[df[0]==item].index.tolist()
                for i in range(len(idx)):
                    if df.loc[idx[i], 3]=='O' and entity_from>=df.loc[idx[i], 1] and entity_from<=df.loc[idx[i]]:
                        if begin:
                            df.loc[idx[i], 3]= 'B-'+entity_cat
                            begin= False
                        else:
                            df.loc[idx[i], 3]= 'I-'+entity_cat

        if 'aspect' in tag:
            aspect = tag['aspect']
            aspect_from = tag['aspect_from']
            aspect_to = tag['aspect_to']
            aspect_cat = tag['aspect_cat']
            aspect_ls = aspect.split()
            begin = True
            for item in aspect_ls:
                idx = df[df[0]==item].index.tolist()
                for i in range(len(idx)):
                    if df.loc[idx[i], 3]=='O' and df.loc[idx[i], 1]>=aspect_from and \
                        df.loc[idx[i], 2]<=aspect_to:
                        if begin:
                            df.loc[idx[i], 3]= 'B-'+aspect_cat
                            begin = False
                        else:
                            df.loc[idx[i], 3]= 'I-'+aspect_cat
    return df

def text_to_conll(data, file_path):
    df = None
    final_df = pd.DataFrame()
    count=0
    for item in data:
        count+=1
        df = pd.DataFrame()
        comment = item['comment']
        print(item['channel'], item['video_id'], item['comment_id'])
        begin = 0
        end = 0
        for word in comment.split():
            end = begin+len(word)
            df = df.append(pd.Series([word,begin,end,'O']), ignore_index=True)
            end+=1
            begin = end
        final_df = re_label(comment, item, df)
        final_df = final_df.append(pd.Series([""]), ignore_index=True)
        output_file = file_path.rpartition('.')[0]+'.conll'
        write_file(output_file, final_df)
        # final_df.to_csv(output_file, header=None, index=None, sep='\t', mode='a')
    print('Done! ',count,' comments converted to conll format')

def write_file(filename, df):
    with open(filename, 'a') as f:
        for idx, row in df.iterrows():
            if not row.all()=="":
                f.write(str(row[0])+"\t"+row[3]+"\n")
            else:
                f.write("\n")

def json_to_conll(file_path):
    data = read_jsonfile(file_path)
    text_to_conll(data, file_path)

def main(argv):
    parser = argparse.ArgumentParser(add_help=True, description=('Json to Conll converter'))
    parser.add_argument('-file', default='../../data/nepsa/al_channels.json', help='Input json file')

    args = parser.parse_args(argv)
    input_file = args.file

    if not os.path.exists(input_file) or not input_file.endswith(".json"):
        raise ValueError('Invalid Input Folder')

    json_to_conll(input_file)
    print(' Successfully completed')

if __name__ == "__main__":
    main(sys.argv[1:])


# -*- mode: python -*-

block_cipher = None

a = Analysis(['core.py'],
             pathex=[r"C:\Users\Иван\Desktop\astrlords", r"C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\x64", r"C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\x86"],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='core',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='core')


import requests
from bs4 import BeautifulSoup
import random
import time

"""
代理使用：

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)
"""

# 此处代理ip下载速度极慢，建议换速度快的，或者设置为自己的本地IP
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)'
headers = {'User-Agent': user_agent}
proxyList = []

def getListProxies():
    global proxyList
    session = requests.session()
    for i in range(1, 21):
        time.sleep(random.randint(1, 10))
        ip_url = r"http://www.kuaidaili.com/free/outha/" + str(i)
        page = session.get(ip_url, headers=headers)
        soup = BeautifulSoup(page.text, 'html.parser')
        taglist = soup.find('tbody').find_all('tr')
        for trtag in taglist:
            tdlist = trtag.find_all('td')
            if tdlist[6].string.find(r"2016") >= 0:
                proxy = {'http': tdlist[0].string + ':' + tdlist[1].string,
                         'https': tdlist[0].string + ':' + tdlist[1].string}
                url = "http://ip.chinaz.com/getip.aspx"  # 用来测试IP是否可用的url
                try:
                    response = session.get(url, proxies=proxy, timeout=3)
                    proxyList.append(proxy)
                    print("有效ip个数: " + len(proxyList) + "个")
                    if len(proxyList) == 10:
                        break
                except Exception as e:
                    print("第" + str(i) + "页：" + tdlist[0].string + ':' + tdlist[1].string + " 超时")
                    continue
    return proxyList

def main():
    proxyList = getListProxies()
    print(proxyList)

if __name__ == '__main__':
    main()


from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
import html
from flask import Flask, request, jsonify, render_template, Response,redirect
from datetime import time
import json
import sqlite3
import numpy as np
import csv
import os

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///incidents.sqlite")

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
Base.classes.keys

# # Save reference to the table
Incidents = Base.classes.Incidents

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

filePath = './static/data/bluered.csv'

with open('./static/json/election.json', 'r') as f:
    table1 = json.loads(f.read())
#
@app.route('/election.json', methods=['GET'])
def election():
    return jsonify(table1)

with open('./static/json/gz_2010_us_040_00_500kjm.json', 'r') as f:
    table2 = json.loads(f.read())
#
@app.route('/gz_2010_us_040_00_500kjm.json', methods=['GET'])
def geo():
    return jsonify(table2)

with open('./static/json/state_data.geojson', 'r') as f:
    table3 = json.loads(f.read())
#
@app.route('/state_data.geojson', methods=['GET'])
def geo2():
    return jsonify(table3)
#################################################
# Flask Routes
#################################################

@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"Available Routes:
"
        f"/api/state_data
"
        f"/Graphic1
"
    )

@app.route("/api/state_data")
def data():
    # Create our session (link) from Python to the DB
    session = Session(engine)

    """Return a list of all State data"""
    # Query all state data
    results = session.query(Incidents.State, Incidents.FirstYear, Incidents.LastYear, \
    Incidents.InjuryIncident, Incidents.TotalDeaths, Incidents.AvgYearlyDeaths, \
    Incidents.Population, Incidents.Rate).all()
    session.close()

    data = list(np.ravel(results))

        # Create a dictionary from the row data and append to a list of all_passengers
    all_states = []
    for state, f_year, l_year, injury, t_deaths, y_deaths, population, rate in results:
        state_dict = {}
        state_dict["State"] = state
        state_dict["FirstYear"] = f_year
        state_dict["LastYear"] = l_year
        state_dict["InjuryIntent"] = injury
        state_dict["TotalDeaths"] = t_deaths
        state_dict["AvgYearlyDeaths"] = y_deaths
        state_dict["Population"] = population
        state_dict["Rate"] = rate
        all_states.append(state_dict)
        print(all_states)

    # return jsonify(all_states)
    return jsonify(data)

@app.route("/Graphic1")
def graphic1():
    return render_template("Graphic1.html")
@app.route("/Graphic2")
def graphic2():
    return render_template("Graphic2.html")
@app.route("/Graphic3")
def graphic3():
    return render_template("Graphic3.html")
@app.route("/home")
def home():
    return render_template("Gun-Violence-In-The-USA_Home.html")
if __name__ == '__main__':
    app.run(debug=True)