"""Test utils for rule generator scanners."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import yaml

from deploy.rule_generator import project_config

_OVERALL_DICT = {
    'domain': 'domain.com',
    'organization_id': '246801357924',
    'folder_id': '357801357924',
    'billing_account': '012345-6789AB-CDEF01',
    'allowed_apis': ['compute.googleapis.com', 'storage.googleapis.com'],
}

_PROJECT_YAML = """
projects:
- project_id: %(project_id)s
  owners_group: %(project_id)s-owners@domain.com
  auditors_group: %(project_id)s-auditors@domain.com
  data_readwrite_groups:
  - %(project_id)s-readwrite@domain.com
  data_readonly_groups:
  - %(project_id)s-readonly@domain.com
  - %(project_id)s-readonly-external@domain.com
  data_buckets:
  - name_suffix: '-bucket'
    location: US-CENTRAL1
    storage_class: REGIONAL
  gce_instances:
  - name: 'instance'
    zone: 'us-central1-f'
    machine_type: 'n1-standard-1'
    existing_boot_image: 'projects/debian-cloud/global/images/family/debian-9'
    start_vm: true
  audit_logs:
    logs_gcs_bucket:
      location: US
      storage_class: MULTI_REGIONAL
      ttl_days: 365
    logs_bigquery_dataset:
      location: US
"""

_GENERATED_FIELDS = """
generated_fields:
  forseti:
    service_account: forseti@sample-forseti.iam.gserviceaccount.com
    server_bucket: gs://forseti-project-server/
  projects:
    %(project_id)s:
      project_number: %(project_num)s
      log_sink_service_account: audit-logs-bq@logging-%(project_num)s.iam.gserviceaccount.com
      gce_instance_info:
      - name: 'instance'
        id: '123'
    forseti-project:
      project_number: 9999
      log_sink_service_account: forseti-logs@logging-9999.iam.gserviceaccount.com
"""

def create_test_global_config():
  """Creates a global config dictionary for tests."""
  return _OVERALL_DICT.copy()

def create_test_project(project_id, project_num, extra_fields=None,
                        audit_logs_project=None):
  """Create a test project with optional extra project_config fields."""
  config_dict = yaml.load(_PROJECT_YAML % {
      'project_id': project_id, 'project_num': project_num})
  project = config_dict['projects'][0]
  generated_fields = yaml.load(_GENERATED_FIELDS % {
      'project_id': project_id,
      'project_num': project_num
  })
  if extra_fields:
    project.update(extra_fields)
  print(generated_fields)
  print('xxxxxxxxxxxxxxxxxxxxxx')
  return project_config.ProjectConfig(
      project=project,
      audit_logs_project=audit_logs_project,
      generated_fields=generated_fields['generated_fields'])

def create_test_projects(num_projects):
  """Create a list of test projects."""
  return [
      create_test_project(project_id='project-%s' % i, project_num=1000000 + i)
      for i in range(num_projects)
  ]


import numpy as np
import json
import sqlite3
import pandas as pd
import copy
import os
from pathlib import Path

# Part I: Get all the possible routes .................................................................

class routes():
    def __init__(self, universe, departure, arrival, fuel, count):

        planets = np.unique(np.concatenate((universe.origin, universe.destination))).tolist()
        self.neigh = {}
        planets.remove(arrival)

        for x in planets:
            self.neigh[x] = pd.concat([universe[universe.origin==x].iloc[:,1:3].set_axis(['neighbors','time'], axis=1),
                       universe[universe.destination==x].iloc[:,[0,2]].set_axis(['neighbors','time'], axis=1)])
            self.neigh[x]['fuel'] = [0]*self.neigh[x].shape[0]
            self.neigh[x] = self.neigh[x].append({'neighbors':x, 'time':1, 'fuel':6}, ignore_index=True)
            self.neigh[x].index = self.neigh[x].neighbors

        self.routes = [] ; self.route = {'route':[[0,departure]], 'fuel':fuel, 'count':count}
        self.departure = departure ; self.arrival = arrival ; self.count = count

    def paths(self):

        for go in self.neigh[self.departure].neighbors:
            pathact = copy.deepcopy(self.route)
            self.nextPlanet(pathact, self.departure, go)
        return self.routes

    def nextPlanet(self, path, leave, go):
        if (path['count']>=self.neigh[leave].loc[go, 'time']):

            if ((path['fuel'] >= self.neigh[leave].loc[go, 'time']) or
                (self.neigh[leave].loc[go, 'fuel']!=0)):
                path['count'] = path['count'] - self.neigh[leave].loc[go, 'time']
                path['route'].append([self.count - path['count'], go])
                path['fuel'] = min(6, path['fuel']
                                   - self.neigh[leave].loc[go, 'time']*(self.neigh[leave].loc[go, 'fuel']==0)
                                   + self.neigh[leave].loc[go, 'fuel'])

                if (go != self.arrival):
                    leave = go
                    for go in self.neigh[leave].neighbors:
                        pathact = copy.deepcopy(path)
                        self.nextPlanet(pathact, leave, go)
                else: self.routes.append(path)

# Part II: Get the odds ..................................................................................

class odds():
    def __init__(self, routes, hunting):
        self.routes = routes ; self.hunting = hunting ; self.odds = []

    def odd(self):
        for route in self.routes:
            k = 0
            for r in route['route']:
                if ((r[0] in self.hunting['day']) and (self.hunting['planet'][self.hunting['day'].index(r[0])]==r[1])):
                    k = k+1
                    r.append('BH')
                else: r.append('Clear')
            route['odd'] = (1-sum([(9**(i-1))/(10**i) for i in range(1, k+1)]))*100
            self.odds.append(route['odd'])
        return self.routes, np.array(self.odds)

# Part III: Let's run it all ..............................................................................

class calcul():
    def __init__(self, milleniumFile, empireFile):
        # Read the routes
        with open(milleniumFile, encoding='utf-8') as json_file:
            self.backData1 = json.load(json_file)
        cnx = sqlite3.connect(os.path.join(Path(milleniumFile).parent.absolute(), self.backData1['routes_db']))
        table = cnx.cursor().execute("SELECT name FROM sqlite_master WHERE type='table';")
        self.universe = pd.read_sql_query('SELECT * FROM {}'.format(''.join(table.fetchone())), cnx)
        self.universe = self.universe.set_axis(['origin', 'destination', 'time'], axis=1)

        # Read the hunting
        with open(empireFile, encoding='utf-8') as json_file:
            self.backData2 = json.load(json_file)

        self.hunting = {'planet':[], 'day':[]}
        for h in self.backData2['bounty_hunters']:
            planet, day = h.values()
            self.hunting['planet'].append(planet) ; self.hunting['day'].append(day)

        self.routes = routes(self.universe, self.backData1['departure'], self.backData1['arrival'], self.backData1['autonomy'], self.backData2['countdown']).paths()
        self.routes, self.odds = odds(self.routes, self.hunting).odd()

        self.res = [0]

    def result(self):

        if (len(self.odds) != 0):
            self.res[0] = max(self.odds) ; self.res.append([])
            indices = np.where(self.odds == max(self.odds))[0].tolist()
            for i in indices:
                route = []
                prev = self.backData1['departure']
                for j in range(1,len(self.routes[i]['route'])):
                    if (prev != self.routes[i]['route'][j][1]):
                        r = 'travel from {} to {}'.format(prev, self.routes[i]['route'][j][1])
                    else: r = 'refuel on {}'.format(prev)
                    if (self.routes[i]['route'][j][2] == 'BH'): r = r + ' with 10% chance of being captured on day {} on {} - '.format(self.routes[i]['route'][j][0], self.routes[i]['route'][j][1])
                    else: r = r + ' - '
                    route.append(r) ; prev = self.routes[i]['route'][j][1]
                route[j-1] = route[j-1].strip(' - ')+'.'
                self.res[1].append(route)

        return self.res


#!/usr/bin/python3
""" Class Base: """
import json
from os import path

class Base:
    """ private class attribute __nb_objects = 0 """
    __nb_objects = 0
    """  class constructor """
    def __init__(self, id=None):
        """ if id is not None, assign the public
        instance attribute id with this argument
        value - you can assume id is an integer and
        you don’t need to test the type of it """
        if id is not None:
            """pide at.. publico"""
            self.id = id
        else:
            """ otherwise, increment __nb_objects
            and assign the new value to the public
            instance attribute id """
            Base.__nb_objects += 1
            self.id = Base.__nb_objects

    @staticmethod
    def to_json_string(list_dictionaries):
        """ returns the JSON string representation
        of list_dictionaries"""
        if list_dictionaries is None or len(list_dictionaries) == 0:
            return "[]"
        return json.dumps(list_dictionaries)

    @classmethod
    def save_to_file(cls, list_objs):
        """class method def save_to_file(cls, list_objs):
        that writes the JSON string representation
        of list_objs to a file:"""
        filename = cls.__name__ + ".json"

        if list_objs is not None:
                list_objs = [i.to_dictionary() for i in list_objs]

        """If list_objs is None, save an empty list"""
        """You must overwrite the file if it already exists"""
        with open(filename, 'w', encoding="utf-8") as myfn:
            """You must use the static method to_json_string"""
            myfn.write(cls.to_json_string(list_objs))

    @staticmethod
    def from_json_string(json_string):
        """the static method def from_json_string
        (json_string): that returns the list of the
        JSON string representation json_string:"""
        if json_string is None or len(json_string) == 0:
            return []
        else:
            return json.loads(json_string)

    @classmethod
    def create(cls, **dictionary):
        """the class method def create(cls, **dictionary):
        that returns an instance with all attributes already set:
            - **dictionary can be thought of as double pointer to a dictionary
            - To use the update method to assign all
        attributes, you must create a “dummy” instance before:
                - Create a Rectangle
                or Square instance with
                “dummy” mandatory attributes (width, height, size, etc.)
                - Call update instance method
                to this “dummy” instance to apply
                your real values
            - You must use the method def update(self, *args, **kwargs)
            - **dictionary must be used as **kwargs of the method update"""
        if cls.__name__ == 'Rectangle':
            dummyNew = cls(1, 1)
        elif cls.__name__ == 'Square':
            dummyNew = cls(1, 0)
        dummyNew.update(**dictionary)
        return dummyNew

    @classmethod
    def load_from_file(cls):
        """the class method def load_from_file(cls):
        that returns a list of instances"""
        filename = cls.__name__ + ".json"
        instanceList = []
        """ If the file doesn’t exist, return an empty list """
        if path.exists(filename) is None or path.exists(filename) is False:
            return []
        else:
            """ Otherwise, return a list of instances - the type of these instances
            depends on cls (current class using this method) """
            with open(filename, 'r', encoding="utf-8") as myfn:
                f = myfn.read()
                myList = cls.from_json_string(f)
            """i = dict - el recorrido"""
            for i in myList:
                instanceList.append(cls.create(**i))
            return instanceList


import random

#from random import randint

#def Unit class

class Unit:

    #add Class Decorators

    def __init__(self, name):
        self.name = name
        self.hits_remaining = 2 #track number of hits, set it to the number of hits that each unit type can survive
        self.attack_rating = random.randint(1,5)

    def hit(self, hit_val=1): # just subtract 1 from self.hits_remaining each time it’s called
        self.hits_remaining -= hit_val

    def is_alive(self): #return true or false
        return self.hits_remaining > 0

    def unit_status(self):

        print(self.name, " rating: ", self.attack_rating, " Hits remaining: ", self.hits_remaining)

    def retreat(self): #make unit move back
        pass

    def attack(self, roll, target_unit):
        attack_result = None #init result as empty/null
        # -1 Destroy, 0 = retreat, 1 = hit, 2 = hit and retreat

        if roll == self.attack_rating: #roll == attack
            print(f"{target_unit.name} must take hit or retreat.")
            num = random.randint(1,10) #rand to decide hit or retreat, even to retreat, odd to hit
            if (num % 2) == 0:
                attack_result = 0 #retreat
                target_unit.retreat()
            else:
                attack_result = 1 #hit
                target_unit.hit()

        if roll < self.attack_rating and self.attack_rating > 1:
            attack_result = 2 #hit and retreat
            target_unit.hit()
            target_unit.retreat()

        if roll == 1 and self.attack_rating >= target_unit.attack_rating: #destroy
            attack_result = -1 #target_unit.hits_remaining = 0

        if attack_result is None: #attack has no effect
            print("Shot miss!")
        else:
            print(f"Attack result: {attack_result}")

        if attack_result == 1 or 2: #"Hit!" or "Hit and retreat!":
            target_unit.hit()
            print(target_unit.name, f"HIT! Hit count: {target_unit.hits_remaining} ")

        if target_unit.is_alive == False: #e.status = -1
            print(f"{target_unit.name} Destroyed!")

        return attack_result


# coding: utf8
#!/usr/bin/env python3

from oscpy.client import OSCClient
from oscpy.server import OSCThreadServer
from omxplayer.player import OMXPlayer
import os
from subprocess import call, check_output
from time import sleep
import logging
from pathlib import Path
import os.path as op

logging.basicConfig(level=logging.INFO)
printToTerminal = True

run = True
quitProcedure = {"quit": "", "shutdown": "sudo shutdown -h now", "reboot": "sudo reboot now", "none": ""}
quitProcedureSelected = "none"

video_dir = "/home/pi/Documents/OSCvideo/" # Specify initial path to video dir. Start with "/" to get an absolute path eg. "/home/pi/videos/"

VIDEO_PATHS = ["#1.m4v", "#2.m4v",
               "#3.m4v", "#4.m4v",
               "#5.m4v", "#6.m4v",
               "#7.m4v", "#8.m4v",
               "#9.m4v", "#10.m4v"]

##TODO GET SCREEN SIZE SO THAT WE ARE ABLE TO CONTROL ALPAH WITH THE WORKAROUND OF USING --win argument
sdCheck = check_output("fbset -s", shell=True).decode('utf-8').split("\n")
screenWidth = sdCheck[2].split(" ")[5]
screenHeight = sdCheck[2].split(" ")[6]
print("Resolution of the screen is {}x{}".format(screenWidth, screenHeight))

player_log = logging.getLogger("Player 1")

backGroundPlayer = None
testImagePlayer = None

players = {"1":None,
           "2": None,
           "3": None,
           "4": None,
           "5": None,
           "6": None,
           "7": None,
           "8": None,
           "9": None,
           "10": None}

##TODO GET BLANKING TO WORK
playerArgs = {"1": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "2": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "3": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "4": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "5": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "6": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "7": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "8": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "9": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)],
              "10": ['--no-osd', '--no-keys', '--win=0,0,{},{}'.format(screenWidth, screenHeight)]}

playerLayer = {"1": ["1"],
              "2": ["2"],
              "3": ["3"],
              "4": ["4"],
              "5": ["5"],
              "6": ["6"],
              "7": ["7"],
              "8": ["8"],
              "9": ["9"],
              "10": ["10"]}

playerLoop = {"1": ["--loop"],
              "2": ["--loop"],
              "3": ["--loop"],
              "4": ["--loop"],
              "5": ["--loop"],
              "6": ["--loop"],
              "7": ["--loop"],
              "8": ["--loop"],
              "9": ["--loop"],
              "10": ["--loop"]}

qlabPort = 53000
oscPort = 53003

##HELPER FUNCTIONS:
encoding = 'utf-8'
def stringAsBytes(stringToEncode=""):
   return stringToEncode.encode(encoding)

def bytesAsString(bytesToDecode):
   return bytesToDecode.decode(encoding)

def toInt(value):
   return int(value)

##PLAYER FUNCTIONS
def playPlayer(playerNumber, ip, filePath=""):
   global players
   try:
      players[str(playerNumber)].play()
      trueFlag = False
   except:
      trueFlag = True
   try:
      if players[str(playerNumber)] == None or trueFlag:
         console_message =("No player loaded at slot#{} - loading a new one".format(str(playerNumber)))
         send_console_message(ip=ip, message=console_message)
         if filePath== "":
            loadPlayer(playerNumber=playerNumber, ip=ip)
         else:
            loadPlayer(playerNumber=playerNumber, ip=ip, filePath=filePath)
      players[str(playerNumber)].play()
      console_message = ("Playling player #{}".format(str(playerNumber)))
   except Exception as e:
      console_message = "Failing to play player #{}. Errormessage is: {}".format(str(playerNumber), e)
   send_console_message(ip=ip, message=console_message)

def loadPlayer(playerNumber, ip, filePath=""):
   global players
   try:
      if players[str(playerNumber)].is_playing():
         if printToTerminal:
            print("Already playing")
         return
      else:
         players[str(playerNumber)].stop()
         trueFlag = False
   except:
      trueFlag = True
   if players[str(playerNumber)] == None or trueFlag:
      pNum = int(playerNumber)
      pStr = str(int(playerNumber))
      addPathToDir = ""
      if filePath[0] != "/":
         print("ABSOLUTE NOT RELATIVE")
         addPathToDir = video_dir
      if filePath == "":
         fp = addPathToDir + VIDEO_PATHS[pNum - 1]
      else:
         fp = addPathToDir + filePath

      myArgs = playerArgs[pStr] + ['--layer'] + playerLayer[pStr] + playerLoop[pStr]
      console_message = ("Loading player#{} with theses args: {}".format(pStr, myArgs))
      dbus_name_tmp = 'org.mpris.MediaPlayer2.omxplayer' + str(playerNumber)
      players[str(playerNumber)] = OMXPlayer(source=fp,
                                             args=myArgs,
                                             dbus_name=dbus_name_tmp)
      players[str(playerNumber)].pause()
   else:
      console_message = ("Player#{} is already loaded...".format(playerNumber))
   send_console_message(ip=ip, message=console_message)

def pausePlayer(playerNumber, ip):
   global players
   players[str(playerNumber)].pause()
   console_message = "Pausing player#{}".format(playerNumber)
   send_console_message(ip=ip, message=console_message)

def stop_player(playerNumber, ip):
   global players
   try:
      if players[str(playerNumber)] != None:
         players[str(playerNumber)].stop()
         players[str(playerNumber)] = None
         console_message = "Stopping and setting to None player#{}".format(playerNumber)
         send_console_message(ip=ip, message=console_message)
      else:
         if printToTerminal:
            print ("I do not exist")
   except Exception as e:
      print(e)
      players[str(playerNumber)] = None

def setArgsToPlayer(playerNumber, argument):
   global playerArgs
   if printToTerminal:
      print (playerNumber, argument)
   # TODO IMPLEMENT THIS (List of args found here: https://elinux.org/Omxplayer)

def setPositionOfPlayer(playerNumber, x1,y1,x2,y2):
   if printToTerminal:
      print (x1, y1, x2, y2)
   players[str(playerNumber)].set_video_pos(x1, y1, x2, y2)

def setAlpha(playerNumber, alphaValue):
   global players
   try:
      if printToTerminal:
         print("SET ALPHA {}".format(players[str(playerNumber)]))
      players[str(playerNumber)].set_alpha(alphaValue)
   except:
      if printToTerminal:
         print("cannot set the alpah to {} on player {}".format(alphaValue, playerNumber))

##OSC (CALLBACKS AND SERVER)
def send_console_message(ip, message):
   try:
      if printToTerminal:
         print ("Sending Console message {} - {} - {}".format(message, ip, qlabPort))
      oscClient = OSCClient(address=ip, port=qlabPort)
      oscClient.send_message(address=b"/cue/console/notes", values=[stringAsBytes(message)])
   except Exception as e:
      print(e)

def default_handler(*values):
   if printToTerminal:
      print("DEFAULT HANDLER. VALUES ARE: {}".format(values))

osc = OSCThreadServer(default_handler=default_handler)
sock = osc.listen(address='0.0.0.0', port=int(oscPort), default=True)

@osc.address(b"/heartbeat")
def heartbeat_callback(*values):
   try:
      if len(values) > 0:
         print(values)
         oscClient = OSCClient(address=osc.get_sender()[1], port=qlabPort)
         oscClient.send_message(address=stringAsBytes("/cue/{}/colorName".format(bytesAsString(values[0]))), values=[stringAsBytes("green")])
         oscClient.send_message(address=stringAsBytes("/cue/{}Color/start".format(bytesAsString(values[0]))), values=[])
         oscClient.send_message(address=stringAsBytes("/cue/{}/start".format(bytesAsString(values[0]))), values=[])
         oscClient.send_message(address=stringAsBytes("/cue/{}Trigger/start".format(bytesAsString(values[0]))), values=[])
   except Exception as e:
      console_message = "Failed with the following error: {}".format(e)
      send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/background_on")
def showBlackBackGround_callback(*values):
   global backGroundPlayer
   try:
      if backGroundPlayer == None or not backGroundPlayer.is_playing():
         path = video_dir + "Static_files/BLACK.m4v"
         dbus_name_tmp = 'org.mpris.MediaPlayer2.omxplayerBackGround'
         backGroundPlayer = OMXPlayer(source=path,
                                      args=['--no-osd', '--no-keys', '--loop'],
                                      dbus_name=dbus_name_tmp,
                                      pause=False)
         backGroundPlayer.set_aspect_mode('fill')
         backGroundPlayer.set_alpha(255)
         console_message = "Starting the background and setting the opacity to 255"
      else:
         backGroundPlayer.set_alpha(255)
         console_message = "Setting the opactity to 255 on the background"
   except Exception as e:
      path = "Static_files/BLACK.m4v"
      dbus_name_tmp = 'org.mpris.MediaPlayer2.omxplayerBackGround'
      backGroundPlayer = OMXPlayer(source=path,
                                   args=['--no-osd', '--no-keys', '--loop'],
                                   dbus_name=dbus_name_tmp,
                                   pause=False)
      backGroundPlayer.set_aspect_mode('fill')
      backGroundPlayer.set_alpha(255)
      console_message = "Starting the background and setting the opacity to 255"
   try:
      send_console_message(ip=osc.get_sender()[1], message=console_message)
   except Exception as e:
      print("Initial black out of screen - Send \"/background_off\" to reveal desktop")

@osc.address(b"/background_off")
def removeBlackBackGround_callback(*values):
   global backGroundPlayer
   try:
      if backGroundPlayer == None or backGroundPlayer.is_playing():
         backGroundPlayer.stop()
         backGroundPlayer = None
         console_message = "Stopping the background"
      else:
         console_message = "Background not playing"
   except Exception as e:
      console_message = e
   send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/testImage_on")
def showTestImage_callback(*values):
   global testImagePlayer
   try:
      if testImagePlayer == None or not testImagePlayer.is_playing():
         if str(values[0]) == "720":
            fn = "TEST_SCREEN_720P.m4v"
         elif str(values[0]) == "1080":
            fn = "TEST_SCREEN_1080P.m4v"
         else:
            fn = "TEST_SCREEN_PAL.m4v"
         path = video_dir + "Static_files/" + fn
         dbus_name_tmp = 'org.mpris.MediaPlayer2.omxplayerTestImage'
         testImagePlayer = OMXPlayer(source=path,
                                      args=['--no-osd', '--no-keys', '--loop', '--layer=11'],
                                      dbus_name=dbus_name_tmp,
                                      pause=False)
         testImagePlayer.set_aspect_mode('fill')
         testImagePlayer.set_alpha(255)
         console_message = "Starting the test image and setting the opacity to 255"
      else:
         testImagePlayer.set_alpha(255)
         console_message = "Setting the opactity to 255 on the test image"
   except Exception as e:
      console_message = "Could not display test image: {}".format(e)
   send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/testImage_off")
def removeBlackBackGround_callback(*values):
   global testImagePlayer
   try:
      if testImagePlayer == None or testImagePlayer.is_playing():
         testImagePlayer.stop()
         console_message = "Stopping the test image"
         testImagePlayer = None
      else:
         console_message = "Test image not playing"
   except Exception as e:
      console_message = e
   send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/opacity")
def setAlpha_callback(*values):
   try:
      if printToTerminal:
         print("OPACITY {}".format(values))
      setAlpha(playerNumber=toInt(values[0]), alphaValue=toInt(values[1]))
   except:
      msg = "Values are missing or in wrong format: {}".format(values)
      print(msg)
      send_console_message(ip=osc.get_sender()[1], message=msg)

@osc.address(b"/setPosition")
def setPosition_callback(*values):
   try:
      setPositionOfPlayer(playerNumber=toInt(values[0]),
                          x1=toInt(values[1]),
                          y1=toInt(values[2]),
                          x2=toInt(values[3]),
                          y2=toInt(values[4]))
   except Exception as e:
      msg = "Error: {}".format(e)
      print(msg)
      send_console_message(ip=osc.get_sender()[1], message=msg)

@osc.address(b"/load")
def loadMovie_callback(*values):

   filePath = ""
   try:
      if len(values) == 2:
         loadPlayer(playerNumber=toInt(values[0]), ip=osc.get_sender()[1])
      elif len(values) == 3:
         loadPlayer(playerNumber=toInt(values[0]), ip=osc.get_sender()[1], filePath=bytesAsString(values[2]))
      else:
         console_message = "Setting filepath failed. Missing arguments. These arguements has been passed {}".format(values)
         send_console_message(ip=osc.get_sender()[1], message=console_message)
   except Exception as e:
      print(e)
      console_message = "Error loading player. These arguements has been passed {}".format(values)
      send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/play")
def playMovie_callback(*values):
   if len(values)>1:
      playPlayer(playerNumber=toInt(values[0]), filePath=bytesAsString(values[1]), ip=osc.get_sender()[1])
   elif len(values)==1:
      playPlayer(playerNumber=toInt(values[0]), ip=osc.get_sender()[1])
   else:
      console_message = "Error starting the player. No arguments passed"
      send_console_message(ip=osc.get_sender()[1], message=console_message)

@osc.address(b"/pause")
def pauseMovie_callback(*values):
   pausePlayer(playerNumber=toInt(values[0]), ip=osc.get_sender()[1])

@osc.address(b"/stop")
def stopMovie_callback(*values):
   stop_player(playerNumber=toInt(values[0]), ip=osc.get_sender()[1])

@osc.address(b"/loop")
def loopMovie_callback(*values):
   global playerLoop
   if bytesAsString(values[1])=="on":
      if playerLoop[str(toInt(values[0]))] == []:
         playerLoop[str(toInt(values[0]))] = ["--loop"]
         msg = "Settting loop on player #{} to true".format(str(toInt(values[0])))
      elif playerLoop[str(toInt(values[0]))] == ["--loop"]:
         msg = "Player{} is already looping".format(str(toInt(values[0])))
   elif bytesAsString(values[1])=="off":
      playerLoop[str(toInt(values[0]))] = []
      msg = "Settting loop on player #{} to False".format(str(toInt(values[0])))
   else:
      msg = "The '{}' argument is not understood".format(bytesAsString(values[1]))
   print (playerLoop)
   send_console_message(ip=osc.get_sender()[1], message=msg)

# def volume_callback(*values):
#    call("amixer set Master {}%".format(args[0]), shell=True)
#    print("Set level to {}".format(args[0]))

@osc.address(b"/quit")
def quit_callback(*values):
   global run, quitProcedureSelected
   quitType = bytesAsString(values[0])
   if quitType in quitProcedure:
      console_message = "Quitting Player, and shutting down"
      os.system('pkill omxplayer.bin')
      quitProcedureSelected = quitType
      run = False
      sock.close()
   else:
      console_message = "No the right keyword use: 'shutdown', 'quit' or 'reboot'"
      quitProcedureSelected = "none"
   send_console_message(ip=osc.get_sender()[1], message=console_message)
   if printToTerminal:
      print (quitProcedureSelected)

print("###SYSTEM HAS STARTED###")
showBlackBackGround_callback()

# Program loop
while run:
   sleep(.01)##

# server.close
sleep(2)
call(quitProcedure[quitProcedureSelected], shell=True)


# -*- coding: utf-8 -*-
"""
Created on Wed May 22 22:24:06 2019

@author: MPAZ
"""

import ccxt
import claves

#ACCESAR A BINANCE
usuario=claves.api_key
clave=claves.secret

#instancia mediante id a Binance con claves
exchange_id= 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange= exchange_class({
	'apiKey':usuario,
	'secret':clave,
	'timeout':30000,
	'enableRateLimit:': True,
})

casas_cambio=ccxt.exchanges
mercados=exchange.load_markets(True)
monedas= exchange.currencies
simbolos=exchange.symbols
metodos=dir(exchange)

print("empezamos")

saldob1=exchange.fetch_balance()['ETC']['free']
saldoq1=exchange.fetch_balance()['SKY']['free']

#ordenes_cerradas=exchange.fetch_closed_orders(par1) # LISTADO ORDENES CERRADAS
print("ORDEN CERRADA !!! ")

#prueba3=exchange.fetch_my_trades(par1)


import startup_font
print(startup_font.cipher)
print(startup_font.Author)
alphabet= ['a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

direction = input('Enter "encode" to Encrypt the plain_text or enter "decode" to Decrypt the encoded text: ')
text = input("Enter the text: ").lower()
shift= int(input("Enter the shift value: "))

def encrypt(plain_text,shift_amount):
    cipher_text=""
    for letter in plain_text:
        position = alphabet.index(letter)
        new_position = position+shift_amount
        cipher_text += alphabet[new_position]
    print(f"Ecrypted text is {cipher_text}")

def decrypt(cipher_text,shift_amount):
    plain_text=""
    for letter in cipher_text:
        position = alphabet.index(letter)
        new_position = position-shift_amount
        plain_text += alphabet[new_position]
    print(f"Decrypted text is {plain_text}")

if direction == "encode":
  encrypt(plain_text=text, shift_amount=shift)
elif direction == "decode":
  decrypt(cipher_text=text, shift_amount=shift )


import pandas as pd

from omnipath._core.utils._orthology import translate_column
from omnipath._core.utils._homologene import download_homologene

class TestHomologene:
    def test_download_homologene(self):
        homologene = download_homologene(9606, 10090)

        expected_shape = (17312, 2)
        actual_shape = homologene.shape

        assert expected_shape == actual_shape

        expected_columns = ["source", "target"]
        actual_columns = homologene.columns

        assert all(expected_columns == actual_columns)

class TestOrthologyConversion:
    def test_complex_genes(self):
        df = pd.DataFrame(
            {
                "symbol": [
                    "CSF2RA_CSF2RB",  # one to many
                    "IFNL3_IFNLR1_IL10RB",  # 3 subunits
                    "HCST_KLRK1",  # one subunit missing
                    "CD8A_CD8B",  # 1 to 1
                    "IL4",  # 1 to 1 simple protein
                ]
            }
        )

        default = translate_column(
            df,
            column="symbol",
            id_type="genesymbol",
            target_organism=10090,
        )
        assert all(default["symbol"] == ["Cd8a_Cd8b1", "Il4"])

        to_many = translate_column(
            df,
            column="symbol",
            id_type="genesymbol",
            target_organism=10090,
            replace=True,
            keep_untranslated=False,
            one_to_many=2,
        )
        expected = [
            "Cd8a_Cd8b1",
            "Csf2ra_Csf2rb",
            "Csf2ra_Csf2rb2",
            "Ifnl2_Ifnlr1_Il10rb",
            "Ifnl3_Ifnlr1_Il10rb",
            "Il4",
        ]

        assert to_many.shape == (6, 1)
        assert all(to_many["symbol"] == expected)

        keep_missing = translate_column(
            df,
            column="symbol",
            id_type="genesymbol",
            target_organism=10090,
            replace=False,
            keep_untranslated=True,
            one_to_many=2,
        )
        untranslated = keep_missing["symbol"].isin(["HCST_KLRK1"])
        assert untranslated.any()
        assert keep_missing[untranslated]["orthology_target"].isna().all()


import pickle
import pandas as pd
import argparse
import sys
import os
sys.path.append(os.path.abspath('./graph'))
sys.path.append(os.path.abspath('./utils'))
from graph import Graph
from utils import *
from node import Node

def read_dataset(fileName):
    """ Read the dataset and transform it to a dataframe
    Parameters
    ----------
    fileName: str
        file name that contains the dataset
    Retruns
    -------
    df: dataframe
        a dataframe to be classified
    """
    articles = pd.read_pickle(fileName)

    #Transforming the dataset to dataframe type to ease the operations on it
    df = df = pd.DataFrame({"title":[],"content":[],"topic":[]})
    for i in range(len(articles)):
        df = df.append({'title':articles[i]["title"],'content':articles[i]["content"],\
            'topic':articles[i]["topic"]},ignore_index=True)

    return df

def create_graph(data, n):
    G = Graph()
    for index, article in data.iterrows():
        G.add_node(Node(index, article["tokens"]))
    G.add_edges(n)
    return G

def main(fileName, n):
    df = read_dataset(fileName)
    df["tokens"] = df["content"].apply(lambda s: text2tokens(s))
    G = create_graph(df, n)
    connected_components = G.connected_components()
    for component in  connected_components:
	    print(component)
	    print("--------------------")

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("fileName", help="dataset file name", type=str)
    parser.add_argument("n", help="minimun nombre of tokens shared between two nodes to have an edge between them", type=int)
    args = parser.parse_args()
    main(args.fileName, args.n)


#!/usr/bin/python3
from os import close, stat
# from threading import TIMEOUT_MAX
import rospy
from nav_msgs.msg import OccupancyGrid
from geometry_msgs.msg import Twist, PoseStamped
from math import frexp, pow, atan2, sqrt
from nav_msgs.msg import Odometry, Path
from tf.transformations import euler_from_quaternion
import sys
from threading import Thread
import matplotlib.pyplot as plt
import math
from nav_msgs.srv import GetPlan

show_animation = False
# show_animation = True

class Dijkstra:

    def __init__(self, ox, oy, resolution, robot_radius):
        """
        Initialize map for a star planning

        ox: x position list of Obstacles [m]
        oy: y position list of Obstacles [m]
        resolution: grid resolution [m]
        rr: robot radius[m]
        """

        self.min_x = None
        self.min_y = None
        self.max_x = None
        self.max_y = None
        self.x_width = None
        self.y_width = None
        self.obstacle_map = None

        self.resolution = resolution
        self.robot_radius = robot_radius
        self.calc_obstacle_map(ox, oy)
        self.motion = self.get_motion_model()

    class Node:
        def __init__(self, x, y, cost, parent_index):
            self.x = x  # index of grid
            self.y = y  # index of grid
            self.cost = cost
            self.parent_index = parent_index  # index of previous Node

        def __str__(self):
            return str(self.x) + "," + str(self.y) + "," + str(
                self.cost) + "," + str(self.parent_index)

    def planning(self, sx, sy, gx, gy):
        """
        dijkstra path search

        input:
            s_x: start x position [m]
            s_y: start y position [m]
            gx: goal x position [m]
            gx: goal x position [m]

        output:
            rx: x position list of the final path
            ry: y position list of the final path
        """

        start_node = self.Node(self.calc_xy_index(sx, self.min_x),
                               self.calc_xy_index(sy, self.min_y), 0.0, -1)
        goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
                              self.calc_xy_index(gy, self.min_y), 0.0, -1)

        open_set, closed_set = dict(), dict()
        open_set[self.calc_index(start_node)] = start_node
        print(open_set, start_node)
        while 1:
            c_id = min(open_set, key=lambda o: open_set[o].cost)
            current = open_set[c_id]

            # show graph
            if show_animation:  # pragma: no cover
                plt.plot(self.calc_position(current.x, self.min_x),
                         self.calc_position(current.y, self.min_y), "xc")
                # for stopping simulation with the esc key.
                plt.gcf().canvas.mpl_connect(
                    'key_release_event',
                    lambda event: [exit(0) if event.key == 'escape' else None])
                if len(closed_set.keys()) % 10 == 0:
                    plt.pause(0.001)

            if current.x == goal_node.x and current.y == goal_node.y:
                print("Find goal")
                goal_node.parent_index = current.parent_index
                goal_node.cost = current.cost
                break

            # Remove the item from the open set
            del open_set[c_id]

            # Add it to the closed set
            closed_set[c_id] = current

            # expand search grid based on motion model
            for move_x, move_y, move_cost in self.motion:
                node = self.Node(current.x + move_x,
                                 current.y + move_y,
                                 current.cost + move_cost, c_id)
                n_id = self.calc_index(node)

                if n_id in closed_set:
                    continue

                if not self.verify_node(node):
                    continue

                if n_id not in open_set:
                    open_set[n_id] = node  # Discover a new node
                else:
                    if open_set[n_id].cost >= node.cost:
                        # This path is the best until now. record it!
                        open_set[n_id] = node

        rx, ry = self.calc_final_path(goal_node, closed_set)

        return rx, ry

    def calc_final_path(self, goal_node, closed_set):
        # generate final course
        rx, ry = [self.calc_position(goal_node.x, self.min_x)], [
            self.calc_position(goal_node.y, self.min_y)]
        parent_index = goal_node.parent_index
        while parent_index != -1:
            n = closed_set[parent_index]
            rx.append(self.calc_position(n.x, self.min_x))
            ry.append(self.calc_position(n.y, self.min_y))
            parent_index = n.parent_index

        return rx, ry

    def calc_position(self, index, minp):
        pos = index * self.resolution + minp
        return pos

    def calc_xy_index(self, position, minp):
        return round((position - minp) / self.resolution)

    def calc_index(self, node):
        return (node.y - self.min_y) * self.x_width + (node.x - self.min_x)

    def verify_node(self, node):
        px = self.calc_position(node.x, self.min_x)
        py = self.calc_position(node.y, self.min_y)

        if px < self.min_x:
            return False
        if py < self.min_y:
            return False
        if px >= self.max_x:
            return False
        if py >= self.max_y:
            return False

        if self.obstacle_map[node.x][node.y]:
            return False

        return True

    def calc_obstacle_map(self, ox, oy):

        self.min_x = round(min(ox))
        self.min_y = round(min(oy))
        self.max_x = round(max(ox))
        self.max_y = round(max(oy))

        self.x_width = round((self.max_x - self.min_x) / self.resolution)
        self.y_width = round((self.max_y - self.min_y) / self.resolution)

        # obstacle map generation
        self.obstacle_map = [[False for _ in range(self.y_width)]
                             for _ in range(self.x_width)]
        for ix in range(self.x_width):
            x = self.calc_position(ix, self.min_x)
            for iy in range(self.y_width):
                y = self.calc_position(iy, self.min_y)
                for iox, ioy in zip(ox, oy):
                    d = math.hypot(iox - x, ioy - y)
                    if d <= self.robot_radius:
                        self.obstacle_map[ix][iy] = True
                        break

    @staticmethod
    def get_motion_model():
        # dx, dy, cost
        motion = [[1, 0, 1],
                  [0, 1, 1],
                  [-1, 0, 1],
                  [0, -1, 1],
                  [-1, -1, math.sqrt(2)],
                  [-1, 1, math.sqrt(2)],
                  [1, -1, math.sqrt(2)],
                  [1, 1, math.sqrt(2)]]

        return motion

class dijkstra_ros:

    def __init__(self):
        self.start_x = None
        self.start_y = None
        self.trans_x = None
        self.trans_y = None
        self.start_pose_x_mtrs = None
        self.start_pose_y_mtrs = None
        # self.rate = rospy.Rate(10)
        self.frame_id = 'map'
        self.pose_x = 0
        self.pose_y = 0
        self.theta = 0
        self.waypoints = []
        self.map_msg = rospy.wait_for_message("/map", OccupancyGrid, timeout=None)
        # path message object
        # self.path = Path()
        # self.path.header.frame_id = 'map'
        # self.path.header.stamp = rospy.Time.now()
        # path waypoint list
        self.path_list = []
        self.path_publisher = rospy.Publisher('/path', Path, queue_size=10)
        # self.path_publisher = rospy.Publisher('/move_base/NavfnROS/plan', Path, queue_size=10)
        # 2D graph initializer
        self.d2_graph = []
        self.map_info = self.map_msg.info
        self.map_res = self.map_msg.info.resolution
        self.map_height = self.map_msg.info.height
        self.map_width = self.map_msg.info.width
        self.occupancy_threshold = 0.65
        self.list_of_pixel_coordinates = []
        self.turn_counter = 0

        # ########## Call functions already ###########33

        self.one_to_two_d_graph()
        self.gen_obstracle_poses()

    def one_to_two_d_graph(self):
        """
        This function converts one dimentional map.data array to a
        nested array of dimentions map.info.width X map.info.height
        so map looks somewhat like following
        [
            [0, 0, 0, 0, -1],
            [0, 1, 0, 0, 0],
            [0, 0, 1, 0, 0],
            [0, 1, 0, 0, 0],
            [0, 0, 0, -1, 0]

        ]

        map coordinate axis
            (0,0)
            +---------------x
            |               |
            |               |
            |               |
            |               |
            |               |
            y---------------+

        """
        one_d_graph = self.map_msg.data

        while one_d_graph:
            self.d2_graph.append(one_d_graph[:self.map_msg.info.width])
            one_d_graph = one_d_graph[self.map_msg.info.width:]

        # reversing the map graph array to normalize the image
        self.d2_graph = self.d2_graph[::-1]

    def gen_obstracle_poses(self):
        self.ox , self.oy = [], []
        for i in range(len(self.d2_graph)):
            for j in range(len(self.d2_graph[0])):
                if self.d2_graph[i][j] >= 0.65:   # 0.65 is obstracle threshold
                    self.ox.append(j)
                    self.oy.append(i)
        print('Done generating obs poses')

    def transform_coordinates_to_grid(self, x_mtrs,  y_mtrs):
        '''
        NOTE: This function takes x,y values in meters
         and transforms it to 2D graph coordinates
        '''
        self.trans_x = self.map_width/2
        self.trans_y = self.map_height/2

        return self.trans_x + x_mtrs/self.map_res, self.trans_y - y_mtrs/self.map_res

    def transform_coordinates_to_rviz(self, x_px, y_px):
        '''
        NOTE: This function takse x,y values in index
        of grid map and transforms it into Rviz mtr coordinates
        '''
        self.trans_x = self.map_width/2
        self.trans_y = self.map_height/2

        x_m, y_m = self.map_res*(x_px - self.trans_x), self.map_res*(self.trans_y - y_px)
        return x_m, y_m

    def publish_path(self, x, y):
        '''
        NOTE: This function is publishes the path with
        respect to rviz coordinates in meters
        '''

        msg = Path()
        msg.header.frame_id = "map"
        list_ = msg.poses
        for i, j in zip(x[::-1], y[::-1]):
            # print(i,j)
            x_mtrs, y_mtrs = self.transform_coordinates_to_rviz(i,j)
            pose_msg = PoseStamped()
            pose_msg.header.frame_id = "map"
            msg.header.stamp = rospy.Time.now()
            pose_msg.pose.position.x = round(x_mtrs,2)
            pose_msg.pose.position.y = round(y_mtrs, 2)
            # print(pose_msg)
            list_.append(pose_msg)
        rospy.loginfo("No of Points : " + str(len(msg.poses)))
        self.path_publisher.publish(msg)
        return msg

    def initiate_dijkstra(self, x_start, y_start, x_goal, y_goal):
        start_time = rospy.get_rostime()
        rospy.loginfo("Inititated ...")

        # start and goal position
        # print(ox,oy)
        sx, sy = self.transform_coordinates_to_grid(x_start, y_start)  # [m]
        gx, gy = self.transform_coordinates_to_grid(x_goal, y_goal) # [m]
        print(sx,sy,gx,gy)
        # grid_size = 0.2 # [m]  ###  BEST for navigation
        grid_size = 1  # [m]  ###  BEST for navigation
        robot_radius = 5 # [m]

        if show_animation:  # pragma: no cover
            plt.plot(self.ox, self.oy, ".k")
            plt.plot(sx, sy, "og")
            plt.plot(gx, gy, "xb")
            plt.grid(True)
            plt.axis("equal")

        dijkstra = Dijkstra(self.ox, self.oy, grid_size, robot_radius)
        rx, ry = dijkstra.planning(sx, sy, gx, gy)
        path = self.publish_path(rx, ry)
        end_time = rospy.get_rostime()
        rospy.logwarn("Time taken to plan : " + str(round(end_time.secs - start_time.secs, 3)))
        if show_animation:  # pragma: no cover
            plt.plot(rx, ry, "-r")
            plt.pause(1)
            plt.show()
        return path

    def path_server(self):
        rospy.loginfo('Started Listening !')
        s = rospy.Service('get_plan', GetPlan, requesting_path)
        s.spin()

    def requesting_path(self,req):
        rospy.loginfo("Received Nav Request !")
        # p = PoseStamped()
        start_x, start_y = req.start.pose.position.x,req.start.pose.position.y
        goal_x, goal_y = req.goal.pose.position.x, req.goal.pose.position.y
        # p.pose.position.
        rospy.loginfo('start pose > x: ' + str(start_x) + ' y: ' + str(start_y))
        rospy.loginfo('goal pose > x: ' + str(goal_x) + ' y: ' + str(goal_y))

        path = self.initiate_dijkstra(start_x, start_y, goal_x, goal_y)
        # rospy.loginfo(req.start.pose.position.x,req.start.pose.position.y)
        return path

    def test_dijkstra(self):
        path = self.initiate_dijkstra(0, -0.5, 0, 0.5)

if __name__ == '__main__':
    rospy.init_node("dijkstra_algo", anonymous=True)
    # path_server()
    k = dijkstra_ros()
    k.test_dijkstra()
    # rospy.spin_once()
    sys.exit()


from pyspark import SparkContext
import numpy as np
from pyspark.mllib.regression import LabeledPoint
from time import time
from pyspark.mllib.tree import DecisionTree
from pyspark.mllib.evaluation import RegressionMetrics
import pandas as pd
import matplotlib.pyplot as plt
import math

"""
决策树回归分析
"""
sc = SparkContext('local')
global Path
Path = "D:\\workingSpace\\pythonAndSpark2\data\\Bike\\"

def PrepareData(sc):
    print("开始导入数据。。。")
    path = Path + "hour.csv"
    print(path)
    # 使用minPartitions=40，将数据分成40片，不然报错
    rawDataWithHeader = sc.textFile(path, minPartitions=40)
    header = rawDataWithHeader.first()
    # 去掉首行，标题
    rawData = rawDataWithHeader.filter(lambda x: x != header)
    # 按照，分字段
    lines = rawData.map(lambda x: x.split(","))
    print("总共有:", str(lines.count()))
    # ----2。创建训练所需的RDD数据
    labelpointRDD = lines.map(lambda r: LabeledPoint(extract_label(r), extractFeatures(r, len(r) - 1)))
    # ----3.随机分成3部分数据返回
    (trainData, validationData, testData) = labelpointRDD.randomSplit([8, 1, 1])
    print("数据集划分为：trainData:", str(trainData.count()), "validationData:", str(validationData.count()), "testData:",
          str(testData.count()))
    return (trainData, validationData, testData)

# 数据转换，将文件的问号转换为0
def converFloat(x):
    return (0 if x == "?" else float(x))

# 返回特征字段
def extract_label(field):
    label = (field[-1])
    return float(label)

# 提取
def extractFeatures(record, featureEnd):
    # 提取数值字段
    featureSeason = [converFloat(field) for field in record[2]]
    features=[converFloat(field) for field in record[4:featureEnd-2]]
    result=np.concatenate((featureSeason,features))
    return result

# 评估模型
def trainEvaluateModel(trainData, validationData, impurtyParam, maxDepthParam, maxBinsParam):
    starttime = time()
    model = DecisionTree.trainRegressor(data=trainData, categoricalFeaturesInfo={},
                                         impurity=impurtyParam, maxDepth=maxDepthParam, maxBins=maxBinsParam)
    RMSE = evaluateModel(model, validationData)#均方根误差
    duration = time() - starttime
    print("训练评估使用参数：\n", "impurity=", impurtyParam, "\n maxDepth=", maxDepthParam, "\n maxBins=", maxBinsParam,
          "====>用时=", duration, "\n 结果AUC=", RMSE)
    return (RMSE, duration, impurtyParam, maxDepthParam, maxBinsParam, model)

# 评价模型计算AUC
def evaluateModel(model, validationData):
    # 计算AUC（ROC曲线下的面积）
    score = model.predict(validationData.map(lambda x: x.features))
    print(score)
    scoreAndLabels = score.zip(validationData.map(lambda x: x.label))
    print("scoreAndLabels的前5项", scoreAndLabels.take(5))
    metrics = RegressionMetrics(scoreAndLabels)
    RMSE = metrics.rootMeanSquaredError
    return (RMSE)

# 评估所有参数，选择最佳参数组合
def evalAllParammeter(trainData, validationData, impurityList, maxDepthList, maxBinsList):
    # for循环遍历所有参数集合
    metrics = [trainEvaluateModel(trainData, validationData, impurty, maxDepth, maxBins)
               for impurty in impurityList
               for maxDepth in maxDepthList
               for maxBins in maxBinsList]
    # 找出均方根误差最小的
    Smetrics = sorted(metrics, key=lambda k: k[0])
    bestParammeter = Smetrics[0]
    # 显示调校后的最佳参数组合
    print("调教的最佳参数: impurity=", bestParammeter[2], ",maxDepth=", bestParammeter[3], ",maxBins=", bestParammeter[4],
          "结果RMSE=", bestParammeter[0])
    # 返回最佳模型
    return bestParammeter[5]
# 评估摸一个参数
def evalParammeter(trainData, validationData, evalParam, impurityList, maxDepthList, maxBinsList):
    # 训练评估参数
    metrics = [trainEvaluateModel(trainData, validationData, impurity, maxDepth, maxBins)
               for impurity in impurityList
               for maxDepth in maxDepthList
               for maxBins in maxBinsList]
    # 设置当前评估的参数
    if evalParam == "impurity":
        indexList = impurityList
    elif evalParam == "maxDepth":
        indexList = maxDepthList
    elif evalParam == "maxBins":
        indexList = maxBinsList
    df = pd.DataFrame(data=metrics, index=indexList,
                      columns=["RMSE", "duration", "impurity", "maxDepth", "maxBins", "model"])
    showChart(df, evalParam, "RMSE", "duration", 0, 200)

def showChart(df, evalParam, barData, lineData, yMin, yMax):
    """
    :param df: metrics 产生的dataframe
    :param evalParam: 此次评估的参数 例如 impurity
    :param barData: 绘出batchart数据 例如 AUC
    :param lineData: 绘出linedata数据，这里是duration
    :param yMin: y轴
    :param yMax:
    :return:
    """
    # 绘制直方图
    ax = df[barData].plot(kind="bar", title=evalParam, figsize=(10, 6), legend=True, fontsize=12)
    ax.set_xlabel(evalParam, fontsize=12)
    ax.set_ylim([yMin, yMax])
    ax.set_ylabel(barData, fontsize=12)
    # 绘制折线图
    ax2 = ax.twinx()
    ax2.plot(df[[lineData]].values, linestyle="-", marker="o", linewidth=2.0, color="r")
    plt.show()

def parameterEval(trainData, validationData):
    print("评估maxDepth参数===========")
    evalParammeter(trainData=trainData,
                   validationData=validationData,
                   evalParam="maxDepth",
                   impurityList=["variance"],
                   maxDepthList=[3,5,10,15,20,25],
                   maxBinsList=[10])
    print("评估maxBins参数===========")
    evalParammeter(trainData=trainData,
                   validationData=validationData,
                   evalParam="maxBins",
                   impurityList=["variance"],
                   maxDepthList=[10],
                   maxBinsList=[3,5,10,50,100])

# 预测
def PredictData(sc, model):
    print("开始导入数据")
    path = Path + "hour.csv"
    print(path)
    # 使用minPartitions=40，将数据分成40片，不然报错
    rawDataWithHeader = sc.textFile(path, minPartitions=40)
    header = rawDataWithHeader.first()
    # 去掉首行，标题
    rawData = rawDataWithHeader.filter(lambda x: x != header)
    # 按照，分字段
    lines = rawData.map(lambda x: x.split(","))
    print("总共有:", str(lines.count()))
    # ----2。创建训练所需的RDD数据
    dataRDD = lines.map(lambda r: LabeledPoint(extract_label(r), extractFeatures(r, len(r) - 1)))
    #--------------3定义字典
    SeasonDict = { 1: "春", 2: "夏",3:"秋",4:"冬" }
    HolidayDict={0:"非假日",1:"假日"}
    WeekDict={0:"一",1:"二",2:"三",3:"四",4:"五",5:"六",6:"日"}
    WorkDay={0:"非工作日",1:"工作日"}
    WeatherDict={1:"晴",2:"阴",3:"小雨",4:"大雨"}
    for data in dataRDD.take(10):
        predictResult = int(model.predict(data.features))
        label = data.label
        features = data.features
        result = ("正确" if (label == predictResult) else "错误")
        error=math.fabs(label-predictResult)
        dataDesc="特征："+SeasonDict[features[0]]+"季节，"+str(features[1])+"月，"+str(features[2])+"时，" \
        +HolidayDict[features[3]]+",星期"+WeatherDict[features[4]]+","+WorkDay[features[5]]+","+WeatherDict[features[6]] \
        +","+str(features[7]*41)+"度，体感"+str(features[8]*50)+"度,湿度"+str(features[9]*100)+",风速"+str(features[10]*67)+",预测结果===》" \
        +str(predictResult)+"，实际："+str(label)+result+"误差："+str(error)
        print(dataDesc)

if __name__ == "__main__":
    print("程序启动。。。。。。。。。。")
    print("数据准备金阶段==================")
    (trainData, validationData, testData) = PrepareData(sc)
    # 将数据暂存内存中
    trainData.persist()
    validationData.persist()
    testData.persist()
    print("训练评估阶段===================")
    (RMSE, duration, impurityParam, maxDepthParam, MaxBinsParam, model) = trainEvaluateModel(trainData, validationData,
                                                                                "variance", 5, 5)
    print("找出最好的参数组合=============")
    model = evalAllParammeter(trainData=trainData,
                              validationData=validationData,
                              impurityList=["variance"],
                              maxDepthList=[3, 5, 10, 15, 20, 25],
                              maxBinsList=[3, 5, 10, 50, 100, 200])
    print("测试阶段====================")
    auc = evaluateModel(model, testData)
    print("使用最佳模型，结果RMSE:", RMSE)
    print("预测数据：\n")
    PredictData(sc, model)
    print(model.toDebugString())


# --*-- coding:utf-8 --*--

import redis

class Task(object):
    def __init__(self):
        self.rcon = redis.StrictRedis(host='localhost', db=0)
        self.pending_queue = 'hupu_users:neo_pending'

    def produce_task(self):
        for i in range(10, 20):

            self.rcon.lpush(self.pending_queue, i)

        # while True:
        #     task = self.rcon.blpop(self.queue, 0)[1]
        #     print("Task get", task)

if __name__ == '__main__':
    print('listen task queue')
    Task().produce_task()


import os
import json
import datetime
import urllib.error
import urllib.parse
import urllib.request

from . import locastauth
from . import location

def xmltimestamp_nextpvr(epochtime):
    xmltime = datetime.datetime.fromtimestamp(int(epochtime)/1000)
    xmltime = str(xmltime.strftime('%Y%m%d%H%M%S')) + " +0000"
    return xmltime

def duration_nextpvr_minutes(starttime, endtime):
    return ((int(endtime) - int(starttime))/1000/60)

class proxyserviceFetcher():

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

        self.epg_cache = None
        self.cache_dir = config.config["main"]["locast_web_cache"]
        self.epg_cache_file = config.config["locast"]["epg_cache"]

        self.servicename = "LocastProxy"

        self.urls = {}
        self.url_assembler()

        self.auth = locastauth.Locast_Auth(config)
        self.location_info = location.LocastDMAFinder(config)
        self.location = self.location_info.location

        self.epg_cache = self.epg_cache_open()

    def epg_cache_open(self):
        epg_cache = None
        if os.path.isfile(self.epg_cache_file):
            with open(self.epg_cache_file, 'r') as epgfile:
                epg_cache = json.load(epgfile)
        return epg_cache

    def url_assembler(self):
        pass

    def get_channels(self):
        self.auth.check_token()

        stationsReq = urllib.request.Request('https://api.locastnet.org/api/watch/epg/' +
                                             str(self.location["DMA"]),
                                             headers={'Content-Type': 'application/json',
                                                      'authorization': 'Bearer ' + self.auth.token})

        stationsOpn = urllib.request.urlopen(stationsReq)
        stationsRes = json.load(stationsOpn)
        stationsOpn.close()

        for index, locast_station in enumerate(stationsRes):
            try:
                assert(float(locast_station['callSign'].split()[0]))
                stationsRes[index]['channel'] = locast_station['callSign'].split()[0]
            except ValueError:
                # result like "WDPN" or "CBS" in the callsign field, or the callsign in the name field
                # then we'll search the callsign in a few different lists to get the station channel
                # note: callsign field usually has the most recent data if it contains an actual callsign
                skip_sub_id = False

                # callsign from "callsign" field
                callsign_result = self.detect_callsign(locast_station['callSign'])

                # callsign from "name" field - usually in "[callsign][TYPE][subchannel]" format
                # example: WABCDT2
                alt_callsign_result = self.detect_callsign(locast_station['name'])

                # then check "name"
                if ('channel' not in stationsRes[index]):
                    pass

                # locast usually adds a number in it's callsign (in either field).  that
                # number is the subchannel
                if (not skip_sub_id) and ('channel' in stationsRes[index]):
                    if callsign_result['verified'] and (callsign_result['subchannel'] is not None):
                        stationsRes[index]['channel'] = stationsRes[index]['channel'] + '.' + callsign_result['subchannel']
                    elif alt_callsign_result['verified'] and (alt_callsign_result['subchannel'] is not None):
                        stationsRes[index]['channel'] = stationsRes[index]['channel'] + '.' + alt_callsign_result['subchannel']
                    else:
                        stationsRes[index]['channel'] = stationsRes[index]['channel'] + '.1'

        cleaned_channels = []
        for station_item in stationsRes:
            clean_station_item = {
                                 "name": station_item["callSign"],
                                 "number": station_item["channel"],
                                 "id": station_item["stationId"],
                                 }
            cleaned_channels.append(clean_station_item)
        return cleaned_channels

    def get_station_list(self, base_url):
        station_list = []

        for c in self.get_channels():
            if self.config["fakehdhr"]["stream_type"] == "ffmpeg":
                watchtype = "ffmpeg"
            else:
                watchtype = "direct"
            url = ('%s%s/watch?method=%s&channel=%s' %
                   ("http://",
                    base_url,
                    watchtype,
                    c['channel']
                    ))
            station_list.append(
                                {
                                 'GuideNumber': str(c['number']),
                                 'GuideName': c['name'],
                                 'URL': url
                                })
        return station_list

    def get_station_total(self):
        total_channels = 0
        for c in self.get_channels():
            total_channels += 1
        return total_channels

    def get_channel_streams(self):
        streamdict = {}
        for c in self.get_channels():
            url = ('%s%s:%s/live?channel=%s&client=%s' %
                   ("https://" if self.config["nextpvr"]["ssl"] else "http://",
                    self.config["nextpvr"]["address"],
                    str(self.config["nextpvr"]["port"]),
                    str(c["id"]),
                    str(c["id"]),
                    ))
            streamdict[str(c["id"])] = url
        return streamdict

    def get_channel_thumbnail(self, channel_id):
        channel_thumb_url = ("%s%s:%s/service?method=channel.icon&channel_id=%s" %
                             ("https://" if self.config["nextpvr"]["ssl"] else "http://",
                              self.config["nextpvr"]["address"],
                              str(self.config["nextpvr"]["port"]),
                              str(channel_id)
                              ))
        return channel_thumb_url

    def get_content_thumbnail(self, content_id):
        self.auth.check_token()
        item_thumb_url = ("%s%s:%s/service?method=channel.show.artwork&sid=%s&event_id=%s" %
                          ("https://" if self.config["nextpvr"]["ssl"] else "http://",
                           self.config["nextpvr"]["address"],
                           str(self.config["nextpvr"]["port"]),
                           self.auth.config['sid'],
                           str(content_id)
                           ))
        return item_thumb_url

    def update_epg(self):
        print('Updating NextPVR EPG cache file.')
        self.auth.check_token()

        programguide = {}

        # Make a date range to pull
        todaydate = datetime.date.today()
        dates_to_pull = [todaydate]
        for x in range(1, 6):
            xdate = todaydate + datetime.timedelta(days=x)
            dates_to_pull.append(xdate)

        self.remove_stale_cache(todaydate)

        for x_date in dates_to_pull:
            url = ('https://api.locastnet.org/api/watch/epg/' +
                   str(self.location["DMA"]) + "?startTime=" + str(x_date))

            result = self.get_cached(str(x_date), url)
            channel_info = json.loads(result)

            for channel_item in channel_info:

                channel_number = str(channel_item['callSign']).split(" ")[0]
                channel_realname = str(channel_item['callSign']).split(" ")[1]
                channel_callsign = str(channel_item['name'])
                channel_ID = str(channel_item['stationId'])
                channel_logo = str(channel_item['logo226Url'])

                if str(channel_number) not in list(programguide.keys()):
                    programguide[str(channel_number)] = {}

                programguide[str(channel_number)]["thumbnail"] = channel_logo

                if "name" not in list(programguide[str(channel_number)].keys()):
                    programguide[str(channel_number)]["name"] = channel_realname

                if "callsign" not in list(programguide[str(channel_number)].keys()):
                    programguide[str(channel_number)]["callsign"] = channel_callsign

                if "id" not in list(programguide[str(channel_number)].keys()):
                    programguide[str(channel_number)]["id"] = channel_ID

                if "number" not in list(programguide[str(channel_number)].keys()):
                    programguide[str(channel_number)]["number"] = channel_number

                if "listing" not in list(programguide[str(channel_number)].keys()):
                    programguide[str(channel_number)]["listing"] = []

            for event in channel_item['listings']:

                clean_prog_dict = {}

                event_genres = []
                if 'genres' in event.keys():
                    event_genres = event['genres'].split(",")
                clean_prog_dict["genres"] = event_genres

                clean_prog_dict["time_start"] = self.locast_xmltime(event['startTime'])
                clean_prog_dict["time_end"] = self.locast_xmltime(event['startTime'] + event['duration'] * 1000)
                clean_prog_dict["duration_minutes"] = event['duration'] * 1000

                if event["preferredImage"] is not None:
                    clean_prog_dict["thumbnail"] = event["preferredImage"]

                if 'title' not in list(event.keys()):
                    event["title"] = "Unavailable"
                elif not event["title"]:
                    event["title"] = "Unavailable"
                clean_prog_dict["title"] = event["title"]

                if 'subtitle' not in list(event.keys()):
                    event["subtitle"] = "Unavailable"
                clean_prog_dict["sub-title"] = event["subtitle"]

                if event['subtitle'].startswith("Movie:"):
                    clean_prog_dict['releaseyear'] = event['subtitle'].split("Movie:")[-1]
                else:
                    clean_prog_dict['releaseyear'] = None

                if 'description' not in list(event.keys()):
                    event["description"] = "Unavailable"
                elif event['description']:
                    event["description"] = "Unavailable"
                clean_prog_dict["description"] = event["description"]

                if 'rating' not in list(event.keys()):
                    event['rating'] = "N/A"
                clean_prog_dict['rating'] = event['rating']

                if 'season' in list(event.keys()) and 'episode' in list(event.keys()):
                    clean_prog_dict["seasonnumber"] = event['season']
                    clean_prog_dict["episodenumber"] = event['episode']
                    clean_prog_dict["episodetitle"] = clean_prog_dict["sub-title"]
                else:
                    if "movie" not in clean_prog_dict["genres"]:
                        clean_prog_dict["episodetitle"] = clean_prog_dict["sub-title"]

                # TODO isNEW

                programguide[str(channel_number)]["listing"].append(clean_prog_dict)

        self.epg_cache = programguide
        with open(self.epg_cache_file, 'w') as epgfile:
            epgfile.write(json.dumps(programguide, indent=4))
        print('Wrote updated NextPVR EPG cache file.')
        return programguide

    def get_location(self):
        pass

    def get_cached(self, cache_key, url):
        cache_path = self.cache_dir.joinpath(cache_key)
        if cache_path.is_file():
            print('FROM CACHE:', str(cache_path))
            with open(cache_path, 'rb') as f:
                return f.read()
        else:
            print('Fetching:  ', url)
            try:
                resp = urllib.request.urlopen(url)
                result = resp.read()
            except urllib.error.HTTPError as e:
                if e.code == 400:
                    print('Got a 400 error!  Ignoring it.')
                    result = (
                        b'{'
                        b'"note": "Got a 400 error at this time, skipping.",'
                        b'"channels": []'
                        b'}')
                else:
                    raise
            with open(cache_path, 'wb') as f:
                f.write(result)
            return result

    def remove_stale_cache(self, todaydate):
        for p in self.cache_dir.glob('*'):
            try:
                cachedate = datetime.datetime.strptime(str(p.name), "%Y-%m-%d")
                todaysdate = datetime.datetime.strptime(str(todaydate), "%Y-%m-%d")
                if cachedate >= todaysdate:
                    continue
            except Exception as e:
                print(e)
                pass
            print('Removing stale cache file:', p.name)
            p.unlink()

    def locast_xmltime(self, tm):
        tm = datetime.datetime.fromtimestamp(tm/1000.0)
        tm = str(tm.strftime('%Y%m%d%H%M%S')) + " +0000"
        return tm


#!/usr/bin/env python3

import os
import sys

#try:
#	data_mem_f = open(sys.argv[1])
#except IndexError:
#	data_mem_f = open('data_mem.txt')

def xor_reduce_11bit(number):
	# Since xor commutes, this can be simplified to popcount
	## i.e. ^(1_0000_0001) == ^(0_0000_0101) == ^(0_0000_0011) etc
	## Popcount intuition:
	##  Any number of 0 ^ 0 ^ 0 ^ ... is 0
	##
	##  if there are an even number of 1's, then all the ones will
	##  reduce 1 ^ 1 to 0, for a final 0 ^ 0 = 0
	##
	##  if there are an odd number of 1's, then all the ones will
	##  reduce 1 ^ 1 ^ 1 to 1, for a final 1 ^ 0 = 1
	return bin(number).count('1') % 2

# Actually, this more general function is going to be more useful
def xor_reduce_bitstring(bitstring):
	# Strip '0b' prefix if needed
	if bitstring[:2] == '0b':
		bitstring = bitstring[2:]

	r = 0
	for b in bitstring:
		if b == '1':
			r = r ^ 1
		elif b == '0':
			r = r ^ 0
		else:
			raise TypeError
	return r

# This implementation is a pretty literal reading of the program spec
def p1_encode(eleven_bit):
	assert eleven_bit >= 0
	assert eleven_bit < 2**11

	#            109_8765_4321
	p8_mask = 0b_111_1111_0000
	p4_mask = 0b_111_1000_1110
	p2_mask = 0b_110_0110_1101
	p1_mask = 0b_101_0101_1011

	p8 = xor_reduce_bitstring(bin(eleven_bit & p8_mask))
	p4 = xor_reduce_bitstring(bin(eleven_bit & p4_mask))
	p2 = xor_reduce_bitstring(bin(eleven_bit & p2_mask))
	p1 = xor_reduce_bitstring(bin(eleven_bit & p1_mask))

	p16 = xor_reduce_bitstring(bin(eleven_bit) + str(p8) + str(p4) + str(p2) + str(p1))

	high_byte = ((eleven_bit >> 3) & 0xfe) | p8
	low_byte = \
			(((eleven_bit >> 1) & 0x7) << 5) | \
			(((p4             ) & 0x1) << 4) | \
			(((eleven_bit     ) & 0x1) << 3) | \
			(((p2             ) & 0x1) << 2) | \
			(((p1             ) & 0x1) << 1) | \
			(((p16            ) & 0x1) << 0)

	parity = (p16 << 4 | p8 << 3 | p4 << 2 | p2 << 1 | p1)
	#print("1\t{:011b}   {:08b} {:08b}   {:05b}".format(eleven_bit, high_byte, low_byte, parity))

	return (high_byte, low_byte, parity)

# This implementation shows more of what students will hopefully think about
def p2_decode(sixteen_bit):
	assert sixteen_bit >= 0
	assert sixteen_bit < 2**16

	high_byte = (sixteen_bit >> 8) & 0xff
	low_byte = sixteen_bit & 0xff

	# Compute all the things to be able to demonstrate how pieces work

	# What were the `x`mitted parity bits?
	x8 = (sixteen_bit & 0b1_0000_0000) >> 8
	x4 = (sixteen_bit & 0b0_0001_0000) >> 4
	x2 = (sixteen_bit & 0b0_0000_0100) >> 2
	x1 = (sixteen_bit & 0b0_0000_0010) >> 1
	x16 = (sixteen_bit & 0b1)

	xarity = (x16 << 4 | x8 << 3 | x4 << 2 | x2 << 1 | x1)

	# Compute the parity of the received bitstream
	p8  = xor_reduce_bitstring(bin(sixteen_bit & 0b1111_1110_0000_0000))
	p4  = xor_reduce_bitstring(bin(sixteen_bit & 0b1111_0000_1110_0000))
	p2  = xor_reduce_bitstring(bin(sixteen_bit & 0b1100_1100_1100_1000))
	p1  = xor_reduce_bitstring(bin(sixteen_bit & 0b1010_1010_1010_1000))
	# Example of using computed mask, equivalent appraoch for p16
	# i.e., p16 = b11 ^ b8 ^ b6 ^ b5 ^ b3 ^ b2 ^ b1
	# insight: write out p16 as all the xors in p1,2,4,8 and cancel
	#          out duplicate bits; p16 is just a different mask
	# Using this mask is an easier HW implementationn.
	p16 = xor_reduce_bitstring(bin(sixteen_bit & 0b1001_0110_0110_1000))

	parity = (p16 << 4 | p8 << 3 | p4 << 2 | p2 << 1 | p1)

	# Can also extract the (nominal) original message
	eleven_bit = \
			((sixteen_bit & 0b1111_1110_0000_0000) >> 5) | \
			((sixteen_bit & 0b0000_0000_1110_0000) >> 4) | \
			((sixteen_bit & 0b0000_0000_0000_1000) >> 3)

	# If no xmit error, re-encoding will match; if there are errors, it may not
	h,l,p = p1_encode(eleven_bit)

	# This can also double-check our two parity math approaches
	assert p == parity

	# Easiest way to generate this is just pen & paper probably
	# Written as bit index here to match spec notation
	sec_lut = {
			0b1_1111: 11,
			0b0_1110: 10,
			0b0_1101: 9,
			0b1_1100: 8,
			0b0_1011: 7,
			0b1_1010: 6,
			0b1_1001: 5,
			0b0_0111: 4,
			0b1_0110: 3,
			0b1_0101: 2,
			0b1_0011: 1,
	}

	corrected_eleven_bit = eleven_bit
	bad_parity_bits = xarity ^ parity
	if bad_parity_bits != 0:
		# Error Detected
		if bin(bad_parity_bits).count('1') == 1:
			# Single bit mismatch means parity bit flipped; ignore
			pass
		else:
			idx = sec_lut[bad_parity_bits]
			corrected_eleven_bit = eleven_bit ^ (1 << (idx-1))

	# This is useful to show examples
	#print("2\t{:011b}   {:08b} {:08b}   {:05b} {:02X}   {:05b} {:02X}   {:05b} {:02X}   {:011b}".
	#		format(eleven_bit, high_byte, low_byte, xarity, xarity, parity,
	#			parity, bad_parity_bits, bad_parity_bits, corrected_eleven_bit))

	return corrected_eleven_bit

##################
# Simple Examples

to_encode = 0b101_0101_0101
h,l,p = p1_encode(to_encode)

sixteen = h << 8 | l

#print("No Change")
#assert to_encode == p2_decode(sixteen)
#
#print("Single Bit Error")
#assert to_encode == p2_decode(sixteen ^ 0b1000_0000_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0100_0000_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0010_0000_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0001_0000_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_1000_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0100_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0010_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0001_0000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_1000_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0100_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0010_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0001_0000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0000_1000)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0000_0100)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0000_0010)
#assert to_encode == p2_decode(sixteen ^ 0b0000_0000_0000_0001)
#
#print("Some Two Bit Errors?")
#p2_decode(sixteen ^ 0b1100_0000_0000_0000)
#p2_decode(sixteen ^ 0b0110_0000_0000_0000)
#p2_decode(sixteen ^ 0b0011_0000_0000_0000)
#p2_decode(sixteen ^ 0b0001_1000_0000_0000)
#p2_decode(sixteen ^ 0b0000_1100_0000_0000)
#p2_decode(sixteen ^ 0b0000_0110_0000_0000)
#p2_decode(sixteen ^ 0b0000_0011_0000_0000)
#p2_decode(sixteen ^ 0b0000_0001_1000_0000)
#p2_decode(sixteen ^ 0b0000_0000_1100_0000)
#p2_decode(sixteen ^ 0b0000_0000_0110_0000)
#p2_decode(sixteen ^ 0b0000_0000_0011_0000)
#p2_decode(sixteen ^ 0b0000_0000_0001_1000)
#p2_decode(sixteen ^ 0b0000_0000_0000_1100)
#p2_decode(sixteen ^ 0b0000_0000_0000_0110)
#p2_decode(sixteen ^ 0b0000_0000_0000_0011)

# Correctness Testing
print("=== Begin correctness test ===")
for n in range(2**11):
	break
	h,l,p = p1_encode(n)
	sixteen = h << 8 | l

	assert n == p2_decode(sixteen)
	for i in range(2**4):
		try:
			assert n == p2_decode(sixteen ^ (1 << i))
		except AssertionError:
			print('-'*40)
			print("{:011b}   {:016b} {:04X}   {}".format(n, sixteen, sixteen, i))
			print("{:011b}".format(p2_decode(sixteen ^ (1 << i))))
			raise

print("=== P1/P2 All good. ===")

# Expects message_string array of 32 integer bytes and 5-bit pattern
def p3_search(message_string, pattern):
	assert len(message_string) == 32
	assert min(message_string) >= 0
	assert max(message_string) < 2**8

	# Part a
	total_in_bytes = 0
	bytes_with_pattern = 0
	for m in message_string:
		if\
		((m >> 0) & 0x1f) == pattern or\
		((m >> 1) & 0x1f) == pattern or\
		((m >> 2) & 0x1f) == pattern or\
		((m >> 3) & 0x1f) == pattern:
			bytes_with_pattern += 1

		if ((m >> 0) & 0x1f) == pattern:
			total_in_bytes += 1
		if ((m >> 1) & 0x1f) == pattern:
			total_in_bytes += 1
		if ((m >> 2) & 0x1f) == pattern:
			total_in_bytes += 1
		if ((m >> 3) & 0x1f) == pattern:
			total_in_bytes += 1

	# Part b
	message_as_text = ''
	for m in message_string:
		message_as_text += '{:08b}'.format(m)
	pattern_as_text = '{:05b}'.format(pattern)
	#print(message_as_text)
	#print(pattern_as_text)
	total_in_stream = 0
	for i in range(len(message_as_text) - len(pattern_as_text) +1):
		if message_as_text[i:i+len(pattern_as_text)] == pattern_as_text:
			total_in_stream += 1

	return (total_in_bytes, bytes_with_pattern, total_in_stream)

# Spec example
mem = [0 for x in range(32)]
pattern = 0
print(p3_search(mem, pattern))

# Mildly more interesting (0xaa8a repeating)
mem = [x for t in zip(*[[0xaa for x in range(16)], [0x8a for x in range(16)]]) for x in t]
pattern = 0x15
print(p3_search(mem, pattern))


from connection.connection import Connection

class AlumnoRepo:

    def __init__(self, nombres, apellido_paterno, apellido_materno, fecha_nacimiento, dni, correo, telefono):
        self.nombres = nombres
        self.apellido_paterno = apellido_paterno
        self.apellido_materno = apellido_materno
        self.fecha_nacimiento = fecha_nacimiento
        self.dni = dni
        self.correo = correo
        self.telefono = telefono

    def all_alumnos(self):
        try:
            conn = Connection('alumno')
            records = conn.select([])

            for record in records:
                print(f'ID: {record[0]}')
                print(f'Nombres: {record[1]}')
                print(f'Apellido_Paterno: {record[2]}')
                print(f'Apellido_Materno: {record[3]}')
                print(f'Fecha_Nacimiento: {record[4]}')
                print(f'DNI: {record[5]}')
                print(f'Correo: {record[6]}')
                print(f'Telefono: {record[7]}')
                print(f'Estado: {record[8]}')
                print('=====================')
        except Exception as e:
            print(e)

    def one_alumno(self,id):
        try:
            conn = Connection('alumno')
            records = conn.selectOne(id)

            print(f'ID: {records[0]}')
            print(f'Nombres: {records[1]}')
            print(f'Apellido_Paterno: {records[2]}')
            print(f'Apellido_Materno: {records[3]}')
            print(f'Fecha_Nacimiento: {records[4]}')
            print(f'DNI: {records[5]}')
            print(f'Correo: {records[6]}')
            print(f'Telefono: {records[7]}')
            print(f'Estado: {records[8]}')
            print('=====================')
            return records
        except Exception as ex:
            print(ex)

    def insert_alumno(self, data):

        try:
            conn = Connection('alumno')
            conn.insert(data)

            print(f'se registro correctamente al alumno')

        except Exception as ex:
            print(ex)

    def update_alumno(self, id_object, data):

        conn = Connection('alumno')

        list_where = []
        for field_name, field_value in id_object.items():
            if isinstance(field_value, str):
                field_value = f"'{field_value}'"
            list_where.append(f"{field_name}={field_value}")

        list_update = []
        for field_name, field_value in data.items():
            if isinstance(field_value, str):
                field_value = f"'{field_value}'"
            list_update.append(f"{field_name}={field_value}")

        query = f'''
            UPDATE {conn.table_name} SET {', '.join(list_update)}
            WHERE {' AND '.join(list_where)}
        '''
        conn.execute_query(query)
        return True

    def inhabilitar_alumno(self,parameter, id_object):
        conn = Connection('alumno')
        conn.disable(parameter, id_object)
        return True

    def alumnos_habilitados(self):
        conn = Connection('alumno')
        query = f'''
            SELECT * FROM {conn.table_name} WHERE estado = 'true'
        '''
        conn.cursor.execute(query)
        records = conn.cursor.fetchall()

        for record in records:
                print(f'ID: {record[0]}')
                print(f'Nombres: {record[1]}')
                print(f'Apellido_Paterno: {record[2]}')
                print(f'Apellido_Materno: {record[3]}')
                print(f'Fecha_Nacimiento: {record[4]}')
                print(f'DNI: {record[5]}')
                print(f'Correo: {record[6]}')
                print(f'Telefono: {record[7]}')
                print(f'Estado: {record[8]}')
                print('=====================')


#
# 75. Sort Colors
#
# Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
#  We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
# You must solve this problem without using the library's sort function.
#
# https://leetcode.com/problems/sort-colors/
from typing import List

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        colors = {0: 0, 1: 0, 2: 0}
        for index, value in enumerate(nums):
            colors[value] = colors[value] + 1

        for index, value in enumerate(nums):
            if index <= colors[0]:
                nums[index] = 0
            elif index <= colors[0] + colors[1]:
                nums[index] = 1
            elif index <= colors[0] + colors[1] + colors[2]:
                nums[index] = 2

nums = [0, 1, 2, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 2]

print(nums)
solution = Solution()
solution.sortColors(nums)
print(nums)


import bot_events
print(bot_events)

"""
	명령어와 질의어를 구분한다.
"""

def detect_event(str):
    if '/' not in str:
        print('명령어가 /으로  시작하지않음')
        return False
    else:
        real_message = str.split('/')[1:][0]
        print(f'real_message: {real_message}')

        c_command = real_message.split(' ')[0]
        value = ' '.join(real_message.split(' ')[1:])
        print(f'c_command: {c_command} value: {value}')
        return c_command, value

"""
	명령에 대한 이벤트를 등록한다.
"""

def send_reply(client, value):
    event_data = value
    message = event_data["event"]
    try:
        if message.get("subtype") is None:
            command = message.get("text").lower()
            channel_id = message["channel"]
            c_command, value = detect_event(command)
            print(f'c_command: {c_command}, value: {value}')
            if c_command == 'help':
                blocks = bot_events.get_bot_doc()

                client.chat_postMessage(
                    channel=channel_id,
                    blocks=blocks
                )
            elif c_command == '날씨':
                attachments = bot_events.get_weather(value)

                client.chat_postMessage(
                    channel=channel_id,
                    attachments=attachments
                )
            elif c_command == '결석':
                blocks = bot_events.get_absentable_day(value)

                client.chat_postMessage(
                    channel=channel_id,
                    blocks=blocks
                )
            elif c_command == '블로그':
                blocks = bot_events.get_post()
                client.chat_postMessage(
                    channel=channel_id,
                    blocks=blocks
                )
            else:
                client.chat_postMessage(
                    channel=channel_id,
                    blocks=[{
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": ":warning: *존재하지 않는 명령어입니다!*"
                        }
                    }]
                )
    except:
        channel_id = message["channel"]
        client.chat_postMessage(
            channel=channel_id,
            blocks=[{
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": ":no_entry_sign: *오류가 발생했습니다. 만든이에게 문의해주세요*"
                    }
                }]
        )


class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        chars = {}
        head, ans = 0, 0
        for j in range(len(s)):
            if s[j] in chars:
                head = max(chars[s[j]],head)
            ans = max(ans,j-head+1)   #here add one because in next line j add one
            chars[s[j]] = j+1         #here add one in order to deal with the case "" an empty string, if not add one ans will be 0, if add ans will be 1, leetcode ans is 1
        return ans


# Imports
import socket

# Global Variables
port = None
host = None
socket_conn = None

# Create Socket
def socket_create():
    global host, port, socket_conn
    host = "localhost"
    port = 8888

    try:
        # Create UDP socket
        socket_conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        socket_conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    except socket.error as msg:
        print("Socket creation error: " + str(msg))

# State 0
def state0():
    global host, port, socket_conn

    # Write message
    message = str(input("Enter you message: "))

    # Escape loop
    if message == "quit":
        return None
    else:
        try:
            # Send message
            socket_conn.sendto(message.encode("utf-8"), (host, port))
        except socket.error as msg:
            print("Error sending message: " + str(msg))

    return state0

# Main
if __name__ == "__main__":
    # Create socket
    socket_create()

    # Initial state
    state = state0

    while state:
        # Loop FSM
        state = state()

    # Close socket
    socket_conn.close()


import logging
from pathlib import Path
from dotenv import find_dotenv, load_dotenv
import pandas as pd
from src.models.predict_model import predict_model
from kafka import KafkaConsumer
import warnings

warnings.filterwarnings('ignore')

## convert dataframe to csv files
def convert_record(result):
    # append data frame to CSV file
    return result.to_csv(r"E:\uOttawa\SecondTerm\AI for CS\Assignments_2\assignment2-A-Fares\data\final_output_dataset.csv", index=False)

def main():
    """ Runs data processing scripts to turn raw data from (../raw) into
        cleaned data ready to be analyzed (saved in ../processed).
    """
    logger = logging.getLogger(__name__)
    logger.info('making final data set from raw data')

    """
    run the training model to make pickle files for the saved model
    Args: training data csv file

    warning: try training by uncomment this line
    """
    ## get the training data from the csv to df
    # df = pd.read_csv(r"..\src\data\training_dataset.csv")

    ## run the kafka consumer
    consumer = KafkaConsumer(
        'ml-raw-dns',
        bootstrap_servers=['localhost:9092'],
        auto_offset_reset='earliest',
        enable_auto_commit=False
    )

    ## init result list, which appended the dataframe line by line
    result = []

    # Ingesting data from input topic
    for i, message in enumerate(consumer):
        if i < 100000:
            domain = message.value.decode("utf-8")
            result.append(predict_model(domain))
        else:
            break

    # concat the lists of dataframes to one dataframe
    result_df = pd.concat(result, ignore_index=True)
    convert_record(result_df)

if __name__ == '__main__':
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)

    # not used in this stub but often useful for finding various files
    project_dir = Path(__file__).resolve().parents[2]

    # find .env automagically by walking up directories until it's found, then
    # load up the .env entries as environment variables
    load_dotenv(find_dotenv())

    main()


from p56 import *
from scipy.linalg import block_diag

def formBlockMatrix(nBlocks, bSize, bCorr):
    block = np.ones((bSize, bSize)) * bCorr
    block[range(bSize), range(bSize)] = 1
    corr = block_diag(*([block] * nBlocks))
    return corr

def corr2cov(corr, std):
    cov = corr * np.outer(std, std)
    return cov

def formTrueMatrix(nBlocks, bSize, bCorr):
    corr0 = formBlockMatrix(nBlocks, bSize, bCorr)
    corr0 = pd.DataFrame(corr0)
    cols = corr0.columns.tolist()
    np.random.shuffle(cols)
    # 열 -> 행 총 두번 섞는 효과
    corr0 = corr0[cols].loc[cols].copy(deep=True)
    std0 = np.random.uniform(.05, .2, corr0.shape[0])
    cov0 = corr2cov(corr0, std0)
    # 평균과 분산이 std0인 분포에서 랜덤 추출
    mu0 = np.random.normal(std0, std0, cov0.shape[0])
    return mu0, cov0

nBlocks, bSize, bCorr = 10, 50, 0.5
np.random.seed(0)
mu0, cov0 = formTrueMatrix(nBlocks, bSize, bCorr)


import collections
import numpy

# with open("outcomes.csv", "w") as outfile:

# 	# C H A E C H E

# 	matches = []
# 	for c1 in [1, -1]:
# 		for h1 in [1, -1, 0]:
# 			for a1 in [1, -1, 0]:
# 				for e1 in [1, -1]:
# 					for c2 in [1, -1]:
# 						for h2 in [1, -1, 0]:
# 							for e2 in [1, -1]:
# 								for c3 in [1, -1]:
# 									for a2 in [1, -1, 0]:
# 										for h3 in [1, -1, 0]:
# 											for e3 in [1, -1]:
# 												for a3 in [1, -1, 0]:
# 													match = [c1, h1, a1, e1, c2, h2, e2, c3, h3, a2, e3, a3]
# 													for i in range(4, 13):
# 														if i == 13:
# 															print(i)
# 														counter = collections.Counter(match[0:i])

# 														if counter[1] == 4:
# 															if counter[-1] < 4:
# 																if not(match[0:i] in matches):
# 																	matches.append(match[0:i])
# 																break

# 	for m in matches:
# 		csvstring = ""
# 		for i in m:
# 			csvstring += str(i) + ","
# 		outfile.write(csvstring + "\n")

matches = []
def match_won(match):
	counter = collections.Counter(match)
	if counter[1] == 4:
		if counter[-1] < 4:
			return True
	else:
		return False

def match_valid(m):
	nodraws = [0,3,4,6,7,10]
	for i in nodraws:
		if len(m) > i:
			if m[i] == 0:
				return False
	return True

def convert_matches_ft4(matches, probs):
	result = []
	for m in matches:
		m1 = []
		order = [
			"control",
			"hybrid",
			"assault",
			"escort",
			"control",
			"hybrid",
			"escort",
			"control",
			"hybrid",
			"assault",
			"escort",
			"assault"
		]
		for i in range(len(m)):
			m1.append(probs[order[i]][m[i]])
		result.append(m1)
	return result

def convert_matches_rs(matches, probs):
	result = []
	for m in matches:
		m1 = []
		order = [
			"control",
			"assault",
			"hybrid",
			"escort",
			"control"
		]
		for i in range(len(m)):
			m1.append(probs[order[i]][m[i]])
		result.append(m1)
	return result

def get_wins_ft4(match = []):
	q = []
	w = []
	q.append(match)
	while len(q) > 0:
		m = q.pop(0)
		if match_valid(m):
			if match_won(m):
				w.append(m)
			elif len(m) == 12:
				continue
			else:
				for i in [1, 0, -1]:
					m1 = m.copy()
					m1.append(i)
					q.append(m1)
	return w

def calc_prob_win_ft4(probs, matches):
	mymatches = convert_matches_ft4(matches, probs)

	s = 0
	products = []
	for m in mymatches:
		products.append(numpy.prod(m))
	products.sort()
	for p in products:
		s += p
	return s

def get_wins_rs(match = []):
	w = []
	for c in [1,-1]:
		for a in [1,-1, 0]:
			for h in [1,-1, 0]:
				for e in [1,-1]:
					match = [c,a,h,e]
					if sum(match) == 0:
						match.append(1)
					if sum(match) > 0:
						w.append(match)
	return w

def calc_prob_win_rs(probs, matches):
	mymatches = convert_matches_rs(matches, probs)
	s = 0
	products = []
	for m in mymatches:
		products.append(numpy.prod(m))
	products.sort()
	for p in products:
		s += p
	return s

# def build_tree(n = {"length":1, "match":[]}):
# 	order = [
# 		"control",
# 		"hybrid",
# 		"assault",
# 		"escort",
# 		"control",
# 		"hybrid",
# 		"escort",
# 		"control",
# 		"hybrid",
# 		"assault",
# 		"escort",
# 		"assault"
# 	]
# 	if n["length"] < 12:
# 		for i in [1, 0, -1]:
# 			m = n["match"].copy()
# 			m.append(i)
# 			if match_valid(m):
# 				l = n["length"] + 1
# 				n[i] = {"length":l, "match":m}
# 				n[i]["maptype"] = order[l - 1]
# 				build_tree(n[i])
# 	return n

# def print_tree(t, spaces):
# 	if -1 in t.keys():
# 		print(spaces+str(-1) )
# 		print_tree(t[-1], spaces + "  ")
# 		print(spaces+str(0) )
# 		print_tree(t[0], spaces + "  ")
# 		print(spaces+str(1) )
# 		print_tree(t[1], spaces + "  ")

# def traverse_tree(t, m = []):
# 	for i in [-1, 0, 1]:
# 		sum = 0
# 		if i in t.keys():
# 			if(match_won(t[i]["match"])):
# 				print(t[i]["match"])
# 			sum = sum + traverse_tree(t[i])

# def print_tree_matches(t, m):
# 	for i in [-1, 0, 1]:
# 		if i in t.keys():
# 			m1 = m.copy()
# 			m1.append(i)
# 			print_tree_matches(t[i], m1)
# 		else:
# 			if match_won(m):
# 				print(m)

# t = build_tree()

# traverse_tree(t)


import streamlit as st
import pandas as pd
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
from textblob import TextBlob
from flair.models import TextClassifier
from flair.data import Sentence
import altair as alt
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load the text file
with open('redit.txt', 'r') as file:
    text = file.read()

# Define a function to perform sentiment analysis using VADER
def vader_sentiment(text):
    nltk.download('vader_lexicon')
    sia = SentimentIntensityAnalyzer()
    vader_score = sia.polarity_scores(text)
    return vader_score['compound']

# Define a function to perform sentiment analysis using TextBlob
def textblob_sentiment(text):
    textblob_score = TextBlob(text).sentiment.polarity
    return textblob_score

# Define a function to perform sentiment analysis using Flair
def flair_sentiment(text):
    classifier = TextClassifier.load('en-sentiment')
    sentence = Sentence(text)
    classifier.predict(sentence)
    flair_score = sentence.labels[0].score
    return flair_score

# Define the options to switch between the models
options = ['VADER', 'TextBlob', 'Flair']

# Display the model selection dropdown
model_name = st.sidebar.selectbox('Select a model', options)

# Perform sentiment analysis using the selected model
if model_name == 'VADER':
    score = vader_sentiment(text)
elif model_name == 'TextBlob':
    score = textblob_sentiment(text)
else:
    score = flair_sentiment(text)

# Display the score
st.write(f'{model_name} score: {score}')

# Visualize the scores using a bar chart
scores = pd.DataFrame({
    'Model': options,
    'Score': [vader_sentiment(text), textblob_sentiment(text), flair_sentiment(text)]
})
chart = alt.Chart(scores).mark_bar().encode(
    x='Model',
    y='Score'
)
st.altair_chart(chart, use_container_width=True)

# Load the pre-trained model and tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Create the Streamlit web application
st.title("Sentiment Analyser for New Dataset")
text_input = st.text_input("Enter text to analyze", value="")
if st.button("Analyze"):
    # Tokenize the text and convert it to a tensor
    inputs = tokenizer(text_input, padding=True, truncation=True, return_tensors="pt")

    # Perform the sentiment analysis
    outputs = model(**inputs)
    probas = torch.softmax(outputs.logits, dim=1).detach().numpy()[0]
    sentiment_label = "positive" if probas[1] > probas[0] else "negative"

    # Display the result
    st.write(f"The sentiment of the text is {sentiment_label}.")