#!/usr/bin/env python
#coding=utf8

from whacked4 import utils
from whacked4.dehacked import statefilter
from whacked4.ui import editormixin, windows
import copy
import wx

class ThingsFrame(editormixin.EditorMixin, windows.ThingsFrameBase):
    """
    Things editor window.
    """

    # Text control to internal key mappings.
    PROPS_VALUES = {
        windows.THING_VAL_ID: 'id',
        windows.THING_VAL_HEALTH: 'health',
        windows.THING_VAL_SPEED: 'speed',
        windows.THING_VAL_RADIUS: 'radius',
        windows.THING_VAL_HEIGHT: 'height',
        windows.THING_VAL_DAMAGE: 'damage',
        windows.THING_VAL_REACTIONTIME: 'reactionTime',
        windows.THING_VAL_PAINCHANCE: 'painChance',
        windows.THING_VAL_MASS: 'mass',
        windows.THING_VAL_GAME: 'game',
        windows.THING_VAL_SPAWNID: 'spawnId',
        windows.THING_VAL_RESPAWNTIME: 'respawnTime',
        windows.THING_VAL_RENDERSTYLE: 'renderStyle',
        windows.THING_VAL_ALPHA: 'alpha',
        windows.THING_VAL_SCALE: 'scale',
        windows.THING_VAL_DECAL: 'decal',
        windows.THING_VAL_DAMAGEFACTOR: 'damageFactor',
        windows.THING_VAL_GRAVITY: 'gravity',
    }

    # Value types for text control validation.
    PROPS_VALUE_TYPES = {
        windows.THING_VAL_ID: 'int',
        windows.THING_VAL_HEALTH: 'int',
        windows.THING_VAL_SPEED: 'int',
        windows.THING_VAL_RADIUS: 'fixed',
        windows.THING_VAL_HEIGHT: 'fixed',
        windows.THING_VAL_DAMAGE: 'int',
        windows.THING_VAL_REACTIONTIME: 'int',
        windows.THING_VAL_PAINCHANCE: 'int',
        windows.THING_VAL_MASS: 'int',
        windows.THING_VAL_GAME: 'int',
        windows.THING_VAL_SPAWNID: 'int',
        windows.THING_VAL_RESPAWNTIME: 'int',
        windows.THING_VAL_RENDERSTYLE: 'int',
        windows.THING_VAL_ALPHA: 'float',
        windows.THING_VAL_SCALE: 'float',
        windows.THING_VAL_DECAL: 'str',
        windows.THING_VAL_DAMAGEFACTOR: 'float',
        windows.THING_VAL_GRAVITY: 'float',
    }

    # State text control to partial internal key mappings.
    PROPS_STATES = {
        windows.THING_STATE_SPAWN: 'Spawn',
        windows.THING_STATE_WALK: 'Walk',
        windows.THING_STATE_PAIN: 'Pain',
        windows.THING_STATE_MELEE: 'Melee',
        windows.THING_STATE_ATTACK: 'Attack',
        windows.THING_STATE_DEATH: 'Death',
        windows.THING_STATE_EXPLODE: 'Explode',
        windows.THING_STATE_RAISE: 'Raise',
        windows.THING_STATE_CRASH: 'Crash',
        windows.THING_STATE_FREEZE: 'Freeze',
        windows.THING_STATE_BURN: 'Burn'
    }

    # State name label to partial internal key mappings.
    PROPS_STATENAMES = {
        windows.THING_STATENAME_SPAWN: 'Spawn',
        windows.THING_STATENAME_WALK: 'Walk',
        windows.THING_STATENAME_PAIN: 'Pain',
        windows.THING_STATENAME_MELEE: 'Melee',
        windows.THING_STATENAME_ATTACK: 'Attack',
        windows.THING_STATENAME_DEATH: 'Death',
        windows.THING_STATENAME_EXPLODE: 'Explode',
        windows.THING_STATENAME_RAISE: 'Raise',
        windows.THING_STATENAME_CRASH: 'Crash',
        windows.THING_STATENAME_FREEZE: 'Freeze',
        windows.THING_STATENAME_BURN: 'Burn'
    }

    # State set button to partial internal key mappings.
    PROPS_STATESET = {
        windows.THING_STATESET_SPAWN: windows.THING_STATE_SPAWN,
        windows.THING_STATESET_WALK: windows.THING_STATE_WALK,
        windows.THING_STATESET_PAIN: windows.THING_STATE_PAIN,
        windows.THING_STATESET_MELEE: windows.THING_STATE_MELEE,
        windows.THING_STATESET_ATTACK: windows.THING_STATE_ATTACK,
        windows.THING_STATESET_DEATH: windows.THING_STATE_DEATH,
        windows.THING_STATESET_EXPLODE: windows.THING_STATE_EXPLODE,
        windows.THING_STATESET_RAISE: windows.THING_STATE_RAISE,
        windows.THING_STATESET_CRASH: windows.THING_STATE_CRASH,
        windows.THING_STATESET_FREEZE: windows.THING_STATE_FREEZE,
        windows.THING_STATESET_BURN: windows.THING_STATE_BURN
    }

    # Sound text control to partial internal key mappings.
    PROPS_SOUNDS = {
        windows.THING_SOUND_ALERT: 'Alert',
        windows.THING_SOUND_ATTACK: 'Attack',
        windows.THING_SOUND_PAIN: 'Pain',
        windows.THING_SOUND_DEATH: 'Death',
        windows.THING_SOUND_ACTIVE: 'Active'
    }

    # Sound set button to partial internal key mappings.
    PROPS_SOUNDSET = {
        windows.THING_SOUNDSET_ALERT: windows.THING_SOUND_ALERT,
        windows.THING_SOUNDSET_ATTACK: windows.THING_SOUND_ATTACK,
        windows.THING_SOUNDSET_PAIN: windows.THING_SOUND_PAIN,
        windows.THING_SOUNDSET_DEATH: windows.THING_SOUND_DEATH,
        windows.THING_SOUNDSET_ACTIVE: windows.THING_SOUND_ACTIVE
    }

    # State name label to partial internal key mappings.
    PROPS_SOUNDNAMES = {
        windows.THING_SOUNDNAME_ALERT: 'Alert',
        windows.THING_SOUNDNAME_ATTACK: 'Attack',
        windows.THING_SOUNDNAME_PAIN: 'Pain',
        windows.THING_SOUNDNAME_DEATH: 'Death',
        windows.THING_SOUNDNAME_ACTIVE: 'Active'
    }

    # Fixed point unit divisor for certain thing properties.
    FIXED_UNIT = 0x10000

    def __init__(self, params):
        windows.ThingsFrameBase.__init__(self, params)
        editormixin.EditorMixin.__init__(self)

        self.SetIcon(wx.Icon('res/editor-things.ico'))

        self.patch = None
        self.pwads = None
        self.clipboard = None

        self.selected_index = 0

        self.thingflag_mnemonics = None

    def build(self, patch):
        """
        @see EditorMixin.build
        """

        self.patch = patch
        self.pwads = self.GetParent().pwads
        self.clipboard = None

        self.selected_index = 0

        self.thingflag_mnemonics = None

        self.set_feature_visibility()
        self.flaglist_build()
        self.thinglist_build()
        self.game_build()
        self.renderstyle_build()

    def set_feature_visibility(self):
        """
        Sets the visibility of controls based on enabled engine features.
        """

        features = self.patch.engine.features

        self.PanelDamageFactor.Show(('thing.damageFactor' in features))
        self.PanelGravity.Show(('thing.gravity' in features))
        self.PanelGame.Show(('thing.game' in features))
        self.PanelSpawnID.Show(('thing.spawnId' in features))
        self.PanelRespawnTime.Show(('thing.respawnTime' in features))
        self.PanelRenderStyle.Show(('thing.renderStyle' in features))
        self.PanelAlpha.Show(('thing.alpha' in features))
        self.PanelScale.Show(('thing.scale' in features))
        self.PanelDecal.Show(('thing.decal' in features))

        self.PanelStateCrash.Show(('thing.stateCrash' in features))
        self.PanelStateFreeze.Show(('thing.stateFreeze' in features))
        self.PanelStateBurn.Show(('thing.stateBurn' in features))

        self.Layout()

    def game_build(self):
        """
        Builds the list of game types.
        """

        self.ThingGame.Clear()
        self.ThingGame.AppendItems([
            'Any',
            'Doom',
            'Heretic',
            'Hexen',
            'Raven'
        ])

    def renderstyle_build(self):
        """
        Builds the list of render styles.
        """

        self.ThingRenderStyle.Clear()
        self.ThingRenderStyle.AppendItems(self.patch.engine.render_styles.values())

    def thinglist_build(self):
        """
        Builds the thing names list from scratch.
        """

        self.ThingList.ClearAll()

        if self.ThingList.GetColumnCount() == 0:
            self.ThingList.InsertColumn(0, 'Idx', width=36)
            self.ThingList.InsertColumn(1, 'Name', width=225)
            self.ThingList.InsertColumn(2, 'ID', width=46)
            self.ThingList.InsertColumn(3, 'Game', width=50)

        for index in range(len(self.patch.things.names)):
            self.ThingList.InsertStringItem(index, '')
            self.thinglist_update_row(index)

        self.list_autosize(self.ThingList)
        self.ThingList.Select(0, True)

    def thinglist_update_row(self, row_index):
        """
        Updates a thing name in the list.
        """

        thing = self.patch.things[row_index]
        thing_name = self.patch.things.names[row_index]

        self.ThingList.SetItemText(row_index, str(row_index + 1))
        self.ThingList.SetStringItem(row_index, 1, thing_name)
        self.ThingList.SetStringItem(row_index, 2, str(thing['id']))
        self.ThingList.SetStringItem(row_index, 3, thing['game'])

    def thinglist_resize(self, event):
        """
        Resizes the thing name column to match the control's width.
        """

        columns_width = self.ThingList.GetColumnWidth(0)
        columns_width += self.ThingList.GetColumnWidth(2) + self.ThingList.GetColumnWidth(3)

        width = self.ThingList.GetClientSizeTuple()[0] - columns_width - 4
        self.ThingList.SetColumnWidth(1, width)

    def flaglist_build(self):
        """
        Build the flags list from scratch.
        """

        flaglist = []
        self.thingflag_mnemonics = []

        for mnemonic, flag in self.patch.engine.things.flags.iteritems():
            if 'alias' in flag:
                continue
            flaglist.append(' ' + flag['name'])
            self.thingflag_mnemonics.append(mnemonic)

        self.ThingFlags.SetItems(flaglist)

    def activate(self, event):
        """
        Called when this editor window is activated by the user.
        """

        # Call the editor mixin function that we are overriding.
        editormixin.EditorMixin.activate(self, event)

        if self.IsBeingDeleted():
            return

        # Update the properties being displayed by this window, in case state or sound names have changed.
        self.update_properties()
        self.Layout()

    def edit_copy(self):
        """
        Copies the currently selected thing to this windows' clipboard.
        """

        self.clipboard = {
            'thing': copy.deepcopy(self.patch.things[self.selected_index]),
            'name': copy.copy(self.patch.things.names[self.selected_index])
        }

    def edit_paste(self):
        """
        Pastes the thing on this windows' clipboard over the currently selected thing.
        """

        if self.clipboard is None:
            return

        self.undo_add()

        dup = copy.deepcopy(self.clipboard['thing'])
        dup_name = copy.copy(self.clipboard['name'])

        # Copy references to newly duplicated thing.
        self.patch.things[self.selected_index] = dup
        self.patch.things.names[self.selected_index] = dup_name

        self.update_properties()
        self.is_modified(True)

    def update_properties(self):
        """
        Update the visible properties of the currently selected thing.
        """

        thing = self.patch.things[self.selected_index]
        renderstyle = self.patch.engine.render_styles[thing['renderStyle']]

        # Set basic property text control values.
        self.ThingId.ChangeValue(str(thing['id']))
        self.ThingHealth.ChangeValue(str(thing['health']))
        self.ThingRadius.ChangeValue(str(thing['radius'] / self.FIXED_UNIT))
        self.ThingHeight.ChangeValue(str(thing['height'] / self.FIXED_UNIT))
        self.ThingDamage.ChangeValue(str(thing['damage']))
        self.ThingReactionTime.ChangeValue(str(thing['reactionTime']))
        self.ThingPainChance.ChangeValue(str(thing['painChance']))
        self.ThingMass.ChangeValue(str(thing['mass']))
        self.ThingGame.SetSelection(self.ThingGame.FindString(thing['game']))
        self.ThingSpawnId.ChangeValue(str(thing['spawnId']))
        self.ThingRespawnTime.ChangeValue(str(thing['respawnTime']))
        self.ThingRenderStyle.SetSelection(self.ThingRenderStyle.FindString(renderstyle))
        self.ThingAlpha.ChangeValue(str(thing['alpha']))
        self.ThingScale.ChangeValue(str(thing['scale']))
        self.ThingDecal.ChangeValue(thing['decal'])
        self.ThingDamageFactor.ChangeValue(str(thing['damageFactor']))
        self.ThingGravity.ChangeValue(str(thing['gravity']))

        # Speed is in fixed point if this thing is a missile, normal otherwise
        if 'MISSILE' in thing['flags']:
            speed = thing['speed'] / self.FIXED_UNIT
        else:
            speed = thing['speed']
        self.ThingSpeed.ChangeValue(str(speed))

        # Set flags.
        flags = thing['flags']
        for index in range(self.ThingFlags.GetCount()):
            mnemonic = self.thingflag_mnemonics[index]
            checked = (mnemonic in flags)
            self.ThingFlags.Check(index, checked)

        # Set state and sound values.
        for name in self.PROPS_STATES.values():
            self.set_display_state(name)
        for name in self.PROPS_SOUNDS.values():
            self.set_display_sound(name)

        # Update name.
        self.thinglist_update_row(self.selected_index)

    def set_display_state(self, state_name):
        """
        Sets state control values based on the partial name of a state thing property.
        """

        thing = self.patch.things[self.selected_index]
        state_index = thing['state' + state_name]
        self.__dict__['ThingState' + state_name].ChangeValue(str(state_index))
        self.__dict__['ThingState' + state_name + 'Name'].SetLabel(self.patch.get_state_name(state_index))

    def set_display_sound(self, sound_name):
        """
        Sets sound control values based on the partial name of a sound thing property.
        """

        thing = self.patch.things[self.selected_index]
        sound_index = thing['sound' + sound_name]
        self.__dict__['ThingSound' + sound_name].ChangeValue(str(sound_index))
        self.__dict__['ThingSound' + sound_name + 'Name'].SetLabel(self.patch.get_sound_name(sound_index))

    def set_value(self, event):
        """
        Sets the currently selected thing's property value.

        Which thing property to change is determined by the text control's id and the PROPS_VALUES lookup table.
        """

        self.undo_add()

        window_id = event.GetId()
        window = self.FindWindowById(window_id)
        value_type = self.PROPS_VALUE_TYPES[window_id]
        if value_type == 'int':
            value = utils.validate_numeric(window)
        elif value_type == 'float' or value_type == 'fixed':
            value = utils.validate_numeric_float(window)
        else:
            value = window.GetValue()

        thing = self.patch.things[self.selected_index]

        # Apply fixed point divisor if the value needs it.
        # This is also necessary for the speed property if the thing has it's MISSILE flag set.
        key = self.PROPS_VALUES[window_id]
        if value_type == 'fixed':
            value *= self.FIXED_UNIT
        elif key == 'speed' and 'MISSILE' in thing['flags']:
            value *= self.FIXED_UNIT

        thing[key] = value

        self.thinglist_update_row(self.selected_index)
        self.is_modified(True)

    def set_state(self, event):
        """
        Sets the currently selected thing's property value.

        Which thing property to change is determined by the text control's id and the PROPS_VALUES lookup table.
        """

        self.undo_add()

        window_id = event.GetId()
        window = self.FindWindowById(window_id)
        value = utils.validate_numeric(window)

        thing = self.patch.things[self.selected_index]

        # Clamp to valid state indices.
        if value < 0:
            value = 0
        if value >= len(self.patch.states):
            value = len(self.patch.states) - 1

        if str(value) != window.GetValue():
            window.ChangeValue(str(value))

        key = self.PROPS_STATES[window_id]
        thing['state' + key] = value
        self.__dict__['ThingState' + key + 'Name'].SetLabel(self.patch.get_state_name(value))
        self.is_modified(True)

    def set_game(self, event):
        """
        Sets the currently selected thing's game property.
        """

        game = event.GetString()
        self.patch.things[self.selected_index]['game'] = game

        self.thinglist_update_row(self.selected_index)
        self.is_modified(True)

    def set_renderstyle(self, event):
        """
        Sets the currently selected thing's render style property.
        """

        index = event.GetSelection()
        renderstyle = self.patch.engine.render_styles.keys()[index]
        self.patch.things[self.selected_index]['renderStyle'] = renderstyle

        self.is_modified(True)

    def set_sound(self, event):
        """
        Sets the currently selected thing's sound index.

        Which sound to change is determined by the text control's id and the PROPS_SOUNDS lookup table.
        """

        self.undo_add()

        window_id = event.GetId()
        window = self.FindWindowById(window_id)
        value = utils.validate_numeric(window)

        thing = self.patch.things[self.selected_index]

        # Clamp to valid sound indices.
        if value < 0:
            value = 0
        if value > len(self.patch.sounds):
            value = len(self.patch.sounds)

        if str(value) != window.GetValue():
            window.ChangeValue(str(value))

        key = self.PROPS_SOUNDS[window_id]
        thing['sound' + key] = value
        self.__dict__['ThingSound' + key + 'Name'].SetLabel(self.patch.get_sound_name(value))
        self.is_modified(True)

    def set_flags(self, event):
        """
        Sets the currently selected thing's flags value.
        """

        self.undo_add()

        flags_set = set()
        projectile_before = 'MISSILE' in self.patch.things[self.selected_index]['flags']

        # Iterate over the flags defined in the flags list, adding mnemonics that are set.
        for index in range(self.ThingFlags.GetCount()):
            if not self.ThingFlags.IsChecked(index):
                continue
            flags_set.add(self.thingflag_mnemonics[index])

        # Recast the fixed point speed property if the thing is now a projectile (or not anymore).
        projectile_after = 'MISSILE' in flags_set
        if projectile_before != projectile_after:
            if projectile_after:
                self.patch.things[self.selected_index]['speed'] *= self.FIXED_UNIT
            else:
                self.patch.things[self.selected_index]['speed'] /= self.FIXED_UNIT

        self.patch.things[self.selected_index]['flags'] = flags_set
        self.is_modified(True)

    def set_flag_tooltip(self, event):
        """
        Updates the tooltip displayed for the flags list when hovering over a flag.
        """

        index = self.ThingFlags.HitTest(wx.Point(event.GetX(), event.GetY()))
        if index != wx.NOT_FOUND:
            mnemonic = self.thingflag_mnemonics[index]
            tip = '{}\n\nMnemonic: {}'.format(self.patch.engine.things.flags[mnemonic]['description'], mnemonic)
        else:
            tip = ''

        self.ThingFlags.SetToolTipString(tip)

    def set_state_external(self, event):
        """
        Sets a state property based on the state that is currently selected in the states editor.
        """

        self.undo_add()

        # Get a reference to the states editor window.
        parent = self.GetParent()
        states_frame = parent.editor_windows[windows.MAIN_TOOL_STATES]

        text_ctrl = self.FindWindowById(self.PROPS_STATESET[event.GetId()])
        text_ctrl.SetValue(str(states_frame.selection_get_state_index()))
        self.is_modified(True)

    def set_sound_external(self, event):
        """
        Sets a sound property based on the sound that is currently selected in the sounds editor.
        """

        self.undo_add()

        # Get a reference to the states editor window.
        parent = self.GetParent()
        sounds_frame = parent.editor_windows[windows.MAIN_TOOL_SOUNDS]

        text_ctrl = self.FindWindowById(self.PROPS_SOUNDSET[event.GetId()])
        text_ctrl.SetValue(str(sounds_frame.selected_row))
        self.is_modified(True)

    def thing_select(self, event):
        """
        Called when a thing is selected from the thing names list.
        """

        self.selected_index = event.GetIndex()
        self.update_properties()

    def thing_rename(self, event):
        """
        Called when the current thing needs to be renamed.
        """

        self.thing_rename_action()

    def thing_rename_action(self):
        """
        Renames the currently selected thing.
        """

        thing_name = self.patch.things.names[self.selected_index]
        new_name = wx.GetTextFromUser('Enter a new name for ' + thing_name, caption='Change name',
                                      default_value=thing_name, parent=self)

        if new_name != '':
            self.undo_add()

            self.patch.things.names[self.selected_index] = new_name

            self.update_properties()
            self.is_modified(True)

            parent = self.GetParent()
            parent.editor_windows[windows.MAIN_TOOL_STATES].update_filterlist()

    def thing_restore(self, event):
        """
        Restores the currently selected thing to the one stored in the engine configuration.
        """

        self.undo_add()

        self.patch.things[self.selected_index] = self.patch.engine.things[self.selected_index].clone()
        self.patch.things.names[self.selected_index] = copy.copy(self.patch.engine.things.names[self.selected_index])

        self.update_properties()
        self.is_modified(True)

    def goto_state_event(self, event):
        """
        Changes the selected state in the states editor window to the one of a thing's state property.
        """

        key = self.PROPS_STATENAMES[event.GetId()]
        state_index = self.patch.things[self.selected_index]['state' + key]

        self.goto_state(state_index, statefilter.FILTER_TYPE_THING, self.selected_index)

    def goto_sound_event(self, event):
        """
        Changes the selected sound in the sounds editor window to the one of a thing's sound property.
        """

        key = self.PROPS_SOUNDNAMES[event.GetId()]
        sound_index = self.patch.things[self.selected_index]['sound' + key]

        self.goto_sound(sound_index)

    def sound_play(self, event):
        """
        Plays a sound entry.
        """

        key = self.PROPS_SOUNDNAMES[event.GetId()]
        sound_index = self.patch.things[self.selected_index]['sound' + key]
        if sound_index == 0:
            return

        utils.sound_play(self.patch.sound_names[sound_index - 1], self.pwads)

    def undo_restore_item(self, item):
        """
        @see EditorMixin.undo_restore_item
        """

        index = item['index']

        self.patch.things[index] = item['item']
        self.patch.things.names[index] = item['name']

        self.thinglist_update_row(index)
        self.update_properties()

        self.is_modified(True)

    def undo_store_item(self):
        """
        @see EditorMixin.undo_store_item
        """

        return {
            'item': self.patch.things[self.selected_index].clone(),
            'name': copy.copy(self.patch.things.names[self.selected_index]),
            'index': self.selected_index
        }


import pandas as pd
import numpy as np
from datetime import date
from config import DISTRIBUTOR_WATCHLIST, MODELS_WATCHLIST, LOCATIONS, INPUT_FILE, OUTPUT_LOCATION

def filter_demo_models(initial_models):
    models = []

    for model in initial_models:
        if 'demo' not in model.lower():
            models.append(model)

    return models

def filter_distributors(initial_distributors):
    distributors = []
    if not DISTRIBUTOR_WATCHLIST:
        return initial_distributors

    for distributor in initial_distributors:
        for watchlist in DISTRIBUTOR_WATCHLIST:
            if watchlist.lower() in distributor.lower():
                distributors.append(distributor)
    return distributors

def generate_dos_template(filtered_df, value_to_use):
    dos_table = pd.pivot_table(filtered_df, values=value_to_use, index=['Territory', 'Statistics Model'],
                        columns=['Distributor'], aggfunc=np.sum, fill_value=0)
    dos_table = dos_table.reset_index()
    dos_table.index.name = None

    d = {'Statistics Model': MODELS_WATCHLIST}
    watchlist_df = pd.DataFrame(data=d)
    for territory in LOCATIONS:
        watchlist_df['Territory'] = territory
        dos_table = pd.merge(dos_table, watchlist_df, how='outer', on=['Territory', 'Statistics Model'])

    dos_table = dos_table.sort_values(["Territory", "Statistics Model"])
    dos_table = dos_table.replace(np.nan, 0)
    return dos_table

def generate_dos_file(filtered_df, date):
    filtered_df.loc[~filtered_df["Statistics Model"].isin(MODELS_WATCHLIST), "Statistics Model"] = "Other Models"

    inventory_dos = generate_dos_template(filtered_df, 'Distributor Stock')
    sales_dos = generate_dos_template(filtered_df, 'Sales Volume')

    writer = pd.ExcelWriter(f'{OUTPUT_LOCATION}/National-DOS-{date}.xlsx', engine='openpyxl')
    inventory_dos.to_excel(writer, sheet_name='Inventory', index=False)
    sales_dos.to_excel(writer, sheet_name='Sales', index=False)
    writer.save()

def generate_distributor_file(filtered_df, date):
    for key, value in filtered_df.groupby('Distributor'):
        writer = pd.ExcelWriter(f'{OUTPUT_LOCATION}/{key.lower().replace(" ", "-")}-{date}.xlsx', engine='openpyxl')
        for territory, territory_df in value.groupby('Territory'):
            territory_pivot = pd.pivot_table(territory_df, values='Distributor Stock', index=['Statistics Model'], columns=['Store/Warehouse'], aggfunc=np.sum, fill_value=0)
            territory_pivot = territory_pivot.replace(0, np.nan)
            territory_pivot.to_excel(writer, sheet_name=territory)
        writer.save()

def get_raw_data():
    """
        Get initial raw data from the file location in config.py.
        Will also do some initial preprocessing.
        (remove last row and make negative data to zero)
    """
    df = pd.read_excel(f'{INPUT_FILE}')
    df = df[:-1]
    num = df._get_numeric_data()
    num[num < 0] = 0
    df = df.fillna(0)
    return df

def main():
    today = date.today().strftime("%b-%d-%Y")

    df = get_raw_data()
    distributors = filter_distributors(df.Distributor.unique().tolist())
    models = filter_demo_models(df['Statistics Model'].unique().tolist())

    filtered_df = df[(df['Distributor'].isin(distributors)) & (df['Statistics Model'].isin(models))]
    generate_dos_file(filtered_df, today)
    generate_distributor_file(filtered_df, today)

if __name__ == "__main__":
    main()


from meta_policy_search.envs.base import MetaEnv

import numpy as np
from gym.spaces import Box

class MetaPointEnvMomentumModes(MetaEnv):
    """
    Simple 2D point meta environment. Each meta-task corresponds to a different goal / corner
    (one of the 4 points (-2,-2), (-2, 2), (2, -2), (2,2)) which are sampled with equal probability
    """

    def __init__(self, reward_type='dense', sparse_reward_radius=2):
        assert reward_type in ['dense', 'dense_squared', 'sparse']
        self.reward_type = reward_type
        print("Point Env reward type is", reward_type)
        self.sparse_reward_radius = sparse_reward_radius
        self.corners = np.array(
            [[-5, -5], [5, -5], [-5, 5], [5, 5]], dtype=np.float32)
        self.noise_scale = 2.0
        self.set_task(self.sample_tasks(1)[0])
        self.observation_space = Box(low=-np.inf, high=np.inf, shape=(4,))
        self.action_space = Box(low=-0.1, high=0.1, shape=(2,))

    def step(self, action):
        """
        Run one timestep of the environment's dynamics. When end of episode
        is reached, reset() should be called to reset the environment's internal state.

        Args:
            action : an action provided by the environment
        Returns:
            (observation, reward, done, info)
            observation : agent's observation of the current environment
            reward [Float] : amount of reward due to the previous action
            done : a boolean, indicating whether the episode has ended
            info : a dictionary containing other diagnostic information from the previous action
        """
        prev_state = self._state
        self._velocity += np.clip(action, -0.1, 0.1)
        self._state = prev_state + self._velocity
        reward = self.reward(prev_state, action, self._state)
        done = False # self.done(self._state)
        next_observation = np.hstack((self._state, self._velocity))
        return next_observation, reward, done, {}

    def reset(self):
        """
        Resets the state of the environment, returning an initial observation.
        Outputs
        -------
        observation : the initial observation of the space. (Initial reward is assumed to be 0.)
        """
        self._state = np.random.uniform(-0.2, 0.2, size=(2,))
        self._velocity = np.random.uniform(-0.1, 0.1, size=(2,))
        observation = np.hstack((self._state, self._velocity))
        return observation

    def done(self, obs):
        if obs.ndim == 1:
            return self.done(np.array([obs]))
        elif obs.ndim == 2:
            goal_distance = np.linalg.norm(obs[:2] - self.goal_pos[None,:], axis=1)
            return np.max(self._state) > 3

    def reward(self, obs, act, obs_next):
        if obs_next.ndim == 2:
            goal_distance = np.linalg.norm(obs_next[:2] - self.goal_pos[None,:], axis=1)[0]
            if self.reward_type == 'dense':
                return - goal_distance
            elif self.reward_type == 'dense_squared':
                return - goal_distance**2
            elif self.reward_type == 'sparse':
                return np.maximum(self.sparse_reward_radius - goal_distance, 0)

        elif obs_next.ndim == 1:
            return self.reward(np.array([obs]), np.array([act]), np.array([obs_next]))
        else:
            raise NotImplementedError

    def log_diagnostics(self, *args):
        pass

    def sample_tasks(self, n_tasks):
        goals = self.corners[np.random.randint(0, 4, n_tasks)]
        goals += np.random.randn(n_tasks, 2)
        return goals

    def set_task(self, task):
        """
        Args:
            task: task of the meta-learning environment
        """
        self.goal_pos = task

    def get_task(self):
        """
        Returns:
            task: task of the meta-learning environment
        """
        return self.goal_pos

if __name__ == "__main__":
    env = MetaPointEnvMomentumModes()
    while True:
        env.set_task(env.sample_tasks(1)[0])
        env.reset()
        for _ in range(100):
            # env.render()
            _, reward, _, _ = env.step(env.action_space.sample())  # take a random action
            print(reward, env.goal_pos)


import sys
import datetime

class logger(object):
    instance = None

    def __init__(self, tag):
        self.tag = str(tag)

    def prn(self, msg, prefix='> ', endl='\n', args=[]):
        msg = '%s [%s][%s] %s%s' % (
            prefix, datetime.datetime.now().strftime('%H:%M:%S'), self.tag, msg, endl)
        msg = msg.format(*args,
                         W='\033[97m',  # white
                         R='\033[91m',  # lt red
                         Y='\033[93m',  # lt yellow
                         G='\033[92m',  # lt green
                         C='\033[96m',  # lt cyan
                         B='\033[94m',  # lt blue
                         P='\033[95m',  # lt purple
                         D='\033[37m',  # lt gray
                         d='\033[90m',  # gray
                         r='\033[31m',  # red
                         y='\033[33m',  # yellow
                         g='\033[32m',  # green
                         c='\033[36m',  # cyan
                         b='\033[34m',  # blue
                         p='\033[35m')  # purple

        sys.stdout.write(msg)
        sys.stdout.flush()

    def nfo(self, msg, endl='\n', args=[]):
        self.prn(msg,
                 prefix='{c}# ',
                 endl=endl,
                 args=args)

    def wrn(self, msg, endl='\n', args=[]):
        self.prn(msg,
                 prefix='{y}* ',
                 endl=endl,
                 args=args)

    def err(self, msg, endl='\n', args=[]):
        self.prn(msg,
                 prefix='{r}! ',
                 endl=endl,
                 args=args)

    def dbg(self, msg, endl='\n', args=[]):
        self.prn(msg,
                 prefix='{d}# ',
                 endl=endl,
                 args=args)

    def fatal(self, msg, endl='\n', args=[]):
        self.prn(msg,
                 prefix='{R}! ',
                 endl=endl,
                 args=args)
        raise Exception(msg)

    def checker(self, msg, checker, args=[]):
        res = ' ... {g}SUCCESS' if checker else' ... {r}FAILED'
        self.nfo(msg + res, args=args)

    def progress(self, it, prefix='', size=60):
        count = len(it)

        def _show(_i):
            x = int(size * _i / count)
            sys.stdout.write("%s[%s%s] %i/%i\r" %
                             (prefix, "#" * x, "." * (size - x), _i, count))
            sys.stdout.flush()
        _show(0)
        for i, item in enumerate(it):
            yield item
            _show(i + 1)
        sys.stdout.write("\n")
        sys.stdout.flush()

def get(tag):
    return logger(tag)

# test

if __name__ == '__main__':
    import time
    log = get('LOG')
    log.prn(
        '{r}RED {p} PURPLE {y} YELLOW {g} GREEN {c} CYAN {b} BLUE {d} DARK ')
    log.prn(
        '{R}RED {P} PURPLE {Y} YELLOW {G} GREEN {C} CYAN {B} BLUE {D} DARK ')

    log.dbg('It is debug {g}message')
    log.nfo('It is info  {y}message')
    log.wrn('It is warn  {b}message')
    log.err('It is error {c}message')

    for x in xrange(10):
        log.checker('pass: {B}{0}', (x > 5), args=[x])

    for i in log.progress(range(15), "progress test: ", 80):
        time.sleep(0.4)

#   log.fatal('fatal problem!')


import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from predict_ages import predict
from sklearn.model_selection import cross_val_score

url = '/home/dimitris/con/train.csv'
data = pd.read_csv(url)
test = pd.read_csv('/home/dimitris/con/test.csv')
#make the prediction for the nan ages
age_predictions,indexes = predict(data)
test_predictions,test_indexes = predict(test)
#Manage the outliers ages in Data
#We delete all the NaN values in a sample dataFrame to find the outliers

for i in range(len(indexes)):
    data.set_value(indexes[i],"Age",age_predictions[i])
for i in range(len(test_indexes)):
    test.set_value(test_indexes[i],"Age",test_predictions[i])
ave_fare = test['Fare'].mean()
test=test.fillna(ave_fare)
#Manage the outliers ages in Data

P = np.percentile(data['Age'],[1,95])
data = data[(data['Age']>P[0]) & (data['Age']<P[1])]

#handle the objects types
le = LabelEncoder()

for column in data:
    if data[column].dtype == type(object):
        data[column] = le.fit_transform(data[column].astype(str))
for column in test:
    if test[column].dtype == type(object):
        test[column] = le.fit_transform(test[column].astype(str))

y = data['Survived']
x = data.drop('Survived',axis=1)
seed = 7
x_train,x_test,y_train,y_test = train_test_split(x,y, test_size=0.2,random_state=seed)

model = LogisticRegression()
model.fit(x_train,y_train)
#predict the test data

predictions = model.predict(test)
subs = pd.read_csv('/home/dimitris/con/gender_submission.csv')
subs = subs['Survived']
scores = cross_val_score(model,x,y,cv=10)
print(accuracy_score(subs,predictions),scores)


#Copyright 2015 Phoenix Bioinformatics Corporation. All rights reserved.

from django.conf.urls import patterns, include, url
from django.contrib import admin
from rest_framework import routers

router = routers.DefaultRouter()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^meters/', include('metering.urls')),
    url(r'^parties/', include('party.urls')),
    url(r'^subscriptions/', include('subscription.urls')),
    url(r'^authorizations/', include('authorization.urls')),
    url(r'^partners/', include('partner.urls')),
    url(r'^session-logs/', include('loggingapp.urls')),
    url(r'^credentials/', include('authentication.urls', namespace="authentication")),
    url(r'^apikeys/', include('apikey.urls')),
    url(r'^cookies/', include('cookies.urls')),
)


#!/usr/bin/env python

import os
import argparse
import numpy as np
import pandas as pd
import shutil

import comptools as comp
from comptools.composition_encoding import composition_group_labels, encode_composition_groups

def extract_dataframe(input_file, config, datatype):
    with pd.HDFStore(input_file, mode='r') as store:
        series_size = store.get_storer('NStations').nrows
        # Dictionary of key: pd.Series pairs to get converted to pd.DataFrame
        series_dict = {}
        # For data stored in that can be accessed via a value column
        value_keys = ['IceTopMaxSignal',
                      'IceTopMaxSignalInEdge',
                      'IceTopMaxSignalString',
                      'IceTopNeighbourMaxSignal',
                      'NStations',
                      'StationDensity',
                      'FractionContainment_Laputop_IceTop',
                      'FractionContainment_Laputop_InIce',
                      'passed_IceTopQualityCuts',
                      'passed_InIceQualityCuts',
                      'avg_inice_radius',
                      'std_inice_radius',
                      'median_inice_radius',
                      'frac_outside_one_std_inice_radius',
                      'frac_outside_two_std_inice_radius',
                      ]

        for cut in ['MilliNCascAbove2', 'MilliQtotRatio', 'MilliRloglBelow2', 'NCh_CoincLaputopCleanedPulsesAbove7', 'StochRecoSucceeded']:
            value_keys += ['passed_{}'.format(cut)]

        min_dists = np.arange(0, 1125, 125)
        for min_dist in min_dists:
            value_keys += ['IceTop_charge_beyond_{}m'.format(min_dist)]

        dom_numbers = [1, 15, 30, 45, 60]
        for min_DOM, max_DOM in zip(dom_numbers[:-1], dom_numbers[1:]):
            key = '{}_{}'.format(min_DOM, max_DOM)
            value_keys += ['NChannels_'+key,
                           'NHits_'+key,
                           'InIce_charge_'+key,
                           'max_qfrac_'+key,
                           ]
        key = '1_60'
        value_keys += ['NChannels_'+key,
                       'NHits_'+key,
                       'InIce_charge_'+key,
                       'max_qfrac_'+key,
                       ]
        if datatype == 'sim':
            # Add MC containment
            value_keys += ['FractionContainment_MCPrimary_IceTop',
                           'FractionContainment_MCPrimary_InIce',
                           'angle_MCPrimary_Laputop',
                           ]
        for key in value_keys:
            series_dict[key] = store[key]['value']

        if datatype == 'sim':
            # Get MCPrimary information
            for key in ['x', 'y', 'energy', 'zenith', 'azimuth', 'type']:
                series_dict['MC_{}'.format(key)] = store['MCPrimary'][key]
            # Add simulation set number and corresponding composition
            # sim_num = int(os.path.splitext(input_file)[0].split('_')[-1])
            sim_num = int(os.path.basename(input_file).split('_')[1])
            series_dict['sim'] = pd.Series([sim_num] * series_size, dtype=int)
            composition = comp.simfunctions.sim_to_comp(sim_num)
            series_dict['MC_comp'] = pd.Series([composition] * series_size, dtype=str)
            # Add composition class
            MC_comp_class = 'light' if composition in ['PPlus', 'He4Nucleus'] else 'heavy'
            series_dict['MC_comp_class'] = pd.Series([MC_comp_class] * series_size, dtype=str)

        # Get timing information
        series_dict['start_time_mjd'] = store['I3EventHeader']['time_start_mjd']
        series_dict['end_time_mjd'] = store['I3EventHeader']['time_end_mjd']

        # Get Laputop information
        laputop = store['Laputop']
        laputop_params = store['LaputopParams']
        lap_keys = ['zenith', 'azimuth', 'x', 'y']
        # if datatype == 'data':
        #     lap_keys += ['ra', 'dec']
        for key in lap_keys:
            series_dict['lap_{}'.format(key)] = laputop[key]
        lap_param_keys = ['s50', 's80', 's125', 's180', 's250', 's500',
                          'ndf', 'beta', 'rlogl']
        for key in lap_param_keys:
            series_dict['lap_{}'.format(key)] = laputop_params[key]
        series_dict['lap_energy'] = laputop_params['e_h4a']
        series_dict['lap_chi2'] = laputop_params['chi2'] / laputop_params['ndf']
        series_dict['lap_fitstatus_ok'] = store['lap_fitstatus_ok']['value'].astype(bool)

        # # Get DDDDR information
        # d4r_params = store['I3MuonEnergyLaputopParams']
        # d4r_keys = ['N', 'b', 'gamma', 'peak_energy', 'peak_sigma', 'exists', 'mean', 'median']
        # for key in d4r_keys:
        #     series_dict['d4r_{}'.format(key)] = d4r_params[key]

        series_dict['eloss_1500_standard'] = store['Stoch_Reco']['eloss_1500']
        # Get number of high energy stochastics
        # series_dict['n_he_stoch_standard'] = store['Stoch_Reco']['n_he_stoch']
        # series_dict['n_he_stoch_strong'] = store['Stoch_Reco2']['n_he_stoch']
        # series_dict['eloss_1500_strong'] = store['Stoch_Reco2']['eloss_1500']
        series_dict['mil_rlogl'] = store['MillipedeFitParams']['rlogl']
        series_dict['mil_qtot_measured'] = store['MillipedeFitParams']['qtotal']
        series_dict['mil_qtot_predicted'] = store['MillipedeFitParams']['predicted_qtotal']

        # Construct unique index from run/event/subevent info in I3EventHeader
        runs = store['I3EventHeader']['Run']
        events = store['I3EventHeader']['Event']
        sub_events = store['I3EventHeader']['SubEvent']
        if datatype == 'sim':
            index = ['{}_{}_{}_{}'.format(sim_num, run, event, sub_event)
                     for run, event, sub_event in zip(runs, events, sub_events)]
        else:
            index = ['{}_{}_{}_{}'.format(config, run, event, sub_event)
                     for run, event, sub_event in zip(runs, events, sub_events)]

        # Extract measured charge for each DOM
        charges_list, columns, tank_charges_index = [], [], []
        grouped = store['tank_charge_v_dist'].groupby(['Run', 'Event', 'SubEvent'])
        for name, group in grouped:
            # if not columns:
            #     columns = ['ldf_{}'.format(i) for i in range(len(group['item']))]
            charges_list.append(group['item'].values)
            if datatype == 'sim':
                tank_charges_index.append('{}_{}_{}_{}'.format(sim_num, *name))
            else:
                tank_charges_index.append('{}_{}_{}_{}'.format(config, *name))
        df_tank_charges = pd.DataFrame(charges_list)
        df_tank_charges.index = tank_charges_index

    df = pd.DataFrame(series_dict)
    df.index = index
    for col in df_tank_charges:
        df['ldf_{}'.format(col)] = df_tank_charges[col]

    return df

def add_extra_columns(df, datatype='sim'):
    '''
    Function to add to new columns to DataFrame that are commonly used

    Parameters
    ----------
    df : pandas.DataFrame
        Input DataFrame
    datatype : str, optional
        Specify whether this is a DataFrame based on simulation or data
        (default is 'sim').

    Returns
    -------
    df : pandas.DataFrame
        Returns DataFrame with added columns
    '''
    if datatype == 'sim':
        df['MC_log_energy'] = np.nan_to_num(np.log10(df['MC_energy']))
        # Add composition group labels
        for num_groups in [2, 3, 4]:
            label_key = 'comp_group_{}'.format(num_groups)
            df[label_key] = composition_group_labels(df['MC_comp'],
                                                     num_groups=num_groups)
            target_key = 'comp_target_{}'.format(num_groups)
            df[target_key] = encode_composition_groups(df[label_key],
                                                       num_groups=num_groups)
    # Add log-scale columns to df
    df['lap_log_energy'] = np.nan_to_num(np.log10(df['lap_energy']))
    df['lap_cos_zenith'] = np.cos(df['lap_zenith'])
    for dist in ['50', '80', '125', '180', '250', '500']:
        df['log_s'+dist] = np.log10(df['lap_s'+dist])
    df['log_dEdX'] = np.log10(df['eloss_1500_standard'])

    return df

def process_i3_hdf(files, config, datatype):
    for input_file in files:
        df = extract_dataframe(input_file, config, datatype)
        df = comp.io.apply_quality_cuts(
                df, datatype=datatype, log_energy_min=None,
                log_energy_max=None, verbose=False)
        df = add_extra_columns(df, datatype=datatype)

        yield df

if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description='Converts input hdf5 files (from save_hdf.py) to a well-formatted pandas dataframe object')
    parser.add_argument('-c', '--config', dest='config',
                        default='IC86.2012',
                        help='Detector configuration')
    parser.add_argument('--type', dest='type',
                        choices=['data', 'sim'],
                        default='sim',
                        help='Option to process simulation or data')
    parser.add_argument('-i', '--input', dest='input', nargs='*',
                        help='Path to input hdf5 files')
    parser.add_argument('-o', '--output', dest='output',
                        help='Path to output hdf5 file')
    args = parser.parse_args()

    # Validate input config
    if (args.type == 'sim' and
            args.config not in comp.simfunctions.get_sim_configs()):
        raise ValueError(
            'Invalid simulation config {} entered'.format(args.config))
    elif (args.type == 'data' and
            args.config not in comp.datafunctions.get_data_configs()):
        raise ValueError('Invalid data config {} entered'.format(args.config))

    # Check if running on condor. If so, write to a local directory on the
    # worker node and copy after output file is written.
    if os.getenv('_CONDOR_SCRATCH_DIR'):
        on_condor = True
        local_outdir = os.getenv('_CONDOR_SCRATCH_DIR')
        outfile = os.path.join(local_outdir, os.path.basename(args.output))
    else:
        on_condor = False
        comp.check_output_dir(args.output)
        outfile = args.output

    # Open HDFStore for output hdf5 file
    with pd.HDFStore(outfile, mode='w') as output_store:
        for df in process_i3_hdf(args.input, args.config, args.type):
            # min_itemsize = {'index': 30}
            # if args.type == 'sim':
            #     min_itemsize['MC_comp'] = 15
            #     for num_groups in [2, 3, 4]:
            #         min_itemsize['comp_group_{}'.format(num_groups)] = 15
            # Add dataframe to output_store
            output_store.append('dataframe', df, format='table',
                                data_columns=True, min_itemsize=30)
                                # data_columns=True, min_itemsize=min_itemsize)

    # If on condor, transfer from worker machine to desired destination
    if on_condor:
        comp.check_output_dir(args.output)
        shutil.move(outfile, args.output)


from __future__ import annotations

from typing import List, Optional

from .column import Column

class Table:

    @property
    def _fully_qualified_name(self) -> str:
        terms: List[str] = list()
        # add the schema to the list of terms if it exists
        if self._schema: terms.append(self._schema)
        # add the name to the list of terms if it exists
        if self._name: terms.append(self._name)
        # join the terms with a period character
        value: str = '.'.join(terms)
        return value

    def __init__(self) -> None:
        self._schema: Optional[str] = None
        self._name: Optional[str] = None
        self._columns: List[Column] = list()

    def __create__(self, *, if_not_exists: bool = True) -> str:
        """
        Get the SQL statement responsible for creating the table

        Parameters:
        - if_not_exists (bool):
            if True, includes 'IF NOT EXISTS' in the statement.
        """
        terms: List[str] = list()
        # add the create table command to the list of terms
        terms.append('CREATE TABLE')
        # add the if not exists term to the list of terms if marked
        if if_not_exists: terms.append('IF NOT EXISTS')
        # add the name to the list of terms if it exists
        terms.append(self._fully_qualified_name)
        # join the terms with a space character
        sql: str = ' '.join(terms)

        # get the statement for each column
        columns: List[str] = [column.__sql__() for column in self._columns]
        # join each statement with a comma
        delimited_columns: str = ', '.join(columns)

        return f'{sql} ({delimited_columns})'

    def __select__(self) -> str:
        """
        Get the SQL statement responsible for selecting the table
        """
        terms: List[str] = list()
        # add the select command to the list of terms
        terms.append('SELECT')

        # get the statement for each column
        column_names: List[str] = [column._name for column in self._columns]
        # join each statement with a comma
        delimited_column_names: str = ', '.join(column_names)
        # add the delimited column names to the list of terms
        terms.append(delimited_column_names)

        # add the from command to the list of terms
        terms.append('FROM')
        # add the name to the list of terms if it exists
        if self._name: terms.append(self._fully_qualified_name)
        # join the terms with a space character
        sql: str = ' '.join(terms)

        return f'{sql}'

    def __insert__(self) -> str:
        """
        Get the SQL statement responsible for inserting the table
        """
        terms: List[str] = list()
        # add the create table command to the list of terms
        terms.append('INSERT INTO')
        # add the name to the list of terms if it exists
        if self._name: terms.append(self._fully_qualified_name)
        # join the terms with a space character
        sql: str = ' '.join(terms)

        # get the statement for each column
        column_names: List[str] = [column._name for column in self._columns]
        # join each statement with a comma
        delimited_column_names: str = ', '.join(column_names)

        # get a parameter placeholder for each column
        values: List[str] = ['?' for _ in self._columns]
        # join each statement with a comma
        delimited_values: str = ', '.join(values)

        return  f'{sql} ({delimited_column_names}) VALUES ({delimited_values})'

    def __delete__(self, *, if_exists: bool) -> str:
        """
        Get the SQL statement responsible for deleting the table

        Parameters:
        - if_exists (bool):
            if True, includes 'IF EXISTS' in the statement.
        """
        terms: List[str] = list()
        # add the create table command to the list of terms
        terms.append('DROP TABLE')
        # add the if exists term to the list of terms if marked
        if if_exists: terms.append('IF EXISTS')
        # add the name to the list of terms if it exists
        terms.append(self._fully_qualified_name)
        # join the terms with a space character
        sql: str = ' '.join(terms)

        return f'{sql}'

class TableBuilder():

    def __init__(self) -> None:
        """
        Creates a new instance of the table builder, resetting internal state
        """
        # reset the builder state
        self.__reset__()

    def __reset__(self) -> None:
        """
        Resets the table builder's internal state
        """
        self._table: Table = Table()

    def table(self) -> Table:
        """
        Outputs the table being built and resets the builder's internal state
        """
        # get a copy of the Table instance
        table: Table = self._table
        # reset the builder state
        self.__reset__()
        # return the copied Table instance
        return table

    def setName(self, value: str) -> TableBuilder:
        """
        Sets the name of the table being built
        """
        # set the table's _name property
        self._table._name = value
        # return the builder for call chaining
        return self

    def addColumn(self, value: Column) -> TableBuilder:
        """
        Adds a column to the table being built
        """
        # add the provided column
        self._table._columns.append(value)
        # return the builder for call chaining
        return self


from scipy.special import comb
import numpy as np
import matplotlib.pyplot as plt
import math

def ensemble_error(n_classifier, error):
    k_start = int(math.ceil(n_classifier/2.))
    probs = [comb(n_classifier, k)*
             error**k *
             (1 - error)**(n_classifier - k)
             for k in range(k_start, n_classifier+1)]
    return sum(probs)

error_range = np.arange(0.0, 1.01, 0.01)

ens_errors = [ensemble_error(n_classifier=11, error=error)
             for error in error_range]

plt.plot(error_range, ens_errors,
         label='Ensemble error',
         linewidth=2)
plt.plot(error_range, error_range,
         label='Base error',
         linestyle='--',
         linewidth=2)
plt.xlabel("Base Error")
plt.ylabel("Base/Ensemble Error")
plt.legend(loc="upper left")
plt.grid(alpha=0.5)
plt.show()

np.argmax(np.bincount([0, 0, 1],
          weights=[0.2,0.2,0.6]))

ex1 = np.array([[0.2, 0.1],
                [0.8, 0.2]])
# (0.2 * 0.2) + (0.8 * 0.8)
# (0.1 * 0.2) + (0.2 * 0.8)
np.average(ex1, axis=0, weights=[0.2, 0.8])

ex2 = np.array([[0.9, 0.1],
                [0.8, 0.2],
                [0.4, 0.6]])

p = np.average(ex2, axis=0, weights=[0.2, 0.2, 0.6])
np.argmax(p)

from sklearn import datasets
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline

iris = datasets.load_iris()

X, y = iris.data[50:, [1,2]], iris.target[50:]
le = LabelEncoder()
y = le.fit_transform(y)

X_train, X_test, y_train, y_test = \
        train_test_split(X, y,
                         test_size=0.4,
                         random_state=1,
                         stratify=y)

clf1 = LogisticRegression(solver='liblinear',
                          penalty='l2',
                          C=0.001, random_state=0)

clf2 = DecisionTreeClassifier(max_depth=1,
                              criterion='entropy',
                              random_state=0)

clf3 = KNeighborsClassifier(n_neighbors=1,
                            p=2, metric='minkowski')

pipe1 = Pipeline([['sc', StandardScaler()],
                  ['clf', clf1]])

pipe3 = Pipeline([['sc', StandardScaler()],
                  ['clf', clf3]])

clf_labels = ['Logistic Regression', 'Decision Tree', 'KNN']
print('10-겹 교차 검증: \n')
for clf, label in zip([pipe1, clf2, pipe3], clf_labels):
    scores = cross_val_score(estimator=clf,
                             X=X_train,
                             y=y_train,
                             cv=10, scoring='roc_auc')
    print("ROC AUC: %0.2f (+/- %0.2f) [%s]" %
            (scores.mean(), scores.std(), label))

from model.vote_classifier import MajorityVoteClassifier

mv_clf = MajorityVoteClassifier(classifiers=[pipe1, clf2, pipe3])
clf_labels += ['Majority voting']
all_clf = [pipe1, clf2, pipe3, mv_clf]

for clf, label in zip(all_clf, clf_labels):
    scores = cross_val_score(estimator=clf,
                             X=X_train,
                             y=y_train,
                             cv=10, scoring='roc_auc')
    print('ROC AUC : %0.2f (+/- %0.2f) [%s]'
            % (scores.mean(), scores.std(), label))

from sklearn.metrics import roc_curve, auc
colors = ['black', 'orange', 'blue', 'green']
linestyle = [':', '--', '-', '-']
for clf, label, clr, ls \
                in zip(all_clf, clf_labels, colors, linestyle):
    y_pred = clf.fit(X_train, y_train).predict_proba(X_test)[:,1]
    fpr, tpr, thresholds = roc_curve(y_true=y_test,
                                     y_score=y_pred)
    roc_auc = auc(x=fpr, y=tpr)
    plt.plot(fpr, tpr,
             color=clr,
             linestyle=ls,
             label='%s (auc= %0.2f)' % (label, roc_auc))

plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1],
         linestyle='--',
         color='gray',
         linewidth=2)
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.grid(alpha=0.5)
plt.xlabel('False positive rate (FPR)')
plt.ylabel('True positive rate (TPR)')
plt.show()

sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)

x1_min, x1_max = X_train_std[:, 0].min() - 1, X_train_std[:, 0].max() + 1
x2_min, x2_max = X_train_std[:, 1].min() - 1, X_train_std[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, 0.1),
                       np.arange(x2_min, x2_max, 0.1))

f, axarr = plt.subplots(nrows=2, ncols=2,
                        sharex='col',
                        sharey='row',
                        figsize=(7, 5))

for idx, clf, tt in zip(product([0, 1], [0, 1]), all_clf, clf_labels):
    clf.fit(X_train_std, y_train)
    Z = clf.predict(np.c_[xx1.ravel(), xx2.ravel()])
    Z = Z.reshape(xx1.shape)
    axarr[idx[0], idx[1]].contourf(xx1, xx2, Z, alpha=0.3)
    axarr[idx[0], idx[1]].scatter(x=X_train_std[y_train == 0, 0],
                                  y=X_train_std[y_train == 0, 1],
                                  c='blue',
                                  marker='^',
                                  s=50)
    axarr[idx[0], idx[1]].scatter(x=X_train_std[y_train == 1, 0],
                                  y=X_train_std[y_train == 1, 1],
                                  c='green',
                                  marker='o',
                                  s=50)
    axarr[idx[0], idx[1]].set_title(tt)
plt.text(-3.5, -4.5,
         s='Sepal width [Standardized]',
         ha='center', va='center',
         fontsize=12)
plt.text(-13, 4.5,
         s='Sepal length [Standardized]',
         ha='center', va='center',
         fontsize=12, rotation=90)
plt.show()
#'pipeline-1__clf__C': 0.001
#'decisiontreeclassifier__max_depth'
mv_clf.get_params()

from sklearn.model_selection import GridSearchCV
params = {'decisiontreeclassifier__max_depth':[1, 2],
          'pipeline-1__clf__C': [0.001, 0.1, 100.0]}
grid = GridSearchCV(estimator=mv_clf,
                    param_grid=params,
                    cv=15,
                    scoring='roc_auc',
                    iid=False)
grid.fit(X_train_std, y_train)

print('최적의 매개변수: %s' % grid.best_params_)
print('최적의 정확도: %s' % grid.best_score_)

###############################################################################
###### Bagging and Bootstrap                                             ######
###############################################################################
import pandas as pd
from sklearn.ensemble import BaggingClassifier

df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/'
                      'machine-learning-databases/wine/wine.data',
                      header=None)

df_wine.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash',
                   'Alcalinity of ash', 'Magnesium', 'Total phenols',
                   'Flavanoids', 'Nonflavanoid phenols', 'Proanthocyanins',
                   'Color intensity', 'Hue',
                   'OD280/OD315 of diluted wines', 'Proline']

# 클래스 1 제외하기
df_wine = df_wine[df_wine['Class label'] != 1]

X, y = df_wine[['Alcohol', 'OD280/OD315 of diluted wines']].values, df_wine['Class label'].values

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
y = le.fit_transform(y)

X_train, X_test, y_train, y_test =\
            train_test_split(X, y,
                             test_size=0.2,
                             random_state=1,
                             stratify=y)

tree = DecisionTreeClassifier(criterion='entropy',
                              max_depth=None,
                              random_state=1)

bag = BaggingClassifier(base_estimator=tree,
                        n_estimators=500,
                        max_samples=1.0,
                        max_features=1.0,
                        bootstrap=True,
                        bootstrap_features=False,
                        n_jobs=1,
                        random_state=1)

from sklearn.metrics import accuracy_score

tree = tree.fit(X_train, y_train)
y_train_pred = tree.predict(X_train)
y_test_pred = tree.predict(X_test)

tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)
print('결정 트리의 훈련 정확도/테스트 정확도 %.3f/%.3f'
      % (tree_train, tree_test))

bag = bag.fit(X_train, y_train)
y_train_pred = bag.predict(X_train)
y_test_pred = bag.predict(X_test)

bag_train = accuracy_score(y_train, y_train_pred)
bag_test = accuracy_score(y_test, y_test_pred)
print('배깅의 훈련 정확도/테스트 정확도 %.3f/%.3f'
      % (bag_train, bag_test))

import numpy as np
import matplotlib.pyplot as plt

x_min = X_train[:, 0].min() - 1
x_max = X_train[:, 0].max() + 1
y_min = X_train[:, 1].min() - 1
y_max = X_train[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
                     np.arange(y_min, y_max, 0.1))

f, axarr = plt.subplots(nrows=1, ncols=2,
                        sharex='col',
                        sharey='row',
                        figsize=(8, 3))

for idx, clf, tt in zip([0, 1],
                        [tree, bag],
                        ['Decision tree', 'Bagging']):
    clf.fit(X_train, y_train)

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    axarr[idx].contourf(xx, yy, Z, alpha=0.3)
    axarr[idx].scatter(X_train[y_train == 0, 0],
                       X_train[y_train == 0, 1],
                       c='blue', marker='^')

    axarr[idx].scatter(X_train[y_train == 1, 0],
                       X_train[y_train == 1, 1],
                       c='green', marker='o')

    axarr[idx].set_title(tt)

axarr[0].set_ylabel('Alcohol', fontsize=12)
plt.text(10.2, -0.5,
         s='OD280/OD315 of diluted wines',
         ha='center', va='center', fontsize=12)

plt.tight_layout()
plt.show()

from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier

tree = DecisionTreeClassifier(criterion='entropy',
                              max_depth=1,
                              random_state=1)

ada = AdaBoostClassifier(base_estimator=tree,
                         n_estimators=500,
                         learning_rate=0.1,
                         random_state=1)

gbrt = GradientBoostingClassifier(n_estimators=20, random_state=42)

tree = tree.fit(X_train, y_train)
y_train_pred = tree.predict(X_train)
y_test_pred = tree.predict(X_test)

tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)
print('결정 트리의 훈련 정확도/테스트 정확도 %.3f/%.3f'
      % (tree_train, tree_test))

ada = ada.fit(X_train, y_train)
y_train_pred = ada.predict(X_train)
y_test_pred = ada.predict(X_test)

ada_train = accuracy_score(y_train, y_train_pred)
ada_test = accuracy_score(y_test, y_test_pred)

print('에이다부스트의 훈련 정확도/테스트 정확도 %.3f/%.3f'
      % (ada_train, ada_test))

gbrt.fit(X_train, y_train)
gbrt_train_score = gbrt.score(X_train, y_train)
gbrt_test_score = gbrt.score(X_test, y_test)

print('앙상블에 사용한 트리 개수 %d' % gbrt.n_estimators_)
print('그래디언트 부스팅 train/test 정확도 %.3f/%.3f'
      % (gbrt_train_score, gbrt_test_score))

f, axarr = plt.subplots(nrows=1, ncols=3,
                        sharex='col',
                        sharey='row',
                        figsize=(12, 4))

for idx, clf, tt in zip([0, 1, 2],
                        [tree, ada, gbrt],
                        ['Decision tree', 'AdaBoost', 'GradientBoost']):
    clf.fit(X_train, y_train)

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    axarr[idx].contourf(xx, yy, Z, alpha=0.3)
    axarr[idx].scatter(X_train[y_train == 0, 0],
                       X_train[y_train == 0, 1],
                       c='blue', marker='^')
    axarr[idx].scatter(X_train[y_train == 1, 0],
                       X_train[y_train == 1, 1],
                       c='green', marker='o')
    axarr[idx].set_title(tt)

axarr[0].set_ylabel('Alcohol', fontsize=12)
plt.text(10.2, -0.5,
         s='OD280/OD315 of diluted wines',
         ha='center', va='center', fontsize=12)

plt.tight_layout()
plt.show()


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

import argparse

import paddle
from ppgan.apps import Pixel2Style2PixelPredictor

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_image", type=str, help="path to source image")

    parser.add_argument("--output_path",
                        type=str,
                        default='output_dir',
                        help="path to output image dir")

    parser.add_argument("--weight_path",
                        type=str,
                        default=None,
                        help="path to model checkpoint path")

    parser.add_argument("--model_type",
                        type=str,
                        default=None,
                        help="type of model for loading pretrained model")

    parser.add_argument("--seed",
                        type=int,
                        default=None,
                        help="sample random seed for model's image generation")

    parser.add_argument("--size",
                        type=int,
                        default=1024,
                        help="resolution of output image")

    parser.add_argument("--style_dim",
                        type=int,
                        default=512,
                        help="number of style dimension")

    parser.add_argument("--n_mlp",
                        type=int,
                        default=8,
                        help="number of mlp layer depth")

    parser.add_argument("--channel_multiplier",
                        type=int,
                        default=2,
                        help="number of channel multiplier")

    parser.add_argument("--cpu",
                        dest="cpu",
                        action="store_true",
                        help="cpu mode.")

    args = parser.parse_args()

    if args.cpu:
        paddle.set_device('cpu')

    predictor = Pixel2Style2PixelPredictor(
        output_path=args.output_path,
        weight_path=args.weight_path,
        model_type=args.model_type,
        seed=args.seed,
        size=args.size,
        style_dim=args.style_dim,
        n_mlp=args.n_mlp,
        channel_multiplier=args.channel_multiplier)
    predictor.run(args.input_image)


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

from __future__ import print_function

import math
import os

from IPython import display
from matplotlib import cm  #color map
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset

tf.logging.set_verbosity(tf.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format

# current working directory
cwd = os.getcwd()

training_file_path = cwd + "//california_housing_train.csv"
Testing_file_path = cwd +  "//california_housing_test.csv"

california_housing_dataframe = pd.read_csv(training_file_path, sep = ",")
california_housing_dataframe = california_housing_dataframe.reindex(
	np.random.permutation(california_housing_dataframe.index))

def preprocess_features(california_housing_dataframe):
	ColNums = california_housing_dataframe.shape[1]
	selected_features = california_housing_dataframe.iloc[:, 0:ColNums-1]  #select the first ColNums-1 features
	processed_features = selected_features.copy()

	# Create a synthetic feature
	processed_features["rooms_per_person"] = (
		california_housing_dataframe["total_rooms"]/
		california_housing_dataframe["population"])

	return processed_features

def preprocess_targets(california_housing_dataframe):
	#output_targets = california_housing_dataframe.copy()
	output_targets = pd.DataFrame()
	output_targets["median_house_value"] = (california_housing_dataframe["median_house_value"]/1000.0)
	return output_targets

training_examples = preprocess_features(california_housing_dataframe.head(12000))
#print(training_examples.describe())

training_targets = preprocess_targets(california_housing_dataframe.head(12000))
#print(training_targets.describe())

validation_examples = preprocess_features(california_housing_dataframe.tail(5000))
#print(validation_examples.describe())

validation_targets = preprocess_targets(california_housing_dataframe.tail(5000))
#print(validation_targets.describe())

# plot the input feature and targets
plt.figure(figsize = (13, 8)) # a figure instance with size (13, 8) in inch
ax = plt.subplot(1,2,1)
ax.set_title("Validation Data")

ax.set_autoscaley_on(False)
ax.set_ylim([32, 43])
ax.set_autoscalex_on(False)
ax.set_xlim([-126, -112])

plt.scatter(validation_examples["longitude"],
			validation_examples["latitude"],
			cmap = "coolwarm",
			c = validation_targets["median_house_value"]/ validation_targets["median_house_value"].max())

ax = plt.subplot(1,2,2)
ax.set_title("Training Data")

ax.set_autoscaley_on(False)
ax.set_ylim([32, 43])
ax.set_autoscalex_on(False)
ax.set_xlim([-126, -112])
plt.scatter(training_examples["longitude"],
			training_examples["latitude"],
			cmap = "coolwarm",
			c = training_targets["median_house_value"] / training_targets["median_house_value"].max())

_ = plt.plot()
plt.show()

def my_input_fn(features, targets, batch_size = 1, shuffle = True, num_epochs = None):
	# convert pandas data into a dict of np arrays
	features = {key: np.array(value) for key, value in dict(features).items()}

	# Construct a dataset, and configure batching/repeating
	ds = Dataset.from_tensor_slices((features, targets))
	ds = ds.batch(batch_size).repeat(num_epochs)

	if shuffle:
		ds = ds.shuffle(100000)

	# return the next batch of data
	features, labels = ds.make_one_shot_iterator().get_next()
	return features, labels

def construct_feature_columns(input_features):
	return set([tf.feature_column.numeric_column(my_feature) for my_feature in input_features])

def train_model(
	learning_rate,
	steps,
	batch_size,
	training_examples,
	training_targets,
	validation_examples,
	validation_targets):

	periods = 10
	steps_per_period = steps/periods

	muy_optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate)
	muy_optimizer = tf.contrib.estimator.clip_gradients_by_norm(muy_optimizer, 5.0)

	linear_regressor = tf.estimator.LinearRegressor(
		feature_columns = construct_feature_columns(training_examples),
		optimizer = muy_optimizer)

	# create input function
	trainging_input_fn = lambda: my_input_fn(training_examples, training_targets["median_house_value"], batch_size = batch_size)

	predict_training_input_fn = lambda: my_input_fn(training_examples, training_targets["median_house_value"], num_epochs = 1, shuffle = False)

	predict_validation_input_fn = lambda: my_input_fn(validation_examples, validation_targets["median_house_value"], num_epochs = 1, shuffle = False)

	print("Training model...")
	print("RMSE (on training data): ")

	training_rmse = []
	validation_rmse = []

	for period in range(0, periods):
		linear_regressor.train(input_fn = trainging_input_fn, steps = steps_per_period)

		# Take a break and compute predictions
		training_predictions = linear_regressor.predict(input_fn = predict_training_input_fn)
		training_predictions = np.array([item['predictions'][0] for item in training_predictions])

		validation_predictions = linear_regressor.predict(input_fn = predict_validation_input_fn)
		validation_predictions = np.array([item['predictions'][0] for item in validation_predictions])

		#print("Trainig Prediction : ")
		#print(training_predictions)

		#print("Training Targets : ")
		#print(training_targets)

		# compute training and validation loss
		training_root_mean_squared_error = math.sqrt(
				metrics.mean_squared_error(training_predictions, training_targets))

		validation_root_mean_squared_error = math.sqrt(
				metrics.mean_squared_error(validation_predictions,validation_targets))

		print("  period %02d  :  %02f"  % (period, training_root_mean_squared_error))

		training_rmse.append(training_root_mean_squared_error)
		validation_rmse.append(validation_root_mean_squared_error)

	print("Model training finished.")

	plt.ylabel("RMSE")
	plt.xlabel("Periods")
	plt.title("Root Mean Squared Error vs . Periods")
	plt.tight_layout()
	plt.plot(training_rmse, label = "training")
	plt.plot(validation_rmse, label = "validation")
	plt.legend()
	plt.show()
	return linear_regressor

linear_regressor = train_model(
	learning_rate = 0.00003,
	steps = 500,
	batch_size = 5,
	training_examples = training_examples,
	training_targets = training_targets,
	validation_examples = validation_examples,
	validation_targets = validation_targets)

califonia_housing_test_data = pd.read_csv(Testing_file_path)

test_example = preprocess_features(califonia_housing_test_data)
test_targets = preprocess_targets(califonia_housing_test_data)

predict_test_input_fn = lambda: my_input_fn(test_example, test_features("median_house_value"),
	num_epochs = 1, shuffle = False)

test_predictions = linear_regressor.predict(input_fn = predict_test_input_fn)
test_predictions = np.array([item("predictions")[0] for item in test_predictions])

root_mean_squared_error = math.sqrt(
	metrics.mean_squared_error(test_predictions, test_targets))

print("Final RMSE (on the data): %0.2f"  % root_mean_squared_error)


import itertools

class Solution:
    def maxCompatibilitySum(self, students, mentors) -> int:
        m = len(students)
        n = len(students[0])
        res = 0
        for mentor in itertools.permutations(range(m)):
            a = 0
            for i, j in enumerate(mentor):
                a += sum([1 - (students[i][k] ^ mentors[j][k])for k in range(n)])
            res = max(res, a)
        return res

s = Solution()
print(s.maxCompatibilitySum([[1, 1, 0], [1, 0, 1], [0, 0, 1]],
                            [[1, 0, 0], [0, 0, 1], [1, 1, 0]]))


import discord
import random
from discord.ext import commands

class Info(commands.Cog):
	def __init__(self, client):
		self.client = client

	@commands.command(name='ping', aliases=['latency'], help='Pong!')
	async def ping(self, ctx):
		embed=discord.Embed(title='Pong!',
			description=f'Ping = {round(self.client.latency * 1000)}ms',
			colour=random.randint(0,0xffffff)
		)
		embed.set_footer(text=f'Requested by {ctx.author}')
		await ctx.send(embed=embed)

def setup(client):
	client.add_cog(Info(client))


#-*- coding:utf-8 -*-
__author__ = 'liubiao'

from nose.plugins.attrib import attr
from common.getconfig import GetConfig
from common.verify import Verify
from common.requestslib import RequestsLib
from common.getdate import GetDate

@attr('nativenightroomlist','hhub3')
class TestNativeNightRoomList():
    @classmethod
    def setup_class(self):
        conf = GetConfig()
        #ged = GetExpectDate(r".\testproject\hhub3\AdminOpenTravelTransaction\QueryToday")
        self.verity = Verify()
        self.url = conf.get_conf_value("hhub3", "url") + r"api/Room/NativeNightRoomList"
        self.date = conf.get_conf_dict("data")
        #self.expect_string = ged.get_json_expect_date()
        self.requestslib = RequestsLib()
        self.date = GetDate()

    @classmethod
    def teardown_class(self):
        pass

    def test_native_night_roomlist(self):
        u'获取华住夜宵房房型列表'
        params = dict()
        params['HotelID'] = '2000505'
        params['CheckInDate'] = self.date.get_date(1)
        params['CheckOutDate'] = self.date.get_date(2)
        params['queryRoomType'] = '0'
        response = self.requestslib.send_request_by_accesstoken('get',self.url,request_body=params)
        self.verity.by_status(response,200)


import requests
from scrapy import Selector
from globals import file_path
from management.ImgRefractor import prod_img, desc_img

def blaupunkt(model, html):
    sel = Selector(text=requests.get(html).content)

    # Zdjęcia produktu
    prod_imgs_links = sel.xpath('//div[@id="product_gallery_new"]//img/@src').extract()
    for prod_img_link in prod_imgs_links:
        prod_img(f'{file_path}/{model}', 'https://www.blaupunkt.com/' + prod_img_link,
                 prod_imgs_links.index(prod_img_link), crop=True)

    # Opis graficzny
    graph_desc = ['
']
    for i, ele in enumerate(sel.xpath('//div[contains(@class, "newkeyvisual")]')):
        complete_element = []

        text = ele.xpath('.//text()').extract()
        if text[0].isupper():
            if i % 2 == 0:
                complete_element.append(f'

{text[0]}
')
            else:
                complete_element.append(f'

{text[0]}
')
            try:
                complete_element.append('
' + ''.join(text[1:]) + '

')
            except IndexError:
                complete_element.append('

')
        else:
            complete_element.append('

' + '\n\n'.join(text) + '

')

        img = 'https://www.blaupunkt.com/' + ele.xpath('.//img/@src').extract()[0]
        desc_img(f'{file_path}/{model}', img, i, crop=False, force_small=True)

        complete_element.append(f'
')

        graph_desc.append(''.join(complete_element))
    graph_desc.append('
')

    # Opis techniczny
    short_desc = ['
']
    tech_desc_data = sel.xpath('//div[@id="tab-details"]/ul/li/text()').extract()
    tech_desc = ['

Specyfikacja
']
    tech_desc_end = []
    for ele in tech_desc_data:
        if len(ele.split(':')) == 2:
            name, value = ele.split(':')
            tech_desc.append(f'
{name}	{value}
')
            short_desc.append(f'
{name}: {value}
')
        else:
            if not tech_desc_end:
                tech_desc_end.append(f'
Dodatkowo	{ele}
')
            else:
                tech_desc_end.append(f'
	{ele}
')

    tech_desc.extend(tech_desc_end)
    tech_desc.append('

')
    tech_desc = ''.join(tech_desc)

    short_desc.append('
')
    short_desc = ''.join(short_desc)

    graph_desc = ''.join(graph_desc)
    print(graph_desc)
    return [graph_desc, short_desc, tech_desc]

if __name__ == '__main__':
    blaupunkt('VCC301', 'https://www.blaupunkt.com/pl/nc/produkty/dom/odkurzacze/products/single/18609')


from matplotlib.figure import Figure
from server.cords.get_coords import get_coords
from server.figure.modify_xy import modify_coords
from server.figure.draw_plot import draw

def create_figure(body: dict) -> Figure:
    try:
        x, y = get_coords(body)

        fig = Figure()
        original = body.get('original', None)
        if original:
            draw((2, 1, 1), fig, x, y, 'Original')
            pos = (2, 1, 2)
        else:
            pos = (1, 1, 1)
        x, y = modify_coords(body, x, y)
        draw(pos, fig, x, y, 'With transformation', 'r')
        fig.tight_layout()

        return fig
    except Exception as ex:
        raise ex


import sys
import pkg_resources
import pip
import xmlrpclib
from pip.log import logger
from pip.basecommand import Command
from pip.commands.search import transform_hits, highest_version
from pip.util import get_installed_distributions

class OutdatedCommand(Command):
    name = 'outdated'
    usage = '%prog [OPTIONS]'
    summary = 'Output all currently installed outdated packages to stdout'

    def __init__(self):
        super(OutdatedCommand, self).__init__()
        self.parser.add_option(
            '-l', '--local',
            dest='local',
            action='store_true',
            default=False,
            help='If in a virtualenv, do not report'
                ' globally-installed packages')

        self.parser.add_option(
            '--index',
            dest='index',
            metavar='URL',
            default='http://pypi.python.org/pypi',
            help='Base URL of Python Package Index (default %default)')

    def setup_logging(self):
        logger.move_stdout_to_stderr()

    def search(self, query, index_url):
        pypi = xmlrpclib.ServerProxy(
            index_url,
            pip.download.xmlrpclib_transport,
        )
        hits = pypi.search({'name': query}, 'or')
        return hits

    def run(self, options, args):
        local_only = options.local
        index_url = options.index

        installations = {}
        dependency_links = []
        find_tags = False

        f = sys.stdout

        for dist in pkg_resources.working_set:
            if dist.has_metadata('dependency_links.txt'):
                dependency_links.extend(
                    dist.get_metadata_lines('dependency_links.txt'),
                )

        for dist in get_installed_distributions(local_only=local_only):
            req = pip.FrozenRequirement.from_dist(
                dist, dependency_links, find_tags=find_tags,
            )
            installations[req.name] = req

        pypi_hits = self.search(
            [i.name for i in installations.values()],
            index_url,
        )
        hits = transform_hits(pypi_hits)

        for hit in hits:
            name = hit['name']
            try:
                if name in installations:
                    req = installations[name].req
                    latest = highest_version(hit['versions'])
                    if req.specs[0][1] != latest:
                        f.write('%s (LATEST: %s)\n' % (str(req), latest))

            except UnicodeEncodeError:
                pass

OutdatedCommand()


#coding:utf-8
from flask import jsonify, request, Response
from . import api
from app import db
from app.models import User, Story, Storyc
from flask_login import login_user, logout_user, current_user, login_required
from sqlalchemy.sql import func
import json

@api.route('/search/', methods = ['POST'])
def search():
    if request.method == 'POST':
        keyword = request.get_json().get('keyword')
        storys = Story.query.all()
        findlist = []
        b = 0 #判断有无
        for s in storys:
            keywords = s.keywords.split(" ")
            for k in keywords:
                if keyword == k:
                    b = 1
                    title = s.title
                    username = User.query.filter_by(id=s.user_id).first().username
                    storyid = s.id
                    sc = Storyc.query.filter_by(storyckeyword=k).first()
                    storyc = sc.storyc
                    likenum = sc.likenum
                    findlist.append({'username':username,
                                    'title':title,
                                    'storyid':storyid,
                                    'storyc':storyc,
                                    'likenum':likenum
                                    })
        return jsonify ({"findlist":findlist}),200