from ..network_device import NetworkDevice
from ciscoconfparse import CiscoConfParse
from .. import utils
import re
from ..netcapt_exceptions import TextFsmParseIssue

class TextFsmParseIssue(Exception):
    pass

class CiscoNetworkDevice(NetworkDevice):

    # mapper for trunk dict method
    _trunk_dict = {
        'vlans_native': 'cisco_ios_get_intf_native_vlan.textfsm',
        'vlans_allowed': 'cisco_ios_get_intf_allowed_vlan.textfsm',
        'vlans_forwarding': 'cisco_ios_get_intf_trunk_vlan.textfsm',
        'vlans_not_pruned': 'cisco_ios_get_intf_not_pruned_vlan.textfsm',
    }

    # Gather Commands
    # TODO: add a gather all, that will gather all the Gather commands
    def gather_all(self):
        pass

    # Ready
    def gather_arp(self):
        """
        Developed on each individual class
        """
        pass

    # Ready
    def gather_bgp(self):
        # TODO: Check if NXOS and IOS needs to be VRF aware
        return self.show_ip_bgp()

    # Ready
    def gather_cdp(self):
        output = self.show_cdp_neigh_detail(use_textfsm=True)

        output2 = self.show_cdp_neigh(use_textfsm=True)
        if len(output) != len(output2):
            raise TextFsmParseIssue(str(self) + " ERROR:\n"
                                    "\tThe detailed CDP count does not equal the regular CDP count, please check the TextFSM file")
        return output

    # Ready
    def gather_interfaces(self):
        """
        Capture the interface Data
        :return:
        """
        # Applies to all of them, will not go through interface_status section because no output
        intf_list = self.show_interface()
        intf_status_list = self.show_interface_status()
        vrf_info = self.get_vrf_info()

        # Gather additional trunk data
        trunk_detail = self.get_trunk_dict()
        switchport_data_found = False

        if isinstance(intf_list, list):
            for intf in intf_list:
                # Default values
                intf['vrf'] = 'global'
                intf['l2_l3'] = 'Layer 2'
                intf['trunk_access'] = 'Access'
                intf['native'] = str()
                intf['allowed'] = str()
                intf['not_pruned'] = str()
                intf['vlan'] = str()
                # Update Layer 3 Interface Data
                if intf['ip_address']:
                    intf['l2_l3'] = 'Layer 3'
                    intf['trunk_access'] = 'Routed'
                # Checking every interface not jus Layer 3
                if isinstance(vrf_info, list):
                    for vrf in vrf_info:
                        for intf_vrf in vrf['interface']:
                            if intf_vrf.lower() in utils.intf_abbvs(intf['interface']):
                                intf = vrf['name']
                # only proceed if Parsed by Textfsm, if a list was provided.
                # parses through
                if isinstance(intf_status_list, list):
                    intf_status = utils.find_intf_data(intf['interface'], intf_status_list, 'port')
                    # Only if a value was found
                    if intf_status:
                        if intf_status['vlan'].isnumeric():
                            intf['vlan'] = intf_status['vlan']
                        elif intf_status['vlan'] == 'trunk':
                            intf['trunk_access'] = 'Trunk'
                            intf['native'] = utils.find_intf_data(
                                intf_status['port'], trunk_detail['vlans_native'], 'interface', 'vlans'
                            )
                            intf['allowed'] = utils.find_intf_data(
                                intf_status['port'], trunk_detail['vlans_allowed'], 'interface', 'vlans'
                            )
                            intf['not_pruned'] = utils.find_intf_data(
                                intf_status['port'], trunk_detail['vlans_not_pruned'], 'interface', 'vlans'
                            )
            return intf_list

        elif isinstance(intf_list, str):
            raise TextFsmParseIssue("TextFSM failed to Parse 'show interface'. "
                                    "Please review and update the Template and output")

    # Ready
    def gather_inventory(self):
        return self.show_inventory()

    # Ready
    def gather_lldp(self):
        output = self.show_lldp_neigh_detailed(use_textfsm=True)
        output2 = self.show_lldp_neigh(use_textfsm=True)
        if len(output) != len(output2):
            raise TextFsmParseIssue(str(self) + " ERROR:\n"
                  "\tThe detailed LLDP count does not equal the regular LLDP count, please check the TextFSM file")
        return output

    def gather_mac(self):
        # TODO: Need to continue work on this for NXOS and XR
        return self.show_mac_address_table()

    # TODO: Need to Fix this this
    def gather_route(self):
        vrf_list = self.get_vrf_names()
        route_list = list()
        route_table_present = False
        for vrf in vrf_list:
            # Defaulting this as the value.
            # TODO: Add a show route option if this comes back empty
            command = "show ip route"
            # Add VRF for non Global
            if vrf != "global":
                command += " vrf " + vrf
            output = self.send_command(command, use_textfsm=True)
            if isinstance(output, list):
                for route in output:
                    route["cidr"] = route['network'] + "/" + route['mask']
                    if "vrf" not in list(route.keys()):
                        route["vrf"] = vrf
                    route_list.append(route)
                    route_table_present = True
            if not route_table_present:
                route = {}
                default_gateway = re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", output)
                route["vrf"] = vrf
                route["protocol"] = "Layer 2 only"
                if default_gateway:
                    route["nexthop_ip"] = default_gateway[0]
                else:
                    output2 = self.send_command("show run | incl default-gateway")
                    default_gateway = re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", output2)
                    if default_gateway:
                        route["nexthop_ip"] = default_gateway[0]
                route_list.append(route)
        return route_list

    # Ready
    def gather_version(self):
        output = self.show_version(use_textfsm=True)
        # Adding CPU Processes tp gather_version
        cpu_data = self.send_command('show processes cpu', use_textfsm=True)
        if isinstance(cpu_data, list):
            output[0].update(cpu_data[0])
        return output

    def gather_ip_mroute(self):
        output = self.show_ip_mroute()
        # Need to determine if the table was empty
        re_match = re.match(r'^IP Multicast Routing Table for VRF\s+\S+\n+$', output)
        if 'IP Multicast Forwarding is not enabled.' in output:
            output = [{"no_data": "IP Multicast Forwarding is not enabled."}]
        elif re_match:
            # if re_match then the rable was empty and not a TextFSM Issue.
            output = [{"no_data": "No show ip mroute data"}]
        elif isinstance(output, str):
            raise TextFsmParseIssue("Please Check TextFSM Template, Data is not being parsed.")
        return output

    def gather_config(self):
        return self.send_command('show running-config', delay_factor=5)

    # Gather specific Data and parse it as needed
    def get_vrf_names(self):
        """
        Gathers the VRF names from the device
        """
        vrf_names = ['global']
        output = self.show_vrf()
        if isinstance(output, list):
            for vrf in output:
                if vrf['name'] not in vrf:
                    vrf_names.append(vrf['name'])
        return vrf_names

    # Update Data from CLI interaction
    def update_hostname(self):
        """Gets Hostname from the Connection and saves it to the device."""
        self.hostname = self.connection.find_prompt()[:-1]

    # capture specific data that is not just a show command
    def configuration(self, cfg_location="run", use_parser=False):
        # check if startup or running config requested
        if cfg_location == "start":
            cfg = self.show_startup_configuration()
        else:
            cfg = self.show_running_configuration()
        # if use_parser is required then return it will parse with CiscoConfParser
        if use_parser:
            cfg = self.show_running_configuration()
            return CiscoConfParse(cfg.splitlines())
        return cfg

    def interface_configuration(self, cfg_obj=None, cfg_location="run"):
        """
        Returns the Interface objects from the CiscoConfParse object
        :param cfg_obj:
        :param cfg_location: 'run' to obtain running-configuration or 'start' for startup-configuration
        :return: List of Interface objects
        """
        if cfg_obj is None:
            cfg_obj = self.configuration(cfg_location=cfg_location, use_parser=True)
        return cfg_obj.find_objects('^interface')

    # Show commands with use_textfsm
    def show_inventory(self, use_textfsm=True):
        return self.send_command("show inventory", use_textfsm=use_textfsm)

    def show_ip_bgp(self, use_textfsm=True):
        return self.send_command("show ip bgp", use_textfsm=use_textfsm)

    def show_mac_address_table(self, use_textfsm=True):
        return self.send_command("show mac address-table", use_textfsm=use_textfsm)

    def show_interface(self, use_textfsm=True):
        return self.send_command("show interface", use_textfsm=use_textfsm)

    def show_interface_status(self, use_textfsm=True):
        return self.send_command("show interface status", use_textfsm=use_textfsm)

    def show_vrf(self, use_textfsm=True):
        """
        Captures the show vrf output

        :param use_textfsm: Boolean to determine if TextFSM should be used to parse the output default: True

        :return: List of Detailed CDP Neighbors

        """
        return self.send_command("show vrf", use_textfsm=use_textfsm)

    def show_cdp_neigh_detail(self, use_textfsm=True):
        """
        Captures the Detailed CDP output

        :param use_textfsm: Boolean to determine if TextFSM should be used to parse the output default: True

        :return: List of Detailed CDP Neighbors

        """
        return self.send_command("show cdp neighbor detail", use_textfsm=use_textfsm)

    def show_cdp_neigh(self, use_textfsm=True):
        """
        Captures the summary CDP output

        :param use_textfsm: Boolean to determine if TextFSM should be used to parse the output default: True

        :return: List of Detailed CDP Neighbors

        """
        return self.send_command("show cdp neighbor", use_textfsm=use_textfsm)

    def show_lldp_neigh_detailed(self, use_textfsm=True):
        """
        Captures the Detailed LLDP output

        :param use_textfsm: Boolean to determine if TextFSM should be used to parse the output default: True

        :return: List of Detailed LLDP Neighbors

        """
        return self.send_command("show lldp neighbor detail", use_textfsm=use_textfsm)

    def show_lldp_neigh(self, use_textfsm=True):
        """
        Captures the summary LLDP output

        :param use_textfsm: Boolean to determine if TextFSM should be used to parse the output default: True

        :return: List of Detailed LLDP Neighbors

        """
        return self.send_command("show lldp neighbor", use_textfsm=use_textfsm)

    def show_version(self, use_textfsm=True):
        return self.send_command("show version", use_textfsm=use_textfsm)

    def show_arp_vrf(self, vrf, use_textfsm=True):
        return self.send_command("show ip arp vrf " + vrf, use_textfsm=use_textfsm)

    def show_arp(self, use_textfsm=True):
        return self.send_command("show ip arp", use_textfsm=use_textfsm)

    def show_ip_mroute(self, use_textfsm=True):
        return self.send_command("show ip mroute", use_textfsm=use_textfsm)

    # show configuration commands with Cisco Config Parser option
    def show_startup_configuration(self, cisco_cfg_parse=False, factory=False):
        self.disable_paging()
        output = self.send_command("show startup-config")
        if cisco_cfg_parse:
            return CiscoConfParse(output.splitlines(), factory=factory)
        return output

    def show_running_configuration(self, cisco_cfg_parse=False, factory=False):
        self.disable_paging()
        output = self.send_command("show running-config")
        if cisco_cfg_parse:
            return CiscoConfParse(output.splitlines(), factory=factory)
        return output

    # show tech option that disables paging and increases the global_delay factor
    def show_tech(self, global_delay_factor=5):
        self.disable_paging()
        return self.send_command('show tech-support', global_delay_factor=global_delay_factor)

    # Enable/Disable Features
    def disable_paging(self):
        self.send_command('term len 0')

    # This is an intentional break

    def get_trunk_dict(self):
        """
        Parses the 'show int trunk' response, only works on cisco_ios
        """
        return_dict = self._trunk_dict.copy()
        def __parse_dictionary_data(list_of_dict):
            if isinstance(list_of_dict, str):
                return None

            return_dict = list()
            for line in list_of_dict:
                t_dict = dict()
                for key, value in line.items():
                    t_dict[key] = str().join(value)
                return_dict.append(t_dict)
            return return_dict

        def __get_vlan_list(txt_tmpl):
            vlans_list = self.send_command("show int trunk", use_textfsm=True, textfsm_template=txt_tmpl)
            vlans_list = __parse_dictionary_data(vlans_list)
            return vlans_list

        for key, val in return_dict.items():
            return_dict[key] = self._textfsm_templates_path.child(val)

        for vlan_list, template_path in return_dict.items():
            return_dict[vlan_list] = __get_vlan_list(template_path)

        return return_dict

    def get_sfp(self):
        """
        Obtain a list of SFP Inventory
        :return: list of SFP Inventory
        """
        inventory = self.gather_inventory()
        sfp_list = list()
        for item in inventory:
            if 'sfp' in item["description"].lower():
                sfp_list.append(item)
        return sfp_list

    def count_intf(self):
        intf_counts = dict()
        interface_list = self.show_interface()
        for intf in interface_list:
            # Strip the Interface Number
            intf_stripped = re.match(r'^([a-zA-Z]+)', intf['interface']).group(1)

            # Add Interface Type if it does not already exist
            if intf_stripped not in intf_counts.keys():
                intf_counts[intf_stripped] = {'count': 0, 'active': 0}

            # Add the Counts
            intf_counts[intf_stripped]['count'] += 1
            if intf['link_status'] == 'up':
                intf_counts[intf_stripped]['active'] += 1
        return intf_counts

    def get_vrf_info(self):
        return self.show_vrf()


import random
def palavra_correta(palavraum,palavradois):
    if palavraum=='s':
        palavra=input('Digite aqui a palavra').lower()
    if palavra==sorteio:
        print('Parabens! a palavra correta era: {}'.format(sorteio))
    return

letra=input('Digite aqui uma letra:')
elementos=['paz','amor','fraternidade']
sorteio=random.choice(elementos)
palavra=''
palavra2=''
if letra in sorteio:
   print('Existe essa letra na palavra')
   print('A palavra tem {} letra'.format(len(sorteio)))
   print('Essa palavra aparece {} na palavra'.format(sorteio.count(letra)))
   palavra=input('Já sabe qual é a palavra S/N')
   palavra_correta(palavra,palavra2)
else:
    while letra not in sorteio:
        print('Letra não existe')
        letra=input('Digite aqui outra letra')
        if letra in sorteio:
            palavra2=''
            print('Existe essa letra na palavra')
            print('A palavra tem {} letra'.format(len(sorteio)))
            print('Essa palavra aparece {} na palavra'.format(sorteio.count(letra)))
            palavra = input('Já sabe qual é a palavra S/N')
            palavra_correta(palavra,palavra2)


from django import forms
from . import models

class CommentForm(forms.ModelForm):
    class Meta:
        model = models.Comment
        fields = (
            "comment",
        )

    def __init__(self, *args, **kwargs):
        # 폼 안에 class를 만들어주는 역할
        super(CommentForm, self).__init__(*args, **kwargs)
        self.fields['comment'].widget.attrs['placeholder'] = '댓글을 남기세요'
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'comment_form'


import argparse
import csv

from XL.columns import *
from XL.column_data import *
from XL.parse_files import *

parser = argparse.ArgumentParser(description='process csv and excel files.')
parser.add_argument('-csv', '--csv1', help='CSV file containing data', required=True)
parser.add_argument('--csv2', help='CSV file containing data', required=False)
parser.add_argument('-xl', '--xl1', help='excel file containing data', required=False)
parser.add_argument('--xl2', help='excel file containing data', required=False)

args = vars(parser.parse_args())
print(args)

csv1 = parse_csv(args['csv1'])
if args['csv2'] is not None:
    csv2 = parse_csv(args['csv2'])
if args['xl1'] is not None:
    xl1 = parse_excel(args['xl1'])
if args['xl2'] is not None:
    xl2 = parse_excel(args['xl2'])

def columns():
    cols = get_columns(csv1)

def write_csv():
    data = list(zip(get_columns(csv1), get_first_row_data(csv1)))
    df = pd.DataFrame(data)
    header = ("Columns", "First row vals")
    df.columns = header
    df.to_csv('output.csv', index=False)

write_csv()

def write_csv_np(zip_obj):
    """
    function to write the csv using csv module instead of pandas
    :param zip_obj:
    :return:
    """
    csv_columns = ['A', 'B', 'C']
    with open('output.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(csv_columns)
        writer.writerows(zip_obj)


from pathlib import Path
import re

space =  '    '
branch = '│   '
tee =    '├── '
last =   '└── '

with open(Path(__file__).parent.parent / '.gitignore', 'r') as f:
	lines = f.read().splitlines()

new_lines = []
for line in lines:
    if len(line) > 0 and line[0] not in ['#']:
        new_line = str(line)

        # if new_line[0] == '\\':
        #     new_line = '/' + new_line[1:]
        # elif new_line[0] != '/':
        #     new_line = '/' + new_line

        new_line = new_line.replace('/','\/').replace('.','\.')

        if new_line[-1] == '*':
            new_lines.append(new_line.replace('*','.*'))
        else:
            new_lines.append(new_line.replace('*','.*?'))

ret = '\/(' + '|'.join(new_lines) + ')'
matcher = re.compile(ret)

class Tree():
    def __init__(self, dir_path: Path, only_dirs: bool=False, ignore_dirs: list=[], level: int=-1):
        self.only_dirs = only_dirs
        self.dir_path = dir_path
        self.ignore_dirs = set(ignore_dirs) | set(['/.git','media/','plots/'])
        self.level = level

        self.files = 0

    def generate(self):
        ret = [self.dir_path.name or '.']
        ret.extend(self.__make__())

        return ret

    def __make__(self, prefix: str=''):
        if not self.level:
            return

        if self.only_dirs:
            contents = []
            for d in self.dir_path.iterdir():
                if d.is_dir() and d.name not in self.ignore_dirs:
                    contents.append(d)
        else:
            contents = []
            for d in self.dir_path.iterdir():
                m = str(d)
                m = '/' + m[0:] if m[0] != '/' else m
                ignore = any([g in m for g in self.ignore_dirs])
                match = matcher.search(m)
                if match is None and not ignore:
                    contents.append(d)

        pointers = [tee] * (len(contents) - 1) + [last]

        for pointer, path in zip(pointers, contents):
            if path.is_dir():
                yield prefix + pointer + path.name

                if pointer == tee:
                    extension = branch
                else:
                    extension = space

                self.dir_path = path
                self.level = self.level-1
                yield from self.__make__(prefix=prefix+extension)
            elif not self.only_dirs:
                yield prefix + pointer + path.name
                self.files += 1

if __name__ == '__main__':
    tree = Tree(Path(__file__).parent.parent)
    pretty = tree.generate()
    print('\n'.join(pretty))


#!/usr/bin/env python2.6

# Standard Lib Modules
import os
import logging
import argparse
import fnmatch
import re

def parse_file(file_obj, regex):
    print("Parsing file.")
    parsed_file = {
        "type": 'unknown',
        "content": {}
    }
    block_num = 0
    in_block = False
    content = dict()

    for line in file_obj:
        line = line.strip()
        line = line.replace('\t', ' ')  # Yes, lines could be tab separated. :(

        if regex["comment"].match(line) or regex["blankline"].match(line):
            continue

        if not in_block:
            if regex["host"].match(line):
                print("Host match. {0}".format(file_obj.name))
                parsed_file["type"] = "host"
                in_block = True
            elif regex["hostgroup"].match(line):
                print("Hostgroup match. {0}".format(file_obj.name))
                parsed_file["type"] = "hostgroup"
                in_block = True
            elif regex["hostdependency"].match(line):
                print("hostdependency match. {0}".format(file_obj.name))
                parsed_file["type"] = "hostdependency"
                in_block = True
            elif regex["hostescalation"].match(line):
                print("hostescalation match. {0}".format(file_obj.name))
                parsed_file["type"] = "hostescalation"
                in_block = True
            elif regex["hostextinfo"].match(line):
                print("hostextinfo match. {0}".format(file_obj.name))
                parsed_file["type"] = "hostextinfo"
                in_block = True
            elif regex["service"].match(line):
                print("Service match. {0}".format(file_obj.name))
                parsed_file["type"] = "service"
                in_block = True
            elif regex["servicegroup"].match(line):
                print("Servicegroup match. {0}".format(file_obj.name))
                parsed_file["type"] = "servicegroup"
                in_block = True
            elif regex["servicedependency"].match(line):
                print("servicedependency match. {0}".format(file_obj.name))
                parsed_file["type"] = "servicedependency"
                in_block = True
            elif regex["serviceescalation"].match(line):
                print("serviceescalation match. {0}".format(file_obj.name))
                parsed_file["type"] = "serviceescalation"
                in_block = True
            elif regex["serviceextinfo"].match(line):
                print("serviceextinfo match. {0}".format(file_obj.name))
                parsed_file["type"] = "serviceextinfo"
                in_block = True
            elif regex["contact"].match(line):
                print("contact match. {0}".format(file_obj.name))
                parsed_file["type"] = "contact"
                in_block = True
            elif regex["contactgroup"].match(line):
                print("contactgroup match. {0}".format(file_obj.name))
                parsed_file["type"] = "contactgroup"
                in_block = True
            elif regex["timeperiod"].match(line):
                print("timeperiod match. {0}".format(file_obj.name))
                parsed_file["type"] = "timeperiod"
                in_block = True
            elif regex["command"].match(line):
                print("command match. {0}".format(file_obj.name))
                parsed_file["type"] = "command"
                in_block = True
        elif in_block:
            if regex["end"].match(line):  # This is for files with multiple blocks.
                print("End of block.")
                # Added content to parsed_file
                parsed_file["content"][block_num] = content
                block_num += 1
                in_block = False
                content = dict()
            else:
                line = line.split(' ', 1)
                print("Line: {0}".format(line))
                if line[1] == '':
                   line[1] = ''
                content[line[0]] = line[1].strip()
    return parsed_file

def merge_files(file1, file2):
    print("Merging files.")
    print("Parsed File 1: {0}".format(file1))
    print("Parsed File 2: {0}".format(file2))
    for block in file2["content"]:  # Making the assumption the type is the same.
        if block in file1["content"]:
            for key in file2["content"][block]:
                if key in file1["content"][block]:
                    if file1["content"][block][key] != file2["content"][block][key]:
                        file1["content"][block][key] = file2["content"][block][key]
                    else:
                        file1["content"][block][key] = file1["content"][block][key]
                else:
                    file1["content"][block][key] = file2["content"][block][key]
        else:
            file1["content"][block] = file2["content"][block]

    print("Merged Files: {0}".format(file1))

    return file1

def file_walk(path, file_filter, regex, host_file={}, hostgroup_file={}, hostdependency_file={}, hostescalation_file={}, hostextinfo_file={}, service_file={}, servicegroup_file={}, servicedependency_file={}, serviceescalation_file={}, serviceextinfo_file={}, contact_file={}, contactgroup_file={}, timeperiod_file={}, command_file={}):
    for root, _, filelist in os.walk(path):
        for base_file_name in fnmatch.filter(filelist, file_filter):
            base_file_name = "/".join(
                [
                    root,
                    base_file_name
                ]
            )
            base_file = open(base_file_name, 'r')
            override_file_name = ".".join([base_file_name, "override"])
            # Possible race condition with this. A try/catch block with open
            # would be better, but I don't want to deal with that right now.
            if os.path.isfile(override_file_name):
                override_file = open(override_file_name, 'r')
                base_file_parsed = parse_file(base_file, regex)
                override_file_parsed = parse_file(override_file, regex)
                final_file = merge_files(base_file_parsed, override_file_parsed)
                if final_file["type"] == "host":
                    print("Adding host file.")
                    host_file[base_file_name] = final_file
                elif final_file["type"] == "hostgroup":
                    print("Adding hostgroup file.")
                    hostgroup_file[base_file_name] = final_file
                elif final_file["type"] == "hostdependency":
                    print("Adding hostdependency file.")
                    hostdependency_file[base_file_name] = final_file
                elif final_file["type"] == "hostescalation":
                    print("Adding hostescalation file.")
                    hostescalation_file[base_file_name] = final_file
                elif final_file["type"] == "hostextinfo":
                    print("Adding hostextinfo file.")
                    hostextinfo_file[base_file_name] = final_file
                elif final_file["type"] == "service":
                    print("Adding service file.")
                    service_file[base_file_name] = final_file
                elif final_file["type"] == "servicegroup":
                    print("Adding servicegroup file.")
                    servicegroup_file[base_file_name] = final_file
                elif final_file["type"] == "servicedependency":
                    print("Adding servicedependency file.")
                    servicedependency_file[base_file_name] = final_file
                elif final_file["type"] == "serviceescalation":
                    print("Adding serviceescalation file.")
                    serviceescalation_file[base_file_name] = final_file
                elif final_file["type"] == "serviceextinfo":
                    print("Adding serviceextinfo file.")
                    serviceextinfo_file[base_file_name] = final_file
                elif final_file["type"] == "contact":
                    print("Adding contact file.")
                    contact_file[base_file_name] = final_file
                elif final_file["type"] == "contactgroup":
                    print("Adding contactgroup file.")
                    contactgroup_file[base_file_name] = final_file
                elif final_file["type"] == "timeperiod":
                    print("Adding timeperiod file.")
                    timeperiod_file[base_file_name] = final_file
                elif final_file["type"] == "command":
                    print("Adding command file.")
                    command_file[base_file_name] = final_file
                override_file.close()
            else:
                final_file = parse_file(base_file, regex)
                if final_file["type"] == "host":
                    host_file[base_file_name] = final_file
                elif final_file["type"] == "hostgroup":
                    hostgroup_file[base_file_name] = final_file
                elif final_file["type"] == "hostdependency":
                    hostdependency_file[base_file_name] = final_file
                elif final_file["type"] == "hostescalation":
                    hostescalation_file[base_file_name] = final_file
                elif final_file["type"] == "hostextinfo":
                    hostextinfo_file[base_file_name] = final_file
                elif final_file["type"] == "service":
                    service_file[base_file_name] = final_file
                elif final_file["type"] == "servicegroup":
                    servicegroup_file[base_file_name] = final_file
                elif final_file["type"] == "servicedependency":
                    servicedependency_file[base_file_name] = final_file
                elif final_file["type"] == "serviceescalation":
                    serviceescalation_file[base_file_name] = final_file
                elif final_file["type"] == "serviceextinfo":
                    serviceextinfo_file[base_file_name] = final_file
                elif final_file["type"] == "contact":
                    contact_file[base_file_name] = final_file
                elif final_file["type"] == "contactgroup":
                    contactgroup_file[base_file_name] = final_file
                elif final_file["type"] == "timeperiod":
                    timeperiod_file[base_file_name] = final_file
                elif final_file["type"] == "command":
                    command_file[base_file_name] = final_file

            base_file.close()

    return host_file, hostgroup_file, hostdependency_file, hostescalation_file, hostextinfo_file, service_file, servicegroup_file, servicedependency_file, serviceescalation_file, serviceextinfo_file, contact_file, contactgroup_file, timeperiod_file, command_file

def build_config(cfg_file, cfg_data):
    for filename in cfg_data:
        source_dir, source_file = filename.split("/")[-2:]
        cfg_file.write("# Start {0}/{1}\n".format(source_dir, source_file))
        for block in cfg_data[filename]["content"]:
            cfg_file.write("define {0} {{\n".format(cfg_data[filename]["type"]))
            for key in cfg_data[filename]["content"][block]:
                cfg_file.write("    {0}    {1}\n".format(key, cfg_data[filename]["content"][block][key]))
            cfg_file.write("}\n")
        cfg_file.write("# End {0}/{1}\n".format(source_dir, source_file))

def main():
    description = "Walks a directory and consolidates config files"
    log_entry_format = ":".join(
        [
            '%(asctime)s',
            '%(levelname)s',
            '%(name)s',
            '%(message)s'
        ]
    )
    logging.basicConfig(
        format=log_entry_format,
        level=logging.INFO,
        filename="consolidation.log"
    )
    logger = logging.getLogger(__name__)

    logger.info("Starting consolidation work.")

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument(
        "path",
        nargs='+',
        help="List of paths to search."
    )
#    parser.add_argument(
#        "-f",
#        "--filter",
#        dest='file_filter',
#        default='*',
#        help="Set the search filter for files, default \"*\"."
#    )
    parser.add_argument(
        "-o",
        "--host-file",
        dest='host_file',
        default='hosts.cfg',
        help="Changes the hosts config file."
    )
    parser.add_argument(
        "-O",
        "--hostgroup-file",
        dest='hostgroup_file',
        default='hostgroups.cfg',
        help="Changes the hostgroups config file."
    )
    parser.add_argument(
        "-D",
        "--hostdependency-file",
        dest='hostdependency_file',
        default='hostdependencies.cfg',
        help="Changes the hostdependencies config file."
    )
    parser.add_argument(
        "-E",
        "--hostescalation-file",
        dest='hostescalation_file',
        default='hostescalations.cfg',
        help="Changes the hostescalations config file."
    )
    parser.add_argument(
        "-F",
        "--hostextinfo-file",
        dest='hostextinfo_file',
        default='hostextinfo.cfg',
        help="Changes the hostextinfo config file."
    )
    parser.add_argument(
        "-s",
        "--service-file",
        dest='service_file',
        default='services.cfg',
        help="Changes the services config file."
    )
    parser.add_argument(
        "-S",
        "--servicegroup-file",
        dest='servicegroup_file',
        default='servicegroups.cfg',
        help="Changes the servicegroups config file."
    )
    parser.add_argument(
        "-H",
        "--servicedependency-file",
        dest='servicedependency_file',
        default='servicedependencies.cfg',
        help="Changes the servicedependencies config file."
    )
    parser.add_argument(
        "-I",
        "--serviceescalation-file",
        dest='serviceescalation_file',
        default='serviceescalations.cfg',
        help="Changes the serviceescalations config file."
    )
    parser.add_argument(
        "-J",
        "--serviceextinfo-file",
        dest='serviceextinfo_file',
        default='serviceextinfo.cfg',
        help="Changes the serviceextinfs config file."
    )
    parser.add_argument(
        "-c",
        "--contact-file",
        dest='contact_file',
        default='contacts.cfg',
        help="Changes the contacts config file."
    )
    parser.add_argument(
        "-C",
        "--contactgroup-file",
        dest='contactgroup_file',
        default='contactgroups.cfg',
        help="Changes the contactgroups config file."
    )
    parser.add_argument(
        "-t",
        "--timeperiod-file",
        dest='timeperiod_file',
        default='timeperiods.cfg',
        help="Changes the timeperiods config file."
    )
    parser.add_argument(
        "-p",
        "--command-file",
        dest='command_file',
        default='commands.cfg',
        help="Changes the commands config file."
    )
    parser.add_argument(
        "-a",
        "--append",
        default=False,
        action='store_true',
        help="Appends the files rather then overwriting."
    )
    args = parser.parse_args()
    # file_filter = args.file_filter
    file_filter = "*.cfg"
    host_file_name = args.host_file
    hostgroup_file_name = args.hostgroup_file
    hostdependency_file_name = args.hostdependency_file
    hostescalation_file_name = args.hostescalation_file
    hostextinfo_file_name = args.hostextinfo_file
    service_file_name = args.service_file
    servicegroup_file_name = args.servicegroup_file
    servicedependency_file_name = args.servicedependency_file
    serviceescalation_file_name = args.serviceescalation_file
    serviceextinfo_file_name = args.serviceextinfo_file
    contact_file_name = args.contact_file
    contactgroup_file_name = args.contactgroup_file
    timeperiod_file_name = args.timeperiod_file
    command_file_name = args.command_file
    if (args.append):
        open_mode = 'a'
    else:
        open_mode = 'w'

    regex_dict = {
        "host": re.compile("^define host( |){"),
        "hostgroup": re.compile("^define hostgroup( |){"),
        "hostdependency": re.compile("^define hostdependency( |){"),
        "hostescalation": re.compile("^define hostescalation( |){"),
        "hostextinfo": re.compile("^define hostextinfo( |){"),
        "service": re.compile("^define service( |){"),
        "servicegroup": re.compile("^define servicegroup( |){"),
        "servicedependency": re.compile("^define servicedependency( |){"),
        "serviceescalation": re.compile("^define serviceescalation( |){"),
        "serviceextinfo": re.compile("^define serviceextinfo( |){"),
        "contact": re.compile("^define contact( |){"),
        "contactgroup": re.compile("^define contactgroup( |){"),
        "timeperiod": re.compile("^define timeperiod( |){"),
        "command": re.compile("^define command( |){"),
        "end": re.compile("^}$"),
        "comment": re.compile("^#"),
        "blankline": re.compile("^\s*$"),
    }

    for path in args.path:
        host_file_dict, hostgroup_file_dict, hostdependency_file_dict, hostescalation_file_dict, hostextinfo_file_dict, service_file_dict, servicegroup_file_dict, servicedependency_file_dict, serviceescalation_file_dict, serviceextinfo_file_dict, contact_file_dict, contactgroup_file_dict, timeperiod_file_dict, command_file_dict = file_walk(
            path,
            file_filter,
            regex_dict
        )

    host_file_handler = open(host_file_name, open_mode)
    hostgroup_file_handler = open(hostgroup_file_name, open_mode)
    hostdependency_file_handler = open(hostdependency_file_name, open_mode)
    hostescalation_file_handler = open(hostescalation_file_name, open_mode)
    hostextinfo_file_handler = open(hostextinfo_file_name, open_mode)
    service_file_handler = open(service_file_name, open_mode)
    servicegroup_file_handler = open(servicegroup_file_name, open_mode)
    servicedependency_file_handler = open(servicedependency_file_name, open_mode)
    serviceescalation_file_handler = open(serviceescalation_file_name, open_mode)
    serviceextinfo_file_handler = open(serviceextinfo_file_name, open_mode)
    contact_file_handler = open(contact_file_name, open_mode)
    contactgroup_file_handler = open(contactgroup_file_name, open_mode)
    timeperiod_file_handler = open(timeperiod_file_name, open_mode)
    command_file_handler = open(command_file_name, open_mode)

    build_config(host_file_handler, host_file_dict)
    build_config(hostgroup_file_handler, hostgroup_file_dict)
    build_config(hostdependency_file_handler, hostdependency_file_dict)
    build_config(hostescalation_file_handler, hostescalation_file_dict)
    build_config(hostextinfo_file_handler, hostextinfo_file_dict)
    build_config(service_file_handler, service_file_dict )
    build_config(servicegroup_file_handler, servicegroup_file_dict)
    build_config(servicedependency_file_handler, servicedependency_file_dict)
    build_config(serviceescalation_file_handler, serviceescalation_file_dict)
    build_config(serviceextinfo_file_handler, serviceextinfo_file_dict)
    build_config(contact_file_handler, contact_file_dict )
    build_config(contactgroup_file_handler, contactgroup_file_dict)
    build_config(timeperiod_file_handler, timeperiod_file_dict)
    build_config(command_file_handler, command_file_dict)

    host_file_handler.close()
    hostgroup_file_handler.close()
    hostdependency_file_handler.close()
    hostescalation_file_handler.close()
    hostextinfo_file_handler.close()
    service_file_handler.close()
    servicegroup_file_handler.close()
    servicedependency_file_handler.close()
    serviceescalation_file_handler.close()
    serviceextinfo_file_handler.close()
    contact_file_handler.close()
    contactgroup_file_handler.close()
    timeperiod_file_handler.close()
    command_file_handler.close()

    return 0

if __name__ == '__main__':
    main()


from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def func(name):
    print("name:" +name)
    info('function f')

if __name__ == '__main__':
    info('main line')
    p = Process(target=func, args=('bob',))
    p2 = Process(target=func, args=('allice',))
    p.start()
    p2.start()
    p2.join()
    p.join()


#!/usr/bin/env python
# coding=utf-8

import logging
from eventlet.green import os
from storm.locals import create_database, Store

import runtime.path
from runtime import env

_database = None
_store = None
logger = logging.getLogger(__name__)

db_uri = os.path.abspath(runtime.path.join(env.get('dir_data'), 'core.db'))
logger.debug("database path: `%s'" % db_uri)

def get_store():
    global _database, _store
    if not _database:
        _database = create_database('sqlite:///%s' % db_uri)
    if not _store:
        _store = Store(_database)

    return _store

def init():
    import sqlite3
    logger.debug('create tables.')
    with open(runtime.path.resources_path('schema.sql')) as fp:
        conn = sqlite3.connect(db_uri)
        conn.executescript(fp.read())


import numpy as np
import matplotlib.pyplot as plt
import re
import os
os.makedirs('./plots/', exist_ok=True)

def add_entry(runtime_dict, px, py, size, time):
    if (px, py, size) not in runtime_dict:
        runtime_dict[(px, py, size)] = []
    runtime_dict[(px, py, size)].append(time)

if __name__ == '__main__':
    with open('./times.txt', 'r') as f:
        content = f.readlines()

    runtimes1D = {}
    runtimes2D = {}
    for line in content:
        result = re.search(r"configuration px = (\d*), py = (\d*), size = (\d*), runtime 1D = (\d*.?\d*), runtime 2D = (\d*.?\d*)", line)
        px, py, size, runtime1D, runtime2D = [float(i) for i in result.groups()]

        add_entry(runtimes1D, px, py, size, runtime1D)
        add_entry(runtimes2D, px, py, size, runtime2D)

    boxplot_1d_runtimes = []
    boxplot_2d_runtimes = []
    boxplot_small_1d = []
    boxplot_small_2d = []
    for i, (config, val) in enumerate(runtimes1D.items()):
        boxplot_1d_runtimes.append(val)
        if config[-1] == 4096:
            boxplot_small_1d.append(val)    # for just size 4096

    for i, (config, val) in enumerate(runtimes2D.items()):
        boxplot_2d_runtimes.append(val)
        if config[-1] == 4096:
            boxplot_small_2d.append(val)    # for just size 4096

    # plot of all values
    bp1 = plt.boxplot(boxplot_1d_runtimes, positions=[1,2,1,2], patch_artist=True, boxprops=dict(facecolor="C0"))
    bp2 = plt.boxplot(boxplot_2d_runtimes, positions=[1,2,1,2], patch_artist=True, boxprops=dict(facecolor="C2"))

    plt.xticks(ticks = range(3), labels = ["", (2,2), (2,4)], rotation = 30)  # required to fix label alignment issue
    plt.xlabel("Configurations")
    plt.ylabel("Runtime in seconds")
    plt.legend([bp1["boxes"][0], bp2["boxes"][0]], ['1D', '2D'], bbox_to_anchor = (1,1))
    plt.title("Configuration Vs Runtime")
    plt.tight_layout()

    plt.savefig('./plots/runtimes.png', dpi=300)
    plt.close()

    # plot of just size=4096 values
    bp1 = plt.boxplot(boxplot_small_1d, positions=[1,2], patch_artist=True, boxprops=dict(facecolor="C0"))
    bp2 = plt.boxplot(boxplot_small_2d, positions=[1,2], patch_artist=True, boxprops=dict(facecolor="C2"))

    plt.xticks(ticks = range(3), labels = ["", (2,2), (2,4)], rotation = 30)  # required to fix label alignment issue
    plt.xlabel("Configurations")
    plt.ylabel("Runtime in seconds")
    plt.legend([bp1["boxes"][0], bp2["boxes"][0]], ['1D', '2D'], bbox_to_anchor = (1,1))
    plt.title("Configuration Vs Runtime for data size = 4096")
    plt.tight_layout()

    plt.savefig('./plots/runtimes_size_4096.png', dpi=300)
    plt.close()


import sys
import subprocess
import re
import string
import random
import types
import socket

# Version
VERSION = '1.2.3'

# Utilities
SUDO = '/usr/bin/sudo'
IFCONFIG = '/sbin/ifconfig'
ROUTE = '/sbin/route'
NETSTAT = '/sbin/netstat'
NDP = '/usr/sbin/ndp'
ARP = '/usr/sbin/arp'

# Helper functions
def perror(*args):
    sys.stderr.write(*args)
    sys.stderr.write("\n")

def execute_cmd(cmd):
  print('Executing: %s' % cmd)
  status, output = subprocess.getstatusoutput(cmd)
  if status == 0:  # unix/linux subprocess 0 true, 1 false
    print(output)
    return True
  else:
    perror(output)
    return False

def matches(arg, command):
  return command.startswith(arg)

# Handles passsing return value, error messages and program exit on error
def help_msg(help_func):
  def wrapper(func):
    def inner(*args, **kwargs):
      if not func(*args, **kwargs):
        specific = eval(help_func)
        if specific:
          if type(specific) == types.FunctionType:
            if args and kwargs:
              specific(*args, **kwargs)
            else:
              specific()
            return False
          else:
            raise Exception("Function expected for: " + help_func)
        else:
          raise Exception("Function variant not defined: " + help_func)
      return True
    return inner
  return wrapper

# Generate random MAC address with XenSource Inc. OUI
# http://www.linux-kvm.com/sites/default/files/macgen.py
def randomMAC():
  mac = [ 0x00, 0x16, 0x3e,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]
  return ':'.join(map(lambda x: "%02x" % x, mac))

# Help
def do_help():
  perror("Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }")
  perror("       ip -V")
  perror("where  OBJECT := { link | addr | route | neigh }")
  perror("       OPTIONS := { -4 | -6 }")
  perror("")
  perror("iproute2ios")
  perror("Homepage: https://github.com/qwqVictor/iproute2ios")
  perror("Original Author: Bronislav Robenek")
  perror("This is CLI wrapper for basic network utilities on jailbroken iOS inspired with iproute2 on Linux systems.")
  perror("Provided functionality is limited and command output is not fully compatible with iproute2.")
  perror("For advanced usage use netstat, ifconfig, ndp, arp, route and networksetup directly.")
  exit(255)

def do_help_route():
  perror( "Usage: ip route list")
  perror( "       ip route get ADDRESS")
  perror( "       ip route { add | del } ROUTE")
  perror( "ROUTE := PREFIX [ nexthop NH ]")
  exit(255)

def do_help_addr():
  perror( "Usage: ip addr show [ dev STRING ]")
  perror( "       ip addr { add | del } PREFIX dev STRING")
  exit(255)

def do_help_link():
  perror( "Usage: ip link show [ DEVICE ]")
  perror( "       ip link set dev DEVICE")
  perror( "                [ { up | down } ]")
  perror( "                [ address { LLADDR | factory | random } ]")
  perror( "                [ mtu MTU ]")
  exit(255)

def do_help_neigh():
  perror( "Usage: ip neighbour { show | flush } [ dev DEV ]") # delete, add
  exit(255)

# Route Module
@help_msg('do_help_route')
def do_route(argv,af):
  if not argv:
    return do_route_list(af)
  elif 'add'.startswith(argv[0]) and len(argv) >= 4:
    if len(argv) > 0:
      argv.pop(0)
    return do_route_add(argv,af)
  elif 'delete'.startswith(argv[0]) and len(argv) >= 2:
    if len(argv) > 0:
      argv.pop(0)
    return do_route_del(argv,af)
  elif 'list'.startswith(argv[0]) or 'show'.startswith(argv[0]) or 'lst'.startswith(argv[0]):
    # show help if there is an extra argument on show
    if len(argv) > 1: return False
    return do_route_list(af)
  elif 'get'.startswith(argv[0]) and len(argv) == 2:
    argv.pop(0)
    return do_route_get(argv,af)
  else:
    return False
  return True

def do_route_list(af):
  if af==6:
    status,res = subprocess.getstatusoutput(NETSTAT + " -nr -f inet6 2>/dev/null")
  else:
    status,res = subprocess.getstatusoutput(NETSTAT + " -nr -f inet 2>/dev/null")
  if status:
      perror(res)
      return False
  res=res.split('\n')
  res=res[4:] # Removes first 4 lines
  for r in res:
    ra=r.split()
    if af == 6:
      target = ra[0]
      gw     = ra[1]
      flags  = ra[2]
      dev    = ra[3]
      target = re.sub('%[^ ]+/','/',target)
      if flags.find('W') != -1 or flags.find('H') != -1:
        continue
      if re.match("link.+",gw):
        print(target + ' dev ' + dev + '  scope link')
      else:
        print(target + ' via ' + gw + ' dev ' + dev)
    else:
      target = ra[0]
      gw     = ra[1]
      flags  = ra[2]
      dev    = ra[5]
      if flags.find('W') != -1 or flags.find('H') != -1:
        continue
      if target == 'default':
        print('default via ' + gw + ' dev ' + dev)
      else:
        dots=target.count('.')
        if target.find('/') == -1:
          addr=target
          netmask=8+dots*8
        else:
          [addr,netmask] = target.split('/')

        if dots == 2:
          addr = addr + '.0'
        elif dots == 1:
          addr = addr + '.0.0'
        elif dots == 0:
          addr = addr + '.0.0.0'

        if re.match("link.+",gw):
          print(addr + '/' + str(netmask)+ ' dev ' + dev + '  scope link')
        else:
          print(addr + '/' + str(netmask) + ' via ' + gw + ' dev ' + dev)
  return True

def do_route_add(argv,af):
  target=argv[0]
  via=argv[1]
  gw=argv[2]
  if via not in ['via','nexthop','gw','dev']:
    do_help_route()
  inet=""
  if ":" in target or af==6:
    af=6
    inet="-inet6 "
  if "dev"==via: gw = "-interface " + gw
  return execute_cmd(SUDO + " " + ROUTE + " add " + inet + target + " " + gw)

def do_route_del(argv,af):
  target=argv[0]
  inet=""
  if ":" in target or af==6:
    af=6
    inet="-inet6 "
  return execute_cmd(SUDO + " " + ROUTE + " delete " + inet + target)

def do_route_get(argv,af):
  target=argv[0]

  inet=""
  if ":" in target or af==6:
    af=6
    inet="-inet6 "
    family=socket.AF_INET6
  else:
    family=socket.AF_INET

  status,res = subprocess.getstatusoutput(ROUTE + " -n get " + inet + target)
  if status: # unix status or not in table
    perror(res)
    return False
  if res.find('not in table') >= 0:
      perror(res)
      exit(1)

  res=dict(re.findall('^\W*((?:route to|destination|gateway|interface)): (.+)$',res, re.MULTILINE))

  route_to=res['route to']
  dev=res['interface']
  via=res.get('gateway',"")

  try:
    s = socket.socket(family, socket.SOCK_DGRAM)
    s.connect((route_to, 7))
    src_ip = s.getsockname()[0]
    s.close()
    src = "  src " + src_ip
  except:
    src = ""

  if via=="":
    print(route_to + " dev " + dev + src)
  else:
    print(route_to + " via " + via + " dev " + dev + src)
  return True

# Addr Module
@help_msg('do_help_addr')
def do_addr(argv,af):
  if not argv:
    return do_addr_show(argv,af)
  elif 'add'.startswith(argv[0]) and len(argv) >= 3:
    if len(argv)>0:
      argv.pop(0)
    return do_addr_add(argv,af)
  elif 'delete'.startswith(argv[0]) and len(argv) >= 3:
    if len(argv)>0:
      argv.pop(0)
    return do_addr_del(argv,af)
  elif 'list'.startswith(argv[0]) or 'show'.startswith(argv[0]) or 'lst'.startswith(argv[0]):
    if len(argv)>0:
      argv.pop(0)
    return do_addr_show(argv,af)
  else:
    return False
  return True

def addr_repl_netmask(matchobj):
  hexmask=matchobj.group(1)
  netmask=int(hexmask, 16)
  cidr = 0
  while netmask:
    cidr += netmask & 0x1
    netmask >>= 1
  return "/%d" % cidr

def do_addr_show(argv,af):
  if len(argv) > 0 and argv[0]=='dev':
    argv.pop(0)
  if len(argv) > 0:
    param=argv[0]
  else:
    param="-a"

  status,res = subprocess.getstatusoutput(IFCONFIG + " " + param + " 2>/dev/null")
  if status:
    if res == "": perror(param + ' not found')
    else: perror(res)
    return False
  res=re.sub('(%[^ ]+)? prefixlen ([\d+])','/\\2',res)
  res=re.sub(' netmask 0x([0-9a-fA-F]+)', addr_repl_netmask, res)
  res=re.sub('broadcast', 'brd', res)

  SIX=""
  if af == 6:
    SIX="6"
  elif af == 4:
    SIX=" "

  address_count=0
  output=""
  buff=""
  ifname=""
  for r in res.split('\n'):
    if re.match('^\w',r):
      if address_count > 0:
        output += buff
      buff=""
      ifname=re.findall("^([^:]+): .+",r)[0]
      address_count=0
      buff += r.rstrip() + "\n"
    elif re.match('^\W+inet' + SIX + '.+',r):
      address_count+=1
      if re.match('^\W+inet .+',r):
        buff += r.rstrip() + " " + ifname + "\n"
      else:
        buff += r.rstrip() + "\n"
    elif re.match('^\W+ether.+',r):
      buff += r.rstrip() + "\n"

  if address_count > 0:
    output += buff
  print(output.rstrip())
  return True

def do_addr_add(argv,af):
  if len(argv) < 2:
    return False
  if argv[1]=="dev":
    argv.pop(1)
  else:
    return False
  try:
    addr=argv[0]
    dev=argv[1]
  except IndexError:
    perror('dev not found')
    exit(1)
    return False
  inet=""
  if ":" in addr or af==6:
    af=6
    inet=" inet6"
  return execute_cmd(SUDO + " " + IFCONFIG + " " + dev + inet + " add " + addr)

def do_addr_del(argv,af):
  if len(argv) < 2:
    return False
  if argv[1]=="dev":
    argv.pop(1)
  try:
    addr=argv[0]
    dev=argv[1]
  except IndexError:
    perror('dev not found')
    exit(1)
    return False
  inet="inet"
  if ":" in addr or af==6:
    af=6
    inet="inet6"
  return execute_cmd(SUDO + " " + IFCONFIG + " " + dev + " " + inet + " " + addr + " remove")

# Link module
@help_msg('do_help_link')
def do_link(argv,af):
  if (not argv):
    return do_link_show(argv,af)
  elif 'show'.startswith(argv[0]) or 'list'.startswith(argv[0]) or 'lst'.startswith(argv[0]):
    if len(argv) > 0:
      argv.pop(0)
    return do_link_show(argv,af)
  elif 'set'.startswith(argv[0]):
    if len(argv) > 0:
      argv.pop(0)
    return do_link_set(argv,af)
  else:
    return False
  return True

def do_link_show(argv,af):
  if len(argv) > 0 and argv[0]=='dev':
    argv.pop(0)
  if len(argv) > 0:
    param=argv[0]
  else:
    param="-a"

  status,res = subprocess.getstatusoutput(IFCONFIG + " " + param + " 2>/dev/null")
  if status: # unix status
    if res == "": perror(param + ' not found')
    else: perror(res)
    return False
  for r in res.split('\n'):
    if not re.match('\s+inet.+',r):
      print(r)
  return True

def do_link_set(argv,af):
  if not argv:
    return False
  elif argv[0]=='dev':
    argv.pop(0)

  if len(argv) < 2:
    return False

  dev = argv[0]

  try:
    args=iter(argv)
    for arg in args:
      if arg=='up':
        if not execute_cmd(SUDO + " " + IFCONFIG + " " + dev + " up"): return False
      elif arg=='down':
        if not execute_cmd(SUDO + " " + IFCONFIG + " " + dev + " down"): return False
      elif arg in ['address','addr','lladdr']:
        addr=args.next()
        if addr in ['random','rand']:
          addr=randomMAC()
        elif addr=='factory':
            return False
        if not execute_cmd(SUDO + " " + IFCONFIG + " " + dev + " lladdr " + addr): return False
      elif arg=='mtu':
        mtu=int(args.next())
        if not execute_cmd(SUDO + " " + IFCONFIG + " " + dev + " mtu " + str(mtu)): return False
  except:
    return False
  return True

# Neigh module
def do_neigh(argv,af):
  statuses = {'R': 'REACHABLE', 'S': 'STALE'}
  idev = None
  if len(argv) > 1:
    if len(argv) < 3 and argv[1] != 'dev':
      do_help_neigh()
    idev = argv[2]
  if (not argv) or (argv[0] in ['show','sh','s','list','lst','ls']):
    if af != 4:
      (status,res) = subprocess.getstatusoutput(NDP + " -an 2>/dev/null")
      if status != 0:
        return False
      res=res.split('\n')
      res=res[1:]
      for r in res:
        ra=r.split()
        l3a=re.sub('%.+$','',ra[0]) # remove interface
        l2a=ra[1]
        dev=ra[2]
        exp=ra[3]
        if ra[4] in statuses:
          stat=statuses[ra[4]]
        else:
          stat='INCOMPLETE'
        if l2a=='(incomplete)' and stat!='REACHABLE':
          print(l3a + ' dev ' + dev + ' INCOMPLETE')
        else:
          print(l3a + ' dev ' + dev + ' lladdr ' + l2a + ' ' + stat)
    if af != 6:
      if idev:
        (status,res)=subprocess.getstatusoutput(ARP + " -anli " + idev +" 2>/dev/null")
      else:
        (status,res)=subprocess.getstatusoutput(ARP + " -anl 2>/dev/null")
      if status != 0:
        return False
      res=res.split('\n')
      res=res[1:]
      for r in res:
        ra=r.split()
        l3a=ra[0]
        l2a=ra[1]
        dev=ra[4]
        if l2a=='(incomplete)':
          print(l3a + ' dev ' + dev + ' INCOMPLETE')
        else:
          print(l3a + ' dev ' + dev + ' lladdr ' + l2a + ' REACHABLE')
  # TODO: delete, add
  elif argv[0] in ['f', 'fl', 'flush']:
    if not idev:
        perror('Flush requires arguments.')
        exit(1)
    if af != 4:
      # TODO: dev option for ipv6 (ndp command doesn't support it now)
      if not execute_cmd(SUDO + " " + NDP + " -c"): return False
    if af != 6:
      if not execute_cmd(SUDO + " " + ARP + " -a -d -i " + idev): return False
  else:
    do_help_neigh()

  return True

# Match iproute2 subprocess
# https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/tree/ip/ip.c#n75
cmds = [
  [ "address",    do_addr ],
  [ "route",      do_route ],
  [ "neighbor",   do_neigh ],
  [ "neighbour",  do_neigh ],
  [ "link",       do_link ],
]

# Main
@help_msg('do_help')
def main(argv):
  argc=len(argv)

  if argc == 0:
    return False

  # Address family
  af=-1 # default / both
  if argv[0] == '-6':
    af=6
    argv.pop(0)
  elif argv[0] == '-4':
    af=4
    argv.pop(0)

  if not argv:
    return False

  if argv[0] == '-V':
    print("iproute2ios, v" + VERSION)
    exit(0)

  if argv[0] == 'help':
    do_help()
    exit(0)

  for cmd, cmd_func in cmds:
    if cmd.startswith(argv[0]):
      argv.pop(0)
      # Functions return true or terminate with exit(255) see help_msg and do_help*
      return cmd_func(argv, af)

  perror('Object "{}" is unknown, try "ip help".'.format(argv[0]))
  exit(1)

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


#transferl2amount/receiverid/receiverkey/receiverpositionid (SET THESE)
def transferl2():
        create_transfer_result = client.private.create_transfer(
                amount = transferl2amount,
                position_id = account['positionId'],
                expiration = epoch_seconds_to_iso(time() + 604801),
                receiver_account_id = receiverid,
                receiver_public_key = receiverkey,
                receiver_position_id = receiverpositionid,
        )


from datetime import date
print(f'{20*"-"}\n{"VOTAÇÃO":^20}\n{20*"-"}')

def voto(ano):
    print(30*'-')
    idade = date.today().year - ano
    print(f'Com {idade} anos, ', end='')
    if idade >= 70 or 16 <= idade <= 17:
        return opcoes[1]
    elif idade < 16:
        return opcoes[2]
    else:
        return opcoes[0]

opcoes = ('O VOTO É OBRIGATÓRIO!', 'O VOTO É OPCIONAL!', 'O VOTO É NEGADO!')
votacao = voto(int(input('Em que ano você nasceu ? ')))
print(votacao)


#import libraries

import lightgbm as lgb
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
import streamlit as st
import pickle
import bz2file as bz2
import altair as alt

from Antuns.prediction_LGBM import Prediction_LGBM
from Antuns.p1_import_csv import upload_csv
from Antuns.p2_plotting_data import curve_plot
from Antuns.page_setting import page_intro

page_intro()
#-----------------------------------------------------------------
#ignore version warnings
import warnings
warnings.filterwarnings("ignore")

#Global variables
THRESHOLD_GLOBAL = 0.75

#All wells to be trained
wells_name = ["01-97-HXS-1X", "15-1-SN-1X", "15-1-SN-2X", "15-1-SN-3XST", "15-1-SN-4X", "15-1-SNN-1P",
              "15-1-SNN-2P", "15-1-SNN-3P", "15-1-SNN-4P", "15-1-SNS-1P", "15-1-SNS-2P", "15-1-SNS-4P"]
#Obtain data and label of wells
name_features = ["GR", "LLD", "LLS", "NPHI", "RHOB", "DTC", "DTS"]
#-----------------------------------------------------------------

# Calculate the confusion matrix of applying model on dataframe (including features and label) df with threshold
def calculate_confusion_matrix (model = None, df= None, threshold=None):
    model_prediction = [model]
    # Apply model on dataframe
    proba = Prediction_LGBM(trained_models=model_prediction, data = df, feature_names=name_features)
    proba_well = proba.loc[:, "model_0"]
    # Apply threshold
    if threshold==None: threshold = 0.5
    # Get label from dataframe df
    well_proba = proba_well.apply(lambda x: 1 if x >= threshold else 0)
    return well_proba

#------------------------------------------------------------------
# Load any compressed pickle file
file = "models/LightGBM_0.45.pbz2"
def decompress_pickle(file):
    data = bz2.BZ2File(file, 'rb')
    data = pickle.load(data)
    return data
# model_best = decompress_pickle(file)

# Loading Modeling
model_best = lgb.Booster(model_file="models/LGBM_20221125.json")

#-------------------------------------------------------------------------------------------------

col_1, col_2, col_3, col_4, col_5, = st.columns(5)
# with col_3:
    # st.image("https://i.ibb.co/Yd42K98/LogoVPI.png", width=250)
    # st.header("Welcome to Fracture Prediction Dashboard!")

#Loading data from browser:----------------------------------------
st.subheader("1. Load CSV input file:")
wells_df_predict = upload_csv()
st.write('---')
if wells_df_predict is not None:
    wells_df_predict = wells_df_predict.replace({-9999: np.nan}).dropna(how='any', subset = ["FRACTURE_ZONE"])

    st.write("Data Input:")
    st.dataframe(wells_df_predict.sort_index(), width=1400, height=300)
    st.write('---')
    st.write("Selected Prediction Model:")
    st.write(model_best)
#------------------------------------------------------------------
    feature_names = [col for col in wells_df_predict.columns if col not in ["WELL", "DEPTH","FRACTURE_ZONE"]]
    # Full data for export data
    st.session_state.pred = st.button("Predict Fracture Zone")
    if st.session_state.pred:
        threshold = 0.23
        #Make label Prediction
        predictions = (model_best.predict(wells_df_predict[feature_names])> threshold).astype(int)
        wells_df_predict['FRACTURE_ZONE_PRED'] = predictions
        st.dataframe(wells_df_predict, width=1400, height=300)
#Plot Data------------------------------------------------------------------
        plotting_curves = [c for c in wells_df_predict.columns.unique() if c not in ["DEPTH", "WELL", "TVD", "FRACTURE_ZONE", "FRACTURE_ZONE_PRED", "DCALI_FINAL", "INCL", "AZIM_TN"]]
        plotting_curves.sort()
        if "FRACTURE_ZONE_PRED" in wells_df_predict.columns.unique():
            plotting_curves.append("FRACTURE_ZONE_PRED")
        for well in wells_df_predict.WELL.unique():
            st.write('---')
            st.write(f"{well} Logs: \n")
            well_plot = wells_df_predict[wells_df_predict.WELL == well]
            charts_dict={}
            for i, c in enumerate(plotting_curves):
                charts_dict[i] = curve_plot(data=well_plot,filted_data=None, x_column=c)
    #Show Curve-----------------------------------------------------------------------
            st.write(alt.concat(*charts_dict.values()).configure(autosize='fit'))
    # Download --------------------------------------------------------------
        st.write('---')
        st.write("Download final result to csv file")

        st.download_button(label='Download All Wells',
                        data = wells_df_predict.to_csv(),
                        file_name='FracturePredictionALL.csv',
                        mime='text/csv')


#!/usr/bin/env python3
import sys

import pybullet as p
import pybullet_data

model_fn = 'stompy.urdf'
if len(sys.argv) > 1:
    model_fn = sys.argv[1]

c = p.connect(p.GUI)
p.setPhysicsEngineParameter(enableFileCaching=0)
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #used by loadURDF
p.setGravity(0,0,-9.8)
planeId = p.loadURDF("plane.urdf")
if 'sdf' in model_fn:
    rid = p.loadSDF(model_fn)
elif 'urdf' in model_fn:
    cubeStartPos = [0,0,1]
    cubeStartOrientation = p.getQuaternionFromEuler([0,0,0])
    rid = p.loadURDF(model_fn,cubeStartPos, cubeStartOrientation)
p.setRealTimeSimulation(True)
#p.stepSimulation()
nj = p.getNumJoints(rid)
# store joint ids for all leg joints
legs = {}
print("N joints: %s" % (nj, ))
for ji in range(nj):
    print("Joint %i" % (ji, ))
    info = p.getJointInfo(rid, ji)
    name = info[1].decode('ascii')
    ts = name.split('_')
    if ts[2] == 'body':  # hip joint
        ln = ts[4]
        jn = 'hip'
    else:
        ln = ts[2]
        if ts[-1] == 'thigh':
            jn = 'thigh'
        elif ts[-1] == 'upper':
            jn = 'knee'
        elif ts[-1] == 'lower':
            jn = 'calf'
        else:
            jn = None
    print("\t%s" % (info, ))
    if jn is None:
        continue
    if ln not in legs:
        legs[ln] = {}
    legs[ln][jn] = ji
print("")
print("Joint IDs:", legs)

# move hips
i = input("any key to move knees")
jids = [legs[ln]['knee'] for ln in legs]
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[-0.5] * len(jids))
i = input("any key to move knees")
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[-0.0] * len(jids))

i = input("any key to move thighs")
jids = [legs[ln]['thigh'] for ln in legs]
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[0.5] * len(jids))
i = input("any key to move thighs")
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[0.0] * len(jids))

i = input("any key to move hips")
jids = [legs[ln]['hip'] for ln in legs]
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[0.5] * len(jids))
i = input("any key to move hips")
p.setJointMotorControlArray(
    rid, jids, p.POSITION_CONTROL, targetPositions=[0.0] * len(jids))

# move thighs
# move knees

i = input("any key to exit")
# move joints with setJointMotorControl2/Array
#p.setJointMotorControlArray(
#    rid, indices, p.POSITION_CONTROL,
#    targetPositions=[0.1] * 6
p.disconnect()


"""
======================
Author: 柠檬班-小简
Time: 2022/7/20 20:14
Project: day8_PO_v3-写完版本-无basepage
Company: 湖南零檬信息技术有限公司
======================
"""

mylist = ["py49", "小冷漠", "活出自己", "安稳"]

sex = ["男","男","男","男"]

for index in range(len(mylist)):
    print(index, mylist[index])

print(list(enumerate(mylist)))

# mydict = {}
# for index, value in enumerate(mylist,start=1):
#     mydict[value] = sex[index]


lista = []
c = mai = men = 0

while True:
    valor = int(input(f'Digite o valor da posição {c} '))
    lista.append(valor)
    if c == 0:
        mai = men = valor
    elif valor > mai:
        mai = valor
    elif valor < men:
        men = valor

    c += 1
    if c == 5:
        break

print(f'Os números digitados foram {lista}')
print(f'O maior valor digitado foi {mai} nas posições', end=' ')
for i, v in enumerate(lista):
    if v == mai:
        print(f'{i}', end='...')
print()
print(f'O menor valor digitado foi {men} nas posições', end=' ')
for i, v in enumerate(lista):
    if v == men:
        print(f'{i}', end='...')


'''
Created on 2013-2-27

@author: tinyms
'''
import sys,hashlib,os,re
import concurrent.futures
import socket,json
import urllib.request
import urllib.parse
#from datetime import date,datetime
from bs4 import BeautifulSoup
from PyQt5 import QtCore,QtGui

import sqlite3

def connect_db():
    cnn = sqlite3.connect("_cache")
    #cnn.text_factory = str
    return cnn

def create_history_db():
    cnn = connect_db()
    cur = cnn.cursor()
    cur.executescript("""
    CREATE TABLE IF NOT EXISTS [Match] (
      [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
      [Score] NVARCHAR(5),
      [Detect_Result] NVARCHAR(3),
      [Asia] INT DEFAULT (0),
      [Result] REAL DEFAULT (0.00),
      [Actual_Result] INT DEFAULT (-1),
      [Top_Draw] REAL DEFAULT (0.00),
      [TeamNames] NVARCHAR(255),
      [Last10TextStyle] NVARCHAR(30),
      [Last6TextStyle] NVARCHAR(30),
      [Last4TextStyle] NVARCHAR(30),
      [Last4BattleHistoryDesc] NVARCHAR(20),
      [Odds_WL] NVARCHAR(20),
      [Odds_AM] NVARCHAR(20),
      [Odds_LB] NVARCHAR(20),
      [Odds_365] NVARCHAR(20),
      [Odds_YSB] NVARCHAR(20),
      [Odds_WL_Change] NVARCHAR(20),
      [Odds_AM_Change] NVARCHAR(20),
      [Odds_LB_Change] NVARCHAR(20),
      [Odds_365_Change] NVARCHAR(20),
      [Odds_YSB_Change] NVARCHAR(20),
      [EventName] NVARCHAR(20)
      );
    """)
    cnn.commit()
    cur.close()

def batch_save_history_matchs(ds):
    try:
        rows = []
        for row in ds:
            if row["db_actual_result"] == -1:
                continue
            companys = 0
            if row["Odds_WL"]:
                companys += 1
            if row["Odds_AM"]:
                companys += 1
            if row["Odds_LB"]:
                companys += 1
            if row["Odds_365"]:
                companys += 1
            if row["Odds_YSB"]:
                companys += 1
            if companys < 3:
                continue
            detect_result(row)
            #_batch_save_history_matchs_for_score(row)
            tmp = dict()
            tmp["Score"] = row["score"]
            tmp["Detect_Result"] = row["result"]
            tmp["Asia"] = row["db_asian"]
            tmp["Result"] = row["asia_avg_balls"]
            tmp["Actual_Result"] = row["db_actual_result"]
            tmp["TeamNames"] = " VS ".join(row["team_names"])
            tmp["Last10TextStyle"] = row["last_10_text_style"]
            tmp["Last6TextStyle"] = row["last_6_text_style"]
            tmp["Last4TextStyle"] = row["last_4_text_style"]
            tmp["Last4BattleHistoryDesc"] = row["last_4_status_text_style"]

            tmp["Odds_WL"] = row["Odds_WL"]#新增
            tmp["Odds_AM"] = row["Odds_AM"]#新增
            tmp["Odds_LB"] = row["Odds_LB"]#新增
            tmp["Odds_365"] = row["Odds_365"]#新增
            tmp["Odds_YSB"] = row["Odds_YSB"]#新增

            tmp["Top_Draw"] = 0.00
            if tmp["Odds_WL"]:
                nums = [float(f) for f in tmp["Odds_WL"].split(" ")]
                draw = nums[1]
                tmp["Top_Draw"] = draw - int(draw)

            tmp["Odds_WL_Change"] = row["Odds_WL_Change"]#新增
            tmp["Odds_AM_Change"] = row["Odds_AM_Change"]#新增
            tmp["Odds_LB_Change"] = row["Odds_LB_Change"]#新增
            tmp["Odds_365_Change"] = row["Odds_365_Change"]#新增
            tmp["Odds_YSB_Change"] = row["Odds_YSB_Change"]#新增

            tmp["EventName"] = row["season_name"]
            rows.append(tmp)
            pass

        cnn = connect_db()
        cur = cnn.cursor()
        sql = """
            INSERT INTO Match(Score,Detect_Result,Asia,Result,Actual_Result,Top_Draw,TeamNames,Last10TextStyle,Last6TextStyle,Last4TextStyle,Last4BattleHistoryDesc,
            Odds_WL,Odds_AM,Odds_LB,Odds_365,Odds_YSB,
            Odds_WL_Change,Odds_AM_Change,Odds_LB_Change,Odds_365_Change,Odds_YSB_Change,EventName)
            VALUES(:Score,:Detect_Result,:Asia,:Result,:Actual_Result,:Top_Draw,:TeamNames,:Last10TextStyle,:Last6TextStyle,:Last4TextStyle,:Last4BattleHistoryDesc,
            :Odds_WL,:Odds_AM,:Odds_LB,:Odds_365,:Odds_YSB,:Odds_WL_Change,:Odds_AM_Change,:Odds_LB_Change,:Odds_365_Change,:Odds_YSB_Change,:EventName)
        """
        cur.executemany(sql,rows)
        cnn.commit()
        cur.close()
    except:
        print("Insert to db failure.")
        info=sys.exc_info()
        print(info[0],":",info[1])

#初赔差异
def diff_wl_lb_first_odds(wl,lb):
    rt = {"flag":"","diff":""}
    if not wl or not lb:
        return rt
    wl_nums = [round(float(i),2) for i in wl.split(" ")]
    lb_nums = [round(float(i),2) for i in lb.split(" ")]
    #N,H,L => NoChange,High,Low
    if lb_nums[0]-wl_nums[0] > 0 :
        rt["flag"] = "H"
    elif lb_nums[0]-wl_nums[0] < 0 :
        rt["flag"] = "L"
    elif lb_nums[0]-wl_nums[0] == 0 :
        rt["flag"] = "N"

    if lb_nums[1]-wl_nums[1] > 0 :
        rt["flag"] += "H"
    elif lb_nums[1]-wl_nums[1] < 0 :
        rt["flag"] += "L"
    elif lb_nums[1]-wl_nums[1] == 0 :
        rt["flag"] += "N"

    if lb_nums[2]-wl_nums[2] > 0 :
        rt["flag"] += "H"
    elif lb_nums[2]-wl_nums[2] < 0 :
        rt["flag"] += "L"
    elif lb_nums[2]-wl_nums[2] == 0 :
        rt["flag"] += "N"

    items = list()
    items.append(lb_nums[0]-wl_nums[0])
    items.append(lb_nums[1]-wl_nums[1])
    items.append(lb_nums[2]-wl_nums[2])

    rt["diff"] = json.dumps(items)

    return rt

def list_history_matchs(model,event_name):
    cnn = connect_db()
    cnn.row_factory = sqlite3.Row
    cur = cnn.cursor()
    if model == "":
        sql2 = "SELECT * FROM match WHERE Event=:evt_name ORDER BY Model LIMIT 100"
        return cur.execute(sql2,{"evt_name":event_name})
    else:
        sql = "SELECT * FROM match WHERE Model=? AND Event=? LIMIT 100"
        return cur.execute(sql,(model,event_name))

def groupby_match_result(model,event_name):
    cnn = connect_db()
    cnn.row_factory = sqlite3.Row
    cur = cnn.cursor()
    if model == "":
        sql2 = "SELECT AR,COUNT(1) as scale FROM match WHERE Event=:evt_name GROUP BY AR LIMIT 100"
        return cur.execute(sql2,{"evt_name":event_name})
    else:
        sql = "SELECT AR,COUNT(1) as scale FROM match WHERE Model=? AND Event=? GROUP BY AR LIMIT 100"
        return cur.execute(sql,(model,event_name))

def cache_last_access_urls(url):
    if url.find("@")!=-1:
        return
    if url == "http://zc.trade.500.com/sfc/index.php":
        return
    if os.path.exists("data/urls-cache"):
        arr = json.loads(read_file("data/urls-cache"))
        if url in arr:
            return
        arr.append(url)
        if len(arr) > 20:
            write_file("data/urls-cache",json.dumps(arr[1:]))
        else:
            write_file("data/urls-cache",json.dumps(arr))
    else:
        arr = [url]
        write_file("data/urls-cache",json.dumps(arr))

def get_last_access_urls():
    if os.path.exists("data/urls-cache"):
        arr = json.loads(read_file("data/urls-cache"))
        arr.reverse()
        return arr
    return []

#utils funciton

#求列表排序后的中值
def median_value(numbers):
    return sum(numbers)/10

def median_value1(numbers):
    n = len(numbers)
    copy = numbers[:]
    copy.sort()
    if n & 1:
        print(int(n/2))
        return copy[int(n/2)]
    else:
        return (copy[int(n/2) - 1] + copy[int(n/2)]) / 2

#求列表中频繁出现的数字
def freq_num(numbers):
    results = set()

    freq = {}
    for i in range(len(numbers)):
        try:
            freq[numbers[i]] += 1
        except KeyError:
            freq[numbers[i]] = 1

    max = 0
    first_max_item = None
    print(freq)
    for k in freq:
        v = freq[k]
        if v > max:
            max = v
            first_max_item = k
    results.add(first_max_item)
    #find onter same nums item
    first_max_value = freq[first_max_item]
    for k in freq:
        v = freq[k]
        if first_max_value == v:
            results.add(k)

    return results

def trim_all(text):
    return "".join(text.split())

def md5(s):
    h = hashlib.new('ripemd160')
    h.update(bytearray(s.encode("utf8")))
    return h.hexdigest()

def url_with_params(url):
    r1 = urllib.parse.urlsplit(str(url))
    if r1.query!="":
        return True
    return False

def write_file(file_name,data):
    f = QtCore.QFile(file_name)
    if not f.open(QtCore.QIODevice.WriteOnly):
        return
    stream = QtCore.QTextStream(f)
    stream.setCodec("UTF-8")
    stream << data

def append_file(file_name,data):
    f = QtCore.QFile(file_name)
    if not f.open(QtCore.QIODevice.Append):
        return
    stream = QtCore.QTextStream(f)
    stream.setCodec("UTF-8")
    stream << data

def read_file(file_name):
    f = QtCore.QFile(file_name)
    if not f.open(QtCore.QIODevice.ReadOnly):
        return
    stream = QtCore.QTextStream(f)
    stream.setCodec("UTF-8")
    return str(stream.readAll())

def web_page_download(url,cached=True):
    soup = None
    while True:
        try:
            f = get_cache_web_file_name(url)
            if os.path.exists(f):
                html = read_file(f)
                soup = BeautifulSoup(html)
            else:
                web_page = urllib.request.urlopen(url,timeout=15)
                html = web_page.read()
                html = html.decode('gb18030')
                soup = BeautifulSoup(html)
                if url_with_params(url) and cached:
                    local_file = get_cache_web_file_name(url)
                    write_file(local_file,html)
            break
        except urllib.error.URLError as ex:
            info=sys.exc_info()
            #send_msg(info[1])
            print(info[0],":",info[1])
            continue
            #break
    return soup

def soup(local_file):
    if os.path.exists(local_file):
        html = read_file(local_file)
        return BeautifulSoup(html)
    return None

def mkdirs(path):
    isExists=os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        return True
    else:
        return False

def get_number(text):
    p = re.compile("\\d+")
    nums = p.findall(text)
    if len(nums)>0:
        return nums[0]
    return ""

def get_date_text(text):
    p = re.compile("\\d{4}-\\d{2}-\\d{2}")
    dates = p.findall(text)
    if len(dates)>0:
        return dates[0]
    return ""

def get_short_time(text):
    p = "\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}"
    r = re.compile(p)
    matchs = r.findall(text)
    if len(matchs)>0:
        return matchs[0]
    return ""

def get_int_numbers(text):
    p = re.compile("\\d+",re.M)
    return p.findall(text)

def get_float_numbers(text):
    p = re.compile("\\d+\\.\\d+",re.M)
    return p.findall(text)

def get_map_value(map_,key,defaut_=None):
    if map_.has_key(key):
        return map_[key]
    return defaut_

def get_zero_div(a,b,extra=0.01):
    try:
        r = a/b
    except ZeroDivisionError:
        r = a/(b+extra)
    return round(r,2)

def get_sina_vote_scale_url():
    parse = web_page_download("http://sports.sina.com.cn/iframe/lottery/sfc/iframe_index.html")
    if not parse:
        return ""
    links = parse.find_all("a")
    for link in links:
        text = link.get_text()
        if text == "火线伤停":
            return link["href"]
    return ""

def udp_server(deal_func):
    ip = ('127.0.0.1',MessageService.PORT)
    udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    while True:
        try:
            udp.bind(ip)
            break
        except:
            MessageService.PORT += 1
            ip = ('127.0.0.1',MessageService.PORT)
            continue

    while True:
        data,ip = udp.recvfrom(2028)
        if data == "close_message_loop":
            udp.close()
            break
        if data:
            if deal_func:
                deal_func.emit(str(data,encoding="utf8"))
    udp.close()

def send_msg(data):
    ip = ('127.0.0.1',MessageService.PORT)
    udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    udp.sendto(data.encode("utf8"),ip)
    udp.close()

class MessageService(QtCore.QThread):
    PORT = 8888
    signal_message_arrival = QtCore.pyqtSignal(str)
    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        try:
            udp_server(self.signal_message_arrival)
        except:
            info=sys.exc_info()
            print(info[0],":",info[1])

def DownloadWebPageTask(url):
    send_msg("下载 %s" % url)
    while True:
        try:
            web_page = urllib.request.urlopen(url,timeout=15)
            html = web_page.read()
            html = html.decode('gb18030')
            local_file = get_cache_web_file_name(url)
            write_file(local_file,html)
            break
        except urllib.error.URLError as ex:
            info=sys.exc_info()
            send_msg(info[1])
            print(info[0],":",info[1])
            send_msg("重下载 %s" % url)
            continue

def get_cache_web_file_name(url):
    return "cache_web_pages/%s/%s" % ("html",md5(url))

def batch_download_web_pages(urls):
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        for url in urls:
            f = get_cache_web_file_name(url)
            if os.path.exists(f):
                continue
            executor.submit(DownloadWebPageTask,url)
        send_msg("等待下载任务完成..")
        executor.shutdown()

def asian_rq(key):
    m = dict()
    m["平手"] = "0"
    m["平手/半球"] = "0.25"
    m["半球"] = "0.5"
    m["半球/一球"] = "0.75"
    m["一球"] = "1"
    m["一球/球半"] = "1.25"
    m["球半"] = "1.5"
    m["球半/两球"] = "1.75"
    m["两球"] = "2"
    m["两球/两球半"] = "2.25"
    m["两球半"] = "2.5"
    m["两球半/三球"] = "2.75"
    m["三球"] = "3"
    m["三球/三球半"] = "3.25"
    m["三球半"] = "3.5"
    m["三球半/四球"] = "3.75"
    m["四球"] = "4"
    sign = "-"
    if key:
        if key.find("受")!=-1:
            sign = "+"
            key = key.replace("受","")
        val = m.get(key)
        if val:
            return sign + val
    return ""

#For Matchs TreeView
class MatchsTreeItem(object):
    def __init__(self, data, parent=None):
        self.parentItem = parent
        self.itemData = data
        self.childItems = []

    def appendChild(self, item):
        self.childItems.append(item)

    def child(self, row):
        return self.childItems[row]

    def childCount(self):
        return len(self.childItems)

    def columnCount(self):
        return len(self.itemData)

    def data(self, column):
        try:
            if column == 0:
                return self.itemData["asia_avg_balls"]
            elif column == 1:
                return self.itemData["result"]
            elif column == 2:
                return self.itemData["actual_result"]
            elif column == 3:
                return self.itemData["odds_asian"]
            elif column == 4:
                return self.itemData["odds"]
            elif column == 5:
                return self.itemData['team_names']
            elif column == 6:
                return self.itemData['score']
            elif column == 7:
                return self.itemData["season_name"]
            elif column == 8:
                return self.itemData["match_date"]
            return ""
        except IndexError:
            return None

    def parent(self):
        return self.parentItem

    def row(self):
        if self.parentItem:
            return self.parentItem.childItems.index(self)
        return 0

#赛果预测
def detect_result(match):
    scores = []
    #scores += match["formula_total"].to_results()
    scores += match["formula_last10"].to_results()
    scores += match["formula_last6"].to_results()
    scores += match["formula_last4"].to_results()
    avg_balls = (match["formula_last10"].avg_balls + match["formula_last6"].avg_balls + match["formula_last4"].avg_balls)/3
    num = round(avg_balls,2)
    match["asia_avg_balls"] = "%.2f" % num
    #scores += match["formula_last_mid"].to_results()
    #r = Result(scores,match["odds"])
    #match["result"]=r.detect(match["formula_last4"])
    diff = abs(num)
    if diff>=0 and diff<=0.25:
        match["result"]= "1"
    elif diff>0.25 and diff < 0.75:
        if num > 0:
            match["result"]= "31"
        else:
            match["result"]= "10"
    elif diff >= 0.75:
        if num > 0:
            match["result"]= "3"
        else:
            match["result"]= "0"

class MatchsTreeModel(QtCore.QAbstractItemModel):
    HIDE_SCORE = False
    def __init__(self, data, parent=None):
        super(MatchsTreeModel, self).__init__(parent)
        titles = dict()
        titles["asia_avg_balls"]="实力差"
        titles["odds_asian"] = "欧亚"
        titles["result"] = "预测"
        titles["actual_result"]="赛果"
        titles["team_names"]="对阵球队"
        titles["score"]="比分"
        titles["odds"]="初盘(威廉)"
        titles["season_name"]="赛事"
        titles["match_date"]="比赛日期"
        self.rootItem = MatchsTreeItem(titles)

        self.setupModelData(data, self.rootItem)

    def columnCount(self, parent):
        if parent.isValid():
            return parent.internalPointer().columnCount()
        else:
            return self.rootItem.columnCount()

    def data(self, index, role):

        if not index.isValid():
            return None

        tip_index = -1
        if role == QtCore.Qt.DisplayRole:
            item = index.internalPointer()
            data = item.data(index.column())
            return data

        if role == QtCore.Qt.TextAlignmentRole:
            if 0==index.column() or 1==index.column() or 2==index.column():
                return QtCore.Qt.AlignCenter

        if role == QtCore.Qt.BackgroundRole:
            item = index.internalPointer()
            data = item.data(index.column())
            if index.column() == 4:
                if data:
                    arr = [float(s) for s in data.split(" ")]
                    draw = arr[1]
                    int_part = int(draw)
                    dot_part = draw - int_part
                    if round(dot_part,1) >= 0.4:
                        tip_index = index.column()
                        return QtGui.QColor("#FFFFCC")
                pass

            if 4 == index.column():
                return QtGui.QColor("#FFCC99")
            elif 0 == index.column():
                return QtGui.QColor("#FFFFCC")
            elif 1 == index.column():
                return QtGui.QColor("#99CC99")

        return None

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.NoItemFlags

        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self.rootItem.data(section)
        return None

    def index(self, row, column, parent):
        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()

        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        childItem = parentItem.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()

        childItem = index.internalPointer()
        parentItem = childItem.parent()

        if not parentItem:
            return QtCore.QModelIndex()

        if parentItem == self.rootItem:
            return QtCore.QModelIndex()

        return self.createIndex(parentItem.row(), 0, parentItem)

    def rowCount(self, parent):
        if parent.column() > 0:
            return 0

        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        return parentItem.childCount()

    def convert_odds_to_balls(self,odds):
        if not odds:
            return "0"
        items = odds.split(" ")
        nums = [float(i) for i in items]
        win_rate = round(1/nums[0],2)
        #draw_rate = round(1/nums[1],2)
        lost_rate = round(1/nums[2],2)
        diff = win_rate-lost_rate
        sign = "-"
        if diff < 0:
            sign = "+"
        balls = "%s%.2f" % (sign,round(abs(diff)/0.5,2))
        return balls

    def setupModelData(self, matchs, parent):
        for match in matchs:
            match["asian_rq_text_style"] = "%s%s%s" % (match["rq"],match["asian"],match["score"])
            detect_result(match)
            #odds_detect_exclude_one_result(match)
            match["odds"] = match["odds"];
            match["odds_asian"] = self.convert_odds_to_balls(match["Odds_WL"])
            if match["actual_result"].find("-1")!=-1:
                match["actual_result"]=""

            if match["score"] == "[]":
                match["score"] = ""

            if type(match['team_names']) == list:
                if MatchsTreeModel.HIDE_SCORE:
                    team_names = ' VS '.join(match['team_names'])
                else:
                    team_names = ' VS '.join(match['team_names'])
            else:
                team_names = match['team_names']

            match["team_names"] = team_names
            match["match_date"] = match["match_date"]
            first_child = MatchsTreeItem(match)
            parent.appendChild(first_child)
        pass


# 4. Programa que lea por teclado tres números enteros H, M, S
# correspondientes a hora, minutos y segundos respectivamente,
# y comprueba si la hora que indican es una hora válida.

def ejercicio4():
    # iniciamos 'otro4' para que entre en el while
    otro4="s"

    while (otro4=="s"):
        # pedimos la hora
        hora=int(input("Introduce la hora: "))
        mins=int(input("Introduce los minutos: "))
        segs=int(input("Introduce los segundos: "))

        correccion=" no " if not(hora>=0 and hora<24 and mins>=0 and mins<60 and segs>=0 and segs<60) else " "
        print(f"La hora indicada ({str(hora).zfill(2)}:{str(mins).zfill(2)}:{str(segs).zfill(2)}){correccion}es correcta")

        # preguntamos si queremos comprobar otra hora
        otro4=str(input("¿Deseas comprobar otra hora (s/n)? "))


import os
import sys
import time
from datetime import datetime
import numpy as np
import atomize.general_modules.general_functions as general
import atomize.device_modules.Termodat_11M6 as td

if len(sys.argv) > 1:
    test_flag = sys.argv[1]
else:
    test_flag = 'None'

tdat11m6 = td.Termodat_11M6()
timenow = str(datetime.now().strftime("%d-%m-%Y_%H-%M-%S"))
path_to_file = os.path.join('D:/Melnikov/', str(timenow) + '_Column_heating_test' + '.csv')

# Event handling flags
flag1 = 0
flag2 = 0

xs = []
ys1 = []
ys2 = []
ys3 = []
ys4 = []

tdat11m6.tc_setpoint('1','623.1')
tdat11m6.tc_setpoint('2','623.1')
tdat11m6.tc_setpoint('3','623.1')
tdat11m6.tc_setpoint('4','623.1')

# Parameters
heat_time = 3600 # in seconds

# Main loop
for i in range(36000):

    # for heating the indicated amount of time
    now = round(time.time())

    xs = np.append(xs, i)
    temp1 = tdat11m6.tc_temperature('1') - 273.1
    temp2 = tdat11m6.tc_temperature('2') - 273.1
    temp3 = tdat11m6.tc_temperature('3') - 273.1
    temp4 = tdat11m6.tc_temperature('4') - 273.1

    ys1 = np.append(ys1, temp1)
    ys2 = np.append(ys2, temp2)
    ys3 = np.append(ys3, temp3)
    ys4 = np.append(ys4, temp4)

    general.plot_1d('Plot 1', xs, ys1, label='ch1')
    general.plot_1d('Plot 2', xs, ys2, label='ch2')
    general.plot_1d('Plot 3', xs, ys3, label='ch3')
    general.plot_1d('Plot 4', xs, ys4, label='ch4')

    if flag1 == 0 and (temp1 > 350 or temp2 > 350 or temp3 > 350 or temp4 > 350):
        temp_reached = round(time.time())
        flag1 = 1
        general.message('Target temperature has been reached')

    if flag2 == 0 and flag1 == 0:
        pass
    elif flag2 == 0 and flag1 == 1:
        if abs(now - temp_reached) > heat_time:
            flag2 = 1
            tdat11m6.tc_setpoint('1','253')
            tdat11m6.tc_setpoint('2','253')
            tdat11m6.tc_setpoint('3','253')
            tdat11m6.tc_setpoint('4','253')
            general.message('Heating is OFF')
    else:
        pass

    if test_flag != 'test':

        head = '' # set empty header
        if not os.path.isfile(path_to_file): # checks if the file exists
            head = 'Time step: ' + '1 s\n'+ \
            'Ch1 - Ch2 - Ch3 - Ch4 (deg. Celcius):'  # if it doesn't then add the header
        file_save = open(path_to_file, 'a')

        np.savetxt(file_save, np.c_[xs[i], ys1[i], ys2[i], ys3[i], ys4[i]], fmt='%.1f', delimiter=',', \
            newline='\n', footer='', header=head, comments='# ', encoding=None)

        file_save.close()

    elif test_flag == 'test':
        pass

    general.wait('1 s')


# ID 1 -> 海防艦 -> Coastal Defence Ship (?)
# ID 2 -> 駆逐艦 -> Destroyer
# ID 3 -> 軽巡洋艦 -> Light Cruiser
# ID 4 -> 重雷装巡洋艦 -> Torpedo Cruiser
# ID 5 -> 重巡洋艦 -> Heavy Cruiser
# ID 6 -> 航空巡洋艦 -> Aviation Cruiser
# ID 7 -> 軽空母 -> Light Aircraft Carrier
# ID 8 -> 戦艦 -> Battleship
# ID 9 -> Also battleship
# ID 10 -> 航空戦艦 -> Aviation battleship
# ID 11 -> 正規空母 -> Aircraft carrier
# ID 12 -> 超弩級戦艦 -> Super Dreadnought
# ID 13 -> 潜水艦 -> Submarine
# ID 14 -> 潜水空母 -> Submarine Aircraft Carrier
# ID 15 -> 補給艦 -> Supply Vessel/Replenishment Oiler?
# ID 16 -> 水上機母艦 -> Seaplane Carrier
# ID 17 -> 揚陸艦 -> amphibious assault ship
# ID 18 -> 装甲空母 -> Armored aircraft carrier
# ID 19 -> 工作艦 -> Repair Ship
# ID 20 -> 潜水母艦 -> Submarine Tender
# ID 21 -> 練習巡洋艦 -> Practice Cruiser
# from db import db,Dock
import random

from flask import abort
from flask import g
from sqlalchemy import text

from db import Kanmusu, db
from . import MemberHelper

def calculate_repair_time(kanmusu: Kanmusu):
    # {Total repair time} = {HP loss} * {base repair time} * {ship type} + {30 seconds}
    # Our modified formula: Total = HP Loss * ((Build Time**0.6) + Level)
    return (kanmusu.ship.max_stats.hp - kanmusu.current_hp) * ((kanmusu.ship.buildtime**0.6) + kanmusu.level)

def get_ship_from_recipe(fuel: int=30, ammo: int=30, steel: int=30, baux: int=30) -> int:

    ship_t = text("""select ship_id, chance from recipe
      join recipe__resources min on recipe.min_resources_id = min.id
      join recipe__resources max on recipe.max_resources_id = max.id
      where
      :fuel between min.fuel and max.fuel and
      :ammo between min.ammo and max.ammo and
      :steel between min.steel and max.steel and
      :baux between min.baux and max.baux;
    """)

    ships = db.session.execute(ship_t, {"fuel": fuel, "ammo": ammo, "steel": steel, "baux": baux})

    if not ships:
        return None

    ship_choices = [[x[0]] * x[1] for x in ships]
    ship_choices = [item for sublist in ship_choices for item in sublist]
    return random.choice(ship_choices)

def get_and_remove_ship_kdock(dockid: int):
    admiral = g.admiral
    try:
        dock = admiral.docks_craft[dockid]
    except IndexError:
        abort(404)
        return None

    if dock.kanmusu is None:
        # ....
        abort(400)
        return

    dock.kanmusu.active = True

    api_data = {
        "api_ship_id": dock.kanmusu.ship.api_id,
        "api_kdock": MemberHelper.dock_data([dock], False),
        "api_id": dock.kanmusu.number,
        "api_slotitem": [], # TODO: Equipment!
        "api_ship": MemberHelper.kanmusu(kanmusu=dock.kanmusu)
    }
    # Update dock data.
    db.session.add(dock.kanmusu)
    dock.update(None)
    db.session.add(dock)
    db.session.commit()
    return api_data

def craft_ship(fuel: int, ammo: int, steel: int, baux: int, dockid: int):
    admiral = g.admiral
    ship = get_ship_from_recipe(fuel, ammo, steel, baux)
    if ship:
        nship = Kanmusu(ship)
        nship.active = False
        nship.number = len(g.admiral.kanmusu)
    else:
        print("Something bad happened. Where are your recipes?")
        abort(404)
        return

    # Change dock data.
    try:
        dock = admiral.docks_craft[dockid]
    except IndexError:
        abort(404)
        return

    if dock.kanmusu is not None:
        # wat
        abort(400)
        return

    dock = dock.update(nship, fuel, ammo, steel, baux, True)

    admiral.resources.sub(fuel, ammo, steel, baux)

    db.session.add(dock)
    admiral.add_kanmusu(nship)
    db.session.add(admiral)
    api_data = {
        "api_ship_id": nship.ship.api_id,
        "api_kdock": MemberHelper.dock_data([dock], False),
        "api_id": nship.number,
        "api_slotitem": [],
        "api_ship": MemberHelper.kanmusu(kanmusu=dock.kanmusu)
    }
    db.session.commit()
    return api_data


# from pudb import set_trace; set_trace()
from typing import List
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution1:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        """Not a difficult problem. All we need to do is DFS while keeping
        track of the height. As long as we DFS with left branch first, the very
        first node encountered on a lower level must be the left most leaf.

        O(N), 69 ms, 30% ranking.
        """
        left_leaf = [0, -1]

        def dfs(node, height) -> None:
            if not node.left and not node.right and height > left_leaf[1]:  # leaf
                left_leaf[0] = node.val
                left_leaf[1] = height
                return
            if node.left:
                dfs(node.left, height + 1)
            if node.right:
                dfs(node.right, height + 1)

        dfs(root, 0)
        return left_leaf[0]

class Solution2:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        """BFS. It is easier to implement and understand then DFS.

        O(N), 41 ms, 83% ranking.
        """
        res = 0
        queue = [root]
        while queue:
            temp = []
            res = queue[0].val
            for node in queue:
                if node.left:
                    temp.append(node.left)
                if node.right:
                    temp.append(node.right)
            queue = temp
        return res

# sol = Solution()
# tests = [
#     ([4,2,1,3], [[1,2],[2,3],[3,4]]),
#     ([1,3,6,10,15], [[1,3]]),
#     ([3,8,-10,23,19,-4,-14,27], [[-14,-10],[19,23],[23,27]]),
# ]

# for i, (arr, ans) in enumerate(tests):
#     res = sol.minimumAbsDifference(arr)
#     if res == ans:
#         print(f'Test {i}: PASS')
#     else:
#         print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')