#! /usr/local/bin/python3
# coding=utf-8
#
# -----------------------------------------------------------------------------
#
#       CLI Weather reader & printer
#       author: dpomondo
#       purpose: avoid looking out the window
#
# -----------------------------------------------------------------------------
#
# TODO:     1. Add method for making daily database file -- DONE
#               a. include method for clearing out old files
#           2. add configuration file (ability to save preferred printing
#              format)
#           3. Add ASCII-art print method for temp etc. graph
#           4. add Curse display method
#           5. Separate:    a. single current conditions call
#                           b. hourly forecast call
#                           c. 10-day forecast call
#                           d. debugging call (keys, time, update, etc)
#           6. light-weight version for TMUX use
#           7. JSON explorer
#
# -----------------------------------------------------------------------------

import sys
import time
import argparse
#  import logging
#  import config.loaders
#  log_file = config.loaders.log_file_name()
#  logging.basicConfig(filename=log_file, level=logging.DEBUG,
    #  format="%(levelname)s:%(name)s:%(asctime)s -- %(message)s")

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('--verbose', '-v',
                        action='store_true',
                        help='verbose flag'
                        )
    parser.add_argument('--keys', '-k',
                        action='store_true',
                        help='number of keys in database'
                        )
    parser.add_argument('--recent', '-r',
                        action='store_true',
                        help='time of most recent call'
                        )
    parser.add_argument('--query', '--requery', '-q',
                        action='store_true',
                        help='time until next re-query'
                        )
    parser.add_argument('--temperature', '-t',
                        action='store_true',
                        help='return current temperature in fahrenheit'
                        )
    parser.add_argument('--wind', '-w',
                        action='store_true',
                        help='return current wind speed and direction'
                        )
    parser.add_argument('--humidity', '-m',
                        action='store_true',
                        help='return current humidity'
                        )
    parser.add_argument('--times', '-i',
                        action='store_true',
                        help='list all times of todays server queries'
                        )
    parser.add_argument('--conditions', '-c',
                        action='store_true',
                        help='return current conditions'
                        )
    parser.add_argument('--all', '-a',
                        action='store_true',
                        help='return temperature, wind, ' +
                        'humidity and sky conditions'
                        )
    parser.add_argument('--now', '-n',
                        action='store_true',
                        help='return conditions as they are now'
                        )
    parser.add_argument('--hourly', '-o',
                        action='store_true',
                        help='return hourly forecast'
                        )
    parser.add_argument('--forecast', '-f',
                        nargs='?',
                        #  nargs='*',
                        const='g',
                        #  default='g',
                        #  action='store',
                        #  type=str,
                        help="""return 10 day forecast, with optional specified
                                format"""
                        )
    parser.add_argument('--update', '-u',
                        action='store_true',
                        help='force update of weather conditions database'
                        )
    parser.add_argument('--moon',
                        action='store_true',
                        help='return sunrise and sunset'
                        )
    parser.add_argument('--options', '-p',
                        action='store_true',
                        help='look at local options'
                        )
    # to understand the following, read the docs:
    # https://docs.python.org/3.4/library/argparse.html#nargs
    parser.add_argument('--debug',
                        action='store',
                        nargs='?',
                        const='c',
                        #  default='c',
                        #  type=str,
                        choices=['hour', 'h', 'forecast', 'f', 'current', 'c'],
                        help="""print out current server response for hour,
                                forecast, or current (default)"""
                        )
    parser.add_argument('--files',
                        action='store_true',
                        help='list database files in data directory'
                        )
    parser.add_argument('--logging',
                        action='store_true',
                        help="""test the logging configuration (returns nothing
                                to the command line)"""
                        )
    parser.add_argument('--width',
                        action='store',
                        nargs='?',
                        const=80,
                        type=int,
                        help="Limit screen width to given integer"
                        )
    parser.add_argument('--yesterday',
                        action='store_true',
                        help='Prints temperature graph for yesterday'
                        )
    return parser.parse_args()

def response_age(db_file):
    if 'current_observation' in db_file:
        res = db_file['current_observation']['observation_epoch']
        return int(time.time()) - int(res)
    else:
        # without gloabl variables this line fails
        # return time_out + 1
        #
        # this is a bad bad hacky thing
        return 601

def update(verbose=False, check_time=True):
    # do the thing
    # this is a very crummy emergency solution:
    home_dir = '/usr/self/weather'
    if home_dir not in sys.path:
        sys.path.append(home_dir)
    import config.loaders
    import json
    import shelve
    import updater
    import logging
    config_file = config.loaders.config_file_name()
    temp = config.loaders.load_vars(config_file=config_file)
    time_out = int(temp.get('time_out', 600))
    db_target = config.loaders.day_file_name()
    log_file = config.loaders.log_file_name()
    logging.basicConfig(filename=log_file, level=logging.DEBUG,
        format="%(levelname)s:%(name)s:%(asctime)s -- %(message)s")

    weat_db_file_open_flag = False
    try:
        weat_db_file = shelve.open(db_target)
        weat_db_file_open_flag = True
        if check_time and ('current_response' in weat_db_file):
            if response_age(weat_db_file['current_response']) < time_out:
                if verbose:
                    print("re-query too soon.")
                    print("re-query possible in {} seconds".format(
                        time_out
                        - response_age(weat_db_file['current_response'])))
            return
        try:
            now = updater.get_response(verbose=verbose, **temp)
            #  logging.info("Response from server at {}".format(time.time()))
        # except requests.exceptions.ConnectionError as e:
        except Exception as e:
            # print("{} caught in getter.update".format(e))
            # sys.exit()
            tb = sys.exc_info()[2]
            raise e.with_traceback(tb)
        if now is not None and 'current_observation' in now:
            logging.info("Response from server at {}".format(time.time()))
            if verbose:
                import pprint
                print('Function returned this:')
                pprint.pprint(now.keys())
            weat_db_file['current_response'] = now
            weat_db_file[now['current_observation']['observation_epoch']] = now
            # for some reason, trying to dump `now['current_response']` to
            # the json file RECALLS this function, looping & crashing
            # the whole thing
            nerd = {}
            for key in now:
                nerd[key] = now[key]
            with open(config.loaders.current_file_name(), "w") as outfile:
                json.dump(nerd, outfile, indent=2)
            if verbose:
                keys = list(weat_db_file.keys())
                print("weather_db has the following keys:")
                pprint.pprint(keys)
            logging.info("Added observation ({}) to weather database".format(
                now['current_observation']['observation_epoch']))
            del(nerd)
    except Exception as e:
        tb = sys.exc_info()[2]
        raise e.with_traceback(tb)
    finally:
        if weat_db_file_open_flag is True:
            weat_db_file.close()
            weat_db_file_open_flag = False

def print_bookkeeping(args, current_ob, weat_db):
    # TODO: everything here should be returned via a call to updater:
    #           1. keys
    #           2. observation_epoch
    #           3. response age
    #       Add the following to updater:
    #           1. number_of_keys()
    #           2. latest_call()
    #           3. response_age()
    if args.times or args.keys:
        import updater
        keys = updater.list_keys(weat_db, verbose=True)
        # from .updater import list_keys
        # keys = list_keys(weat_db)
    if args.times:
        for k in keys[:-1]:     # cut off 'current_response' key
            print('{}: {}'.format(k, time.ctime(int(k))))
    if args.keys:
        print("number of keys: {}".format(len(keys)))
    if args.recent:
        print("Latest call: {}".format(
            time.ctime(float(current_ob['current_observation']
                                       ['observation_epoch']))))
    if args.query:
        print("re-query possible in {} seconds".format(
            # time_out - response_age()))
            600 - response_age(current_ob)))

def main():
    """ Hooray, a giant if-elif-else tree!
    """
    # TODO:     1. move ALL this junk to main(), call from here
    #           2. open shelve file here
    #           3. check to see if there IS a current response key
    #           4. put open/close shelve in update so it can be called
    #              seperately
    # -------------------------------------------------------------------------
    # init variables
    # -------------------------------------------------------------------------
    home_dir = '/usr/self/weather'
    # here we make sure the package imports can go through:
    if home_dir not in sys.path:
        sys.path.append(home_dir)
    import config.loaders
    import logging
    import utils.utilities as utils
    config_file = config.loaders.config_file_name()
    weather_db_name = config.loaders.day_file_name()
    log_file = config.loaders.log_file_name()
    logging.basicConfig(filename=log_file, level=logging.DEBUG,
        format="%(levelname)s:%(name)s:%(asctime)s -- %(message)s")

    args = parse_arguments()
    logging.debug("Loaded args object. {} attrs in args.__dict__".format(
        len(args.__dict__)))

    configs = config.loaders.parse_config()
    loop_flag = True
    while loop_flag is True:
        try:
            if args.update:
                args.verbose = True
                update(verbose=args.verbose, check_time=True)
                loop_flag = False
            elif args.options:
                temp = config.loaders.load_vars(config_file)
                res = config.loaders.key_formatter(temp)
                for r in res:
                    print(r)
                loop_flag = False
            else:
                # only get current response object if we need it
                import returner
                current = returner.main()

                if args.width:
                    screen_width = min(utils.get_terminal_width(), args.width)
                else:
                    screen_width = utils.get_terminal_width()
                if args.logging:
                    logging.info("Sending test message -- all is well!")
                    loop_flag = False
                elif args.debug in ['current', 'c']:
                    res = config.loaders.key_formatter(
                        current['current_observation'])
                    for r in res:
                        print(r)
                    loop_flag = False
                elif args.debug in ['hourly', 'h']:
                    res = config.loaders.key_formatter(
                        current['hourly_forecast'][0])
                    for r in res:
                        print(r)
                    loop_flag = False
                elif args.debug in ['forecast', 'f']:
                    res = config.loaders.key_formatter(
                        current['forecast']
                               ['simpleforecast']
                               ['forecastday'][0])
                    for r in res:
                        print(r)
                    loop_flag = False
                elif args.files:
                    raise NotImplementedError
                elif args.yesterday:
                    import printers.prinutils as pu
                    import utils.file_utils as fu
                    import datetime as dt
                    tim = dt.date.today() - dt.timedelta(days=1)
                    target = config.loaders.day_file_name(tim)
                    target_keys = []
                    target_keys.append(('current_observation', 'temp_f'))
                    target_keys.append(('current_observation',
                                        'observation_epoch'))

                    opened = fu.parse_database(target, target_keys)
                    res = pu.day_temps_formatter(opened[target_keys[0]],
                                                 opened[target_keys[1]])
                    print("Queried Temperatures for {}".format(
                        tim.strftime('%b %d %y')))
                    for lin in res:
                        print(lin)
                elif args.moon:
                    import printers.moon
                    printers.moon.print_moon(current['moon_phase'])
                    loop_flag = False
                elif (args.keys or args.recent or args.query or args.times):
                    print_bookkeeping(args, current_ob=current,
                                      weat_db=weather_db_name)
                    loop_flag = False
                elif args.now or not (args.hourly or args.forecast):
                    import printers.current
                    printers.current.print_current(
                        current['current_observation'], args)
                    loop_flag = False
                elif args.hourly:
                    import printers.print_hourly
                    res = printers.print_hourly.print_hourly(
                        current['hourly_forecast'],
                        current['sun_phase'],
                        configs,
                        screen_width)
                    for lin in res:
                        print(lin)
                    loop_flag = False
                elif args.forecast:
                    #  print('args.forecst: {}'.format(args.forecast))
                    #  if args.forecast.startswith('w'):
                    if args.forecast[0].startswith('w'):
                        frmt = 'week'
                    elif args.forecast[0].startswith('g'):
                        frmt = 'grid'
                    else:
                        # the default, should be set in a config file
                        frmt = 'grid'
                    import printers.forecast
                    res = printers.forecast.print_forecast(
                        current['forecast']
                               ['simpleforecast']
                               ['forecastday'],
                        frmt=frmt,
                        screen_width=screen_width)
                    for lin in res:
                        print(lin)
                    loop_flag = False
            # if we've fallen through to here, do SOMETHING useful:
            if loop_flag is not False:
                update(verbose=args.verbose)
                loop_flag = False
        except KeyError as e:
            # update(verbose=args.verbose)
            message = "KeyError caught: {}".format(e)
            print(message)
            import traceback
            traceback.print_exception(*sys.exc_info())
            loop_flag = False
            logging.debug(message)
        except Exception as e:
            # tb = sys.exc_info()[2]
            message = "exception caught at top level: {}".format(e)
            print(message)
            import traceback
            traceback.print_exception(*sys.exc_info())
            loop_flag = False
            logging.debug(message)

if __name__ == '__main__':
    main()


import numpy as np

#Question 1
def f(x, t):
    return -0.001*(0 - 40) #plugging in given values into given DE
a = 0
b = 600
N = 600
h = (b - a) / N
T_0 = 0
x = 0 #initial conditions
time = np.arange(a, b, h)
for t in time: #Euler's method, using previous results to create new ones in T_n = T_0
    T_n = T_0 + h*f(x, t)
    T_0 = T_n
    t += h
print("The temperature using time steps of 1 second and Euler's method is", T_n, "C")

T_0 = 0 #repeated so that it doesn't combine with the first initial temperature value
for t in time: #2nd order RK method with k_1 and k_2
    k_1 = h*f(x, t)
    k_2 = h*f(x + 0.5*k_1, t + 0.5*h)
    T_0 += h*f(x + 0.5*h, t + 0.5*h*f(x, t))
print("The temperature using time steps of 1 second and the 2nd order RK method is", T_0, "C")

#these two for loops are repeated for the remaining time steps, h, just with different N-values
T_0 = 0
N = 1200
h = (b - a) / N
time = np.arange(a, b, h)
for t in time:
    T_n = T_0 + h*f(x, t)
    T_0 = T_n
    t += h
print("The temperature using time steps of 0.5 seconds and Euler's method is", T_n, "C")
T_0 = 0
for t in time:
    k_1 = h*f(x, t)
    k_2 = h*f(x + 0.5*k_1, t + 0.5*h)
    T_0 += h*f(x + 0.5*h, t + 0.5*h*f(x, t))
print("The temperature using time steps of 0.5 seconds and the 2nd order RK method is", T_0, "C")

T_0 = 0
N = 2400
h = (b - a) / N
time = np.arange(a, b, h)
for t in time:
    T_n = T_0 + h*f(x, t)
    T_0 = T_n
    t += h
print("The temperature using time steps of 0.25 seconds and Euler's method is", T_n, "C")
T_0 = 0
for t in time:
    k_1 = h*f(x, t)
    k_2 = h*f(x + 0.5*k_1, t + 0.5*h)
    T_0 += h*f(x + 0.5*h, t + 0.5*h*f(x, t))
print("The temperature using time steps of 0.25 seconds and the 2nd order RK method is", T_0, "C")

T_0 = 0
N = 4800
h = (b - a) / N
time = np.arange(a, b, h)
for t in time:
    T_n = T_0 + h*f(x, t)
    T_0 = T_n
    t += h
print("The temperature using time steps of 0.125 seconds and Euler's method is", T_n, "C")
T_0 = 0
for t in time:
    k_1 = h*f(x, t)
    k_2 = h*f(x + 0.5*k_1, t + 0.5*h)
    T_0 += h*f(x + 0.5*h, t + 0.5*h*f(x, t))
print("The temperature using time steps of 0.125 seconds and the 2nd order RK method is", T_0, "C")
print("The actual value after solving the DE is 24 C.")
print("From the results above, it appears that the most accurate results are those with time steps of 0.125 seconds as the values are just 4.9x10^-13 short of the actual value.")
print("Surprisingly, the values become less and less accurate as the time step decreases until the value of the time step reaches 0.125 seconds.")

#Question 2
import matplotlib.pyplot as plt
def f_x(x, y):
    return x - 0.5*x*y
def f_y(x, y):
    return 0.5*x*y - 2*y #defining given functions with given coefficients
x = 2
y = 2
a = 0
b = 30
N = 300
h = (b - a) / N #setting up increments of time, 0.1 was found to be appropriate
time = np.arange(a, b, h)
xpops = []
ypops = []
for t in time: #big for loop going through the 4th order RK method
    k_1x = h*f_x(x, y)
    k_2x = h*f_x(x + 0.5*k_1x, y)
    k_3x = h*f_x(x + 0.5*k_2x, y)
    k_4x = h*f_x(x + k_3x, y)
    x += (k_1x + 2*k_2x + 2*k_3x + k_4x) / 6
    xpops.append(x)
    k_1y = h*f_y(x, y)
    k_2y = h*f_y(x, y + 0.5*h)
    k_3y = h*f_y(x, y + 0.5*h)
    k_4y = h*f_y(x, y + h)
    y += (k_1y + 2*k_2y + 2*k_3y + k_4y) / 6
    ypops.append(y)
#Special shout out to Derod for helping me effectively combine the two equations under one by modifying the RK equations in terms of x and y and puttind both in a single for loop
plt.plot(time, xpops, color = 'blue')
plt.plot(time, ypops, color = 'red')
plt.title("Rabbit and Fox Populations Over Time")
plt.xlabel('Time')
plt.ylabel('Population')
plt.legend(["Rabbit Population", "Fox Population"], loc = 'upper right')
plt.savefig('homework8_graph_kolos-furman_zachary.png')


import requests

import urlutil

class Client:

    def __init__(self, url_prefix, auth=None):
        """
        :param url_prefix: Prefix of the URL to the Atlas API. Example: 'http://atlas.host:21000/api/atlas'
        :param auth: If authentication is used. For Kerberos HTTPKerberosAuth(principal="user@MY.REALM")
        """
        self.url_prefix = url_prefix # http://atlas.host.my.org:21000/api/atlas/
        self.auth=auth

    def _search(self, query):
        return requests.post(self.url_prefix + "/v2/search/basic", json=query, auth=self.auth)

    def _post_entity(self, entity):
        return requests.post(self.url_prefix + "/v2/entity", json=entity, auth=self.auth)

    def _create_qualifiedname_query(self, type_name, *values):
        """
        See _get_qualified_name for reason why implemented like this.
        :param type: type of expected entities
        :param values: Provide as many as you know of schema, table, column in that order.
        :return: Query to be sent to Atlas API.
        """
        query = {
            'typeName': type_name,
            'excludeDeletedEntities': True,
            'limit': 10000
        }
        entity_filter = {
            'condition': 'AND'
        }
        criterion = []
        n = 0
        for v in values:
            criteria = {
                'attributeName': 'qualifiedName',
                'operator': 'STARTSWITH' if n == 0 else 'CONTAINS',
                'attributeValue': v
            }
            n += 1
            criterion.append(criteria)
        if type_name == 'hive_table':
            # Ignore temporary tables.
            criterion.append({'operator': '=', 'attributeName': 'temporary', 'attributeValue': False})
        entity_filter['criterion'] = criterion
        query['entityFilters'] = entity_filter
        return query

    def _filter_entities_on_qualifiedName(self, entities, qualtified_name):
        return [e for e in entities if e['attributes']['qualifiedName'].startswith(qualtified_name)]

    def _create_qualifiedName_prefix(self, *values):
        return ".".join(values)+"."

    def _get_qualified_name(self, type, *values):
        """
        This method together with _create_qualifiedname_query are a bit weird implemented.
        Reason for not searching on the starts_with of the qualified name direct in Atlas API is that the
        search is shaky when searching on string with dot or underscore (not fully investigated). For instance
        searching on "hadoop_out_utv.vegas_title_d." gives a server error (500) but "foo.product_s." works fine.
        Therefore we do an "AND" search of all desired substrings of the qualified name, which gives us a super-set
        of our wanted result, but it is of a reasonable size. Then we do the final fully correct filtering on the
        client side.

        :param type: type of wanted entities
        :param values: Provide as many as you know of schema, table, column in that order.
        :return: Array of one dict per entity. Dict is on form:
            {u'status': u'ACTIVE',
             u'guid': u'1bbe630c-927e-43f5-846b-94513db1d625',
             u'typeName': u'hive_table',
             u'displayText': u'tablename',
             u'attributes': {
                u'owner': u'owner',
                u'qualifiedName': u'database.tablename@dhadoopname',
                u'name': u'tablename',
                u'description': None},
             u'classificationNames': [u'TAG1', u'TAG2']}
        """
        query = self._create_qualifiedname_query(type, *values)
        response = self._search(query)
        if response.status_code == 200:
            json_response = response.json()
            if json_response.has_key('entities'):
                return self._filter_entities_on_qualifiedName(json_response['entities'], self._create_qualifiedName_prefix(*values))
            else:
                return []
        else:
            raise AtlasError(response.content, response.status_code)

    def get_tables(self, db):
        """
        Get all active tables in a hive database.
        :param db: Name of database to get all tables for.
        :return: Array of one dict per table. Dict is on form:
            {u'status': u'ACTIVE',
             u'guid': u'1bbe630c-927e-43f5-846b-94513db1d625',
             u'typeName': u'hive_table',
             u'displayText': u'tablename',
             u'attributes': {
                u'owner': u'owner',
                u'qualifiedName': u'database.tablename@hadoopname',
                u'name': u'tablename',
                u'description': None},
             u'classificationNames': [u'TAG1', u'TAG2']}
        """
        return self._get_qualified_name("hive_table", db)

    def get_columns(self, db, table):
        """
        Get all columns for a table.
        :param db: Name of database
        :param table: Name of table
        :return: Array of one dict per column. Dict is on form:
            {u'status': u'ACTIVE',
             u'guid': u'7880d2a3-fec5-4b35-a91b-bea6c75f56b1',
             u'typeName': u'hive_column',
             u'displayText': u'columnname',
             u'attributes': {
                u'owner': u'user',
                u'qualifiedName': u'database.table.columnname@hadoopname',
                u'name': u'columnname',
                u'description': None},
            u'classificationNames': [u'TAG1', u'TAG2']}

        """
        return self._get_qualified_name("hive_column", db, table)

    def add_tags_on_guid(self, guid, tags):
        """
        Add Tags to an entity.
        :param guid: guid on the entity(e.g. column or table) to tag.
        :param tags: Array of strings(tags) to add.
        """
        tags_struct = []
        for t in tags:
            tags_struct.append({"typeName": t})

        response = requests.post(self.url_prefix + "/v2/entity/guid/" + guid + "/classifications", json=tags_struct, auth=self.auth)
        if response.status_code != 204:
            raise AtlasError(response.content, response.status_code)

    def delete_tags_on_guid(self, guid, tags):
        """
        Add Tags to an entity.
        :param guid: guid on the entity(e.g. column or table) to tag.
        :param tags: Array of strings(tags) to remove.
        """
        failed_tags = []
        for t in tags:
            response = requests.delete(self.url_prefix + "/v2/entity/guid/" + guid + "/classification/" + t, auth=self.auth)
            if response.status_code != 204:
                failed_tags.append(t)
        if len(failed_tags) != 0:
            raise AtlasError("Failed to delete tags " + repr(tags) + " on " + guid)

    def known_tags(self):
        """
        Get all tags know by Atlas
        :return: Array of one dict per tag. Dict is on form:
            {u'category': u'CLASSIFICATION', u'guid': u'5a76bab9-02ec-434d-bbee-1c7294f0cf31', u'name': u'PII'}
        """
        response = requests.get(self.url_prefix + "/v2/types/typedefs/headers", auth=self.auth)
        if response.status_code == 200:
            return [e for e in response.json() if e['category']=='CLASSIFICATION']
        else:
            raise AtlasError(response.content, response.status_code)

    def add_tag_definitions(self, tags):
        """
        Create new tag definitions in Atlas.
        :param tags: Array of strings that are new tags. Tags must not exist in Atlas. Use known_tags() to figure out.
        :return: None
        """
        post_data={"classificationDefs": list([{"name": t, "description":"", "superTypes":[], "attributeDefs":[]} for t in tags])}
        response=requests.post(self.url_prefix + "/v2/types/typedefs?type=classification", auth=self.auth, json=post_data)
        if response.status_code != 200:
            raise AtlasError(response.content, response.status_code)

    def get_tags_on_guid(self, guid):
        """
        Return tags on the entity guid in Atlas.
        :param guid: Guid to find tags for
        :return: List of tags.
        """
        response = requests.get(self.url_prefix + "/entities/" + guid, auth=self.auth)
        if response.status_code == 200:
            json_response = response.json()
            if json_response['definition'].has_key('traitNames'):
                return set(json_response['definition']['traitNames'])
            else:
                return set()
        else:
            raise AtlasError("Cannot look up guid {}.".format(guid, response.status_code))

    def add_hdfs_path(self, hdfs_path):
        """
        Post to http://atlas.hadoop.svenskaspel.se/api/atlas/v2/entity
        Post data {"entity":{"typeName":"hdfs_path","attributes":{"description":null,"name":"hdfs://svsprod/apps/hive/warehouse/hadoop_out_prod.db/country_d","owner":null,"qualifiedName":"hdfs://svsprod/apps/hive/warehouse/hadoop_out_prod.db/country_d","createTime":1536098400000,"fileSize":null,"group":null,"isFile":null,"isSymlink":null,"modifiedTime":1536098400000,"path":"hdfs://svsprod/apps/hive/warehouse/hadoop_out_prod.db/country_d","clusterName":null,"numberOfReplicas":null},"guid":-1},"referredEntities":{}}
        Response: {"mutatedEntities":{"CREATE":[{"typeName":"hdfs_path","attributes":{"qualifiedName":"hdfs://svsprod/apps/hive/warehouse/hadoop_out_prod.db/country_d"},"guid":"e20823a6-5521-4dc9-b2a7-b5a1d9babecd","status":"ACTIVE"}]},"guidAssignments":{"-1":"e20823a6-5521-4dc9-b2a7-b5a1d9babecd"}}
        :param hdfs_path: Full url to the file or directory hdfs://environment/my/path/
        :return: guid assigned
        """
        cluster_name = urlutil.get_host(hdfs_path)
        name = urlutil.get_path(hdfs_path)
        entity = {
            "entity": {
                "typeName": "hdfs_path",
                "attributes": {
                    "description": "Created/Updated by cobra-policytool.",
                    "name": name,
                    "qualifiedName": hdfs_path,
                    "path": hdfs_path,
                    "clusterName": cluster_name,
                },
                "guid": -1
            },
        }
        response = self._post_entity(entity)
        if response.status_code == 200:
            json_response = response.json()
            if json_response.has_key('guidAssignments'):
                return json_response['guidAssignments']['-1']
            else:
                AtlasError("Failed to add hdfs path {} content mismatch {}".format(hdfs_path, response.content))
        else:
            raise AtlasError(response.content, response.status_code)

class AtlasError(Exception):
    def __init__(self, message, http_code=None):
        self.message = message
        self.http_code = http_code

    def __str__(self):
        return "HTTP code: " + repr(self.http_code) + " Message: " + repr(self.message)


def backTrack(letters, comb, t, l, length, ind):
    global ans
    if length == t:
        count = 0
        for vowel in 'aeiou':
            if vowel in comb:
                count += 1
        if 1 <= count <= t-2:
            ans.append(comb)
        return
    for i in range(ind+1, l):
        backTrack(letters, comb+letters[i], t, l, length+1, i)

def main():
    global ans
    t, l = map(int, input().split())
    letters = list(map(str, input().split()))
    letters.sort()

    ans = []
    for ind in range(l-t+1):
        backTrack(letters, letters[ind], t, l, 1, ind)

    for a in ans:
        print(a)

main()


import unittest
import sys
import shutil
import os

try:
    import NXSCreateCompFS_test
except Exception:
    from . import NXSCreateCompFS_test

if sys.version_info > (3,):
    unicode = str
    long = int

# test fixture
class NXSCreateCompFS2Test(
        NXSCreateCompFS_test.NXSCreateCompFSTest):

    # constructor
    # \param methodName name of the test method
    def __init__(self, methodName):
        NXSCreateCompFS_test.NXSCreateCompFSTest.__init__(
            self, methodName)

        self.directory = "my_test_nxs"
        self._dircreated = False
        self.flags = " -d %s" % self.directory

    # test starter
    # \brief Common set up
    def setUp(self):
        NXSCreateCompFS_test.NXSCreateCompFSTest.setUp(self)
        if not os.path.isdir(self.directory):
            os.makedirs(self.directory)
            self._dircreated = True

    # test closer
    # \brief Common tear down
    def tearDown(self):
        NXSCreateCompFS_test.NXSCreateCompFSTest.tearDown(self)
        if self._dircreated:
            shutil.rmtree(self.directory)
            self._dircreated = False

    def test_comp_first_last_file_prefix(self):
        """ test nxsccreate comp file system
        """
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        args = [
            [
                ('nxscreate comp -v testmotor '
                 '-s  my_exp_mot  -l 3 %s -x my_ '
                 % self.flags).split(),
                ['my_testmotor01',
                 'my_testmotor02',
                 'my_testmotor03'],
                [
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n<definition>\n'
                    '  <group name="$var.entryname#\'scan\'$var.serialno"'
                    ' type="NXentry">\n'
                    '    <group name="instrument" type="NXinstrument">\n'
                    '      <group name="collection" type="NXcollection">\n'
                    '        <field name="my_exp_mot01" type="NX_FLOAT">'
                    '$datasources.my_exp_mot01<strategy mode="STEP"/>'
                    '</field>\n'
                    '      </group>\n'
                    '    </group>\n'
                    '  </group>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n<definition>\n'
                    '  <group name="$var.entryname#\'scan\'$var.serialno"'
                    ' type="NXentry">\n'
                    '    <group name="instrument" type="NXinstrument">\n'
                    '      <group name="collection" type="NXcollection">\n'
                    '        <field name="my_exp_mot02" type="NX_FLOAT">'
                    '$datasources.my_exp_mot02<strategy mode="STEP"/>'
                    '</field>\n'
                    '      </group>\n'
                    '    </group>\n'
                    '  </group>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n<definition>\n'
                    '  <group name="$var.entryname#\'scan\'$var.serialno"'
                    ' type="NXentry">\n'
                    '    <group name="instrument" type="NXinstrument">\n'
                    '      <group name="collection" type="NXcollection">\n'
                    '        <field name="my_exp_mot03" type="NX_FLOAT">'
                    '$datasources.my_exp_mot03<strategy mode="STEP"/>'
                    '</field>\n'
                    '      </group>\n'
                    '    </group>\n'
                    '  </group>\n'
                    '</definition>\n',
                ],
            ],
            [
                ('nxscreate comp -v testvm  --file-prefix test_exp_ '
                 ' -s  test_exp_mot -f 2 -l 3 %s' % self.flags).split(),
                ['test_exp_testvm02',
                 'test_exp_testvm03'],
                [
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n<definition>\n'
                    '  <group name="$var.entryname#\'scan\'$var.serialno"'
                    ' type="NXentry">\n'
                    '    <group name="instrument" type="NXinstrument">\n'
                    '      <group name="collection" type="NXcollection">\n'
                    '        <field name="test_exp_mot02" type="NX_FLOAT">'
                    '$datasources.test_exp_mot02<strategy mode="STEP"/>'
                    '</field>\n'
                    '      </group>\n'
                    '    </group>\n'
                    '  </group>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n<definition>\n'
                    '  <group name="$var.entryname#\'scan\'$var.serialno"'
                    ' type="NXentry">\n'
                    '    <group name="instrument" type="NXinstrument">\n'
                    '      <group name="collection" type="NXcollection">\n'
                    '        <field name="test_exp_mot03" type="NX_FLOAT">'
                    '$datasources.test_exp_mot03<strategy mode="STEP"/>'
                    '</field>\n'
                    '      </group>\n'
                    '    </group>\n'
                    '  </group>\n'
                    '</definition>\n',
                ],
            ],
        ]

        totest = []
        try:
            for arg in args:
                skip = False
                for cp in arg[1]:
                    if self.cpexists(cp):
                        skip = True
                if not skip:
                    for cp in arg[1]:
                        totest.append(cp)

                    vl, er = self.runtest(arg[0])

                    if er:
                        self.assertTrue(er.startswith("Info: "))
                    else:
                        self.assertEqual('', er)
                    self.assertTrue(vl)

                    for i, cp in enumerate(arg[1]):
                        xml = self.getcp(cp)
                        self.assertEqual(
                            arg[2][i], xml)

                    for cp in arg[1]:
                        self.deletecp(cp)
        finally:
            for cp in totest:
                if self.cpexists(cp):
                    self.deletecp(cp)

    def ttest_comp_file_prefix(self):
        """ test nxsccreate comp file system
        """
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        args = [
            [
                ('nxscreate comp --device test/motor/ -x my_ '
                 '--datasource-prefix exp_mot -a Data  --last 3 %s'
                 % self.flags).split(),
                ['my_exp_mot01',
                 'my_exp_mot02',
                 'my_exp_mot03'],
                [
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n'
                    '<definition>\n'
                    '  <datasource name="exp_mot01" type="TANGO">\n'
                    '    <device hostname="%s" member="attribute"'
                    ' name="test/motor/01" port="%s"/>\n'
                    '    <record name="Data"/>\n'
                    '  </datasource>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n'
                    '<definition>\n'
                    '  <datasource name="exp_mot02" type="TANGO">\n'
                    '    <device hostname="%s" member="attribute"'
                    ' name="test/motor/02" port="%s"/>\n'
                    '    <record name="Data"/>\n'
                    '  </datasource>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n'
                    '<definition>\n'
                    '  <datasource name="exp_mot03" type="TANGO">\n'
                    '    <device hostname="%s" member="attribute"'
                    ' name="test/motor/03" port="%s"/>\n'
                    '    <record name="Data"/>\n'
                    '  </datasource>\n'
                    '</definition>\n',
                ],
            ],
            [
                ('nxscreate comp --device test/vm/ --file-prefix test_exp_ '
                 ' --datasource-prefix mot -a Voltage '
                 '--first 2 --last 3 %s'
                 % self.flags).split(),
                ['test_exp_mot02',
                 'test_exp_mot03'],
                [
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n'
                    '<definition>\n'
                    '  <datasource name="mot02" type="TANGO">\n'
                    '    <device hostname="%s" member="attribute"'
                    ' name="test/vm/02" port="%s"/>\n'
                    '    <record name="Voltage"/>\n'
                    '  </datasource>\n'
                    '</definition>\n',
                    '<?xml version=\'1.0\' encoding=\'utf8\'?>\n'
                    '<definition>\n'
                    '  <datasource name="mot03" type="TANGO">\n'
                    '    <device hostname="%s" member="attribute"'
                    ' name="test/vm/03" port="%s"/>\n'
                    '    <record name="Voltage"/>\n'
                    '  </datasource>\n'
                    '</definition>\n',
                ],
            ],
        ]

        totest = []
        try:
            for arg in args:
                skip = False
                for ds in arg[1]:
                    if self.dsexists(ds):
                        skip = True
                if not skip:
                    for ds in arg[1]:
                        totest.append(ds)

                    vl, er = self.runtest(arg[0])

                    if er:
                        self.assertTrue(er.startswith("Info: "))
                    else:
                        self.assertEqual('', er)
                    self.assertTrue(vl)

                    for i, ds in enumerate(arg[1]):
                        xml = self.getds(ds)
                        self.assertEqual(
                            arg[2][i] % (self.host, self.port), xml)

                    for ds in arg[1]:
                        self.deleteds(ds)
        finally:
            for ds in totest:
                if self.dsexists(ds):
                    self.deleteds(ds)

if __name__ == '__main__':
    unittest.main()


import operator
import random
import string
import timeit
from io import StringIO

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

def main():
    str_list = [create_random_string(255) for i in range(1000)]
    functions = [
        (plus_concat, "plus_concat"),
        (join_concat, "join_concat"),
        (cstring_concat, "cstring_concat"),
    ]
    duration_list = {}
    for func, name in functions:
        durations = timeit.repeat(lambda: func(str_list), repeat=5000, number=3)
        duration_list[name] = list(np.array(durations) * 1000)
        print(
            "{func:<20}: "
            "min: {min:0.3f}s, mean: {mean:0.3f}s, max: {max:0.3f}s".format(
                func=name,
                min=min(durations),
                mean=np.mean(durations),
                max=max(durations),
            )
        )
        create_boxplot(duration_list)

def create_boxplot(duration_list, showfliers=False):
    plt.figure(num=None, figsize=(8, 4), dpi=300, facecolor="w", edgecolor="k")
    sns.set(style="whitegrid")
    sorted_keys, sorted_vals = zip(
        *sorted(duration_list.items(), key=operator.itemgetter(1))
    )
    flierprops = dict(markerfacecolor="0.75", markersize=1, linestyle="none")
    ax = sns.boxplot(
        data=sorted_vals,
        width=0.3,
        orient="h",
        flierprops=flierprops,
        showfliers=showfliers,
    )
    ax.set(xlabel="Time in ms", ylabel="")
    plt.yticks(plt.yticks()[0], sorted_keys)
    plt.tight_layout()
    plt.savefig("output.png")

def create_random_string(N):
    return "".join(
        random.choice(string.ascii_uppercase + string.digits) for _ in range(N)
    )

def plus_concat(str_list):
    total = ""
    for str_ in str_list:
        total += str_
    return total

def join_concat(str_list):
    new_list = []
    for str_ in str_list:
        new_list.append(str_)
    return "".join(new_list)

def cstring_concat(str_list):
    buf = StringIO()
    for str_ in str_list:
        buf.write(str_)
    return buf.getvalue()

if __name__ == "__main__":
    main()


import numpy as np
import math
from ccblade import *
from wakeLoadsFuncs import *
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import time as Time
import fast_calc_aep
import scipy.signal
from numpy import fabs as fabs
import damage_calc

"""preprocess"""
def extract_damage(filename_free,filename_close,filename_far):

    lines = np.loadtxt(filename_free,skiprows=8)
    atm_free = lines[:,12]

    lines = np.loadtxt(filename_close,skiprows=8)
    atm_close = lines[:,12]

    lines = np.loadtxt(filename_far,skiprows=8)
    atm_far = lines[:,12]

    damage_free = get_damage(atm_free,1.0,fos=3)
    damage_close = get_damage(atm_close,1.0,fos=3)
    damage_far = get_damage(atm_far,1.0,fos=3)

    return damage_free,damage_close,damage_far

"""preprocess"""
def get_damage(moments,freq,fos=3):

    """
    calculate the damage of a turbine from a single direction

    inputs:
    moments:        the moment history
    freq:           the probability of these moments
    fos:            factor of safety

    outputs:
    damage:         fatigue damage

    """

    N = len(moments)
    t = np.linspace(0.,600.,N)

    d = 0.
    R = 0.5 #root cylinder radius
    I = 0.25*np.pi*R**4
    I = 0.25*np.pi*(R**4-(R-0.08)**4)

    #go from moments to stresses
    sigma = moments*R/I

    #find the peak stresses
    pp = scipy.signal.find_peaks(sigma)[0]
    pn = scipy.signal.find_peaks(-sigma)[0]
    p = np.append(0,pp)
    p = np.append(p,pn)
    p = np.append(p,len(sigma)-1)
    p = np.sort(p)
    peaks = np.zeros(len(p))
    time = np.zeros(len(p))
    for i in range(len(p)):
        peaks[i] = sigma[p[i]]
        time[i] = t[p[i]]

    #rainflow counting
    array = rainflow(peaks)

    alternate = array[0,:]/2.
    mean = array[1,:]
    count = array[3,:]

    # Goodman correction
    su = 345000.
    mar = alternate/(1.-mean/su)

    npts = len(mar)

    #damage calculations
    for i in range(npts):
        # Nfail = (su/mar[i])**m
        Nfail = 10.**((-mar[i]/su+1.)/0.1)
        mult = 25.*365.*24.*6.*freq
        d += count[i]*mult/Nfail

    return d*fos

"""preprocess"""
def rainflow(array_ext,flm=0,l_ult=1e16,uc_mult=0.5):

    # """ Rainflow counting of a signal's turning points with Goodman correction
    #
    #     Args:
    #         array_ext (numpy.ndarray): array of turning points
    #
    #     Keyword Args:
    #         flm (float): fixed-load mean [opt, default=0]
    #         l_ult (float): ultimate load [opt, default=1e16]
    #         uc_mult (float): partial-load scaling [opt, default=0.5]
    #
    #     Returns:
    #         array_out (numpy.ndarray): (5 x n_cycle) array of rainflow values:
    #                                     1) load range
    #                                     2) range mean
    #                                     3) Goodman-adjusted range
    #                                     4) cycle count
    #                                     5) Goodman-adjusted range with flm = 0
    #
    # """

    flmargin = l_ult - fabs(flm)            # fixed load margin
    tot_num = array_ext.size                # total size of input array
    array_out = np.zeros((5, tot_num-1))    # initialize output array

    pr = 0                                  # index of input array
    po = 0                                  # index of output array
    j = -1                                  # index of temporary array "a"
    a  = np.empty(array_ext.shape)          # temporary array for algorithm

    # loop through each turning point stored in input array
    for i in range(tot_num):

        j += 1                  # increment "a" counter
        a[j] = array_ext[pr]    # put turning point into temporary array
        pr += 1                 # increment input array pointer

        while ((j >= 2) & (fabs( a[j-1] - a[j-2] ) <= \
                fabs( a[j] - a[j-1]) ) ):
            lrange = fabs( a[j-1] - a[j-2] )

            # partial range
            if j == 2:
                mean      = ( a[0] + a[1] ) / 2.
                adj_range = lrange * flmargin / ( l_ult - fabs(mean) )
                adj_zero_mean_range = lrange * l_ult / ( l_ult - fabs(mean) )
                a[0]=a[1]
                a[1]=a[2]
                j=1
                if (lrange > 0):
                    array_out[0,po] = lrange
                    array_out[1,po] = mean
                    array_out[2,po] = adj_range
                    array_out[3,po] = uc_mult
                    array_out[4,po] = adj_zero_mean_range
                    po += 1

            # full range
            else:
                mean      = ( a[j-1] + a[j-2] ) / 2.
                adj_range = lrange * flmargin / ( l_ult - fabs(mean) )
                adj_zero_mean_range = lrange * l_ult / ( l_ult - fabs(mean) )
                a[j-2]=a[j]
                j=j-2
                if (lrange > 0):
                    array_out[0,po] = lrange
                    array_out[1,po] = mean
                    array_out[2,po] = adj_range
                    array_out[3,po] = 1.00
                    array_out[4,po] = adj_zero_mean_range
                    po += 1

    # partial range
    for i in range(j):
        lrange    = fabs( a[i] - a[i+1] );
        mean      = ( a[i] + a[i+1] ) / 2.
        adj_range = lrange * flmargin / ( l_ult - fabs(mean) )
        adj_zero_mean_range = lrange * l_ult / ( l_ult - fabs(mean) )
        if (lrange > 0):
            array_out[0,po] = lrange
            array_out[1,po] = mean
            array_out[2,po] = adj_range
            array_out[3,po] = uc_mult
            array_out[4,po] = adj_zero_mean_range
            po += 1

    # get rid of unused entries
    array_out = array_out[:,:po]

    return array_out

def farm_damage(turbineX,turbineY,windDirections,windFrequencies,damage_free,damage_close,damage_far,wind_speed=8,TI=0.11,rotor_diameter=126.4):
    """
    calculate the damage of each turbine in the farm for every wind direction

    inputs:
    turbineX:       x locations of the turbines (in the wind frame)
    turbineY:       y locations of the turbines (in the wind frame)
    windDirections: the wind directions
    windFrequencies: the associated probabilities of the wind directions
    atm_free:       the freestream atmospheric loads as a function of time
    atm_close:      the 4D downstream fully waked atmospheric loads as a function of time
    atm_far:        the 10D downstream fully waked atmospheric loads as a function of time

    outputs:
    damage:         fatigue damage of the farm

    """

    damage = np.zeros_like(turbineX)
    nDirections = len(windDirections)
    nTurbines = len(turbineX)

    for j in range(nDirections):
            turbineXw, turbineYw = fast_calc_aep.windframe(windDirections[j], turbineX, turbineY)
            ind = np.argsort(turbineXw)
            sortedX = np.zeros(nTurbines)
            sortedY = np.zeros(nTurbines)
            for k in range(nTurbines):
                sortedX[k] = turbineXw[ind[k]]
                sortedY[k] = turbineYw[ind[k]]
            # _, sigma = get_speeds(turbineXw, turbineYw, np.array([0.]), np.array([0.]), np.array([0.]), wind_speed, TI=TI)
            _, sigma = get_speeds(sortedX, sortedY, np.array([0.]), np.array([0.]), np.array([0.]), wind_speed, TI=TI)
            wake_radius = sigma*2.
            for i in range(nTurbines):
                    # damage[i] += damage_calc.combine_damage(turbineXw,turbineYw,i,damage_free,damage_close,damage_far,rotor_diameter,wake_radius)*windFrequencies[j]
                    damage[i] += damage_calc.combine_damage(sortedX,sortedY,np.where(ind==i)[0][0],damage_free,damage_close,damage_far,rotor_diameter,wake_radius)*windFrequencies[j]
    return damage

if __name__ == '__main__':

    # # T11
    # filename_free = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C680_W8_T11.0_P0.0_m2D_L0/Model.out'
    # filename_close = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C653_W8_T11.0_P0.0_4D_L0/Model.out'
    # filename_far = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C671_W8_T11.0_P0.0_10D_L0/Model.out'
    # # T5.6
    # # filename_free = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C464_W8_T5.6_P0.0_m2D_L0/Model.out'
    # # filename_close = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C437_W8_T5.6_P0.0_4D_L0/Model.out'
    # # filename_far = '/Users/ningrsrch/Dropbox/Projects/waked-loads/BYU/BYU/C455_W8_T5.6_P0.0_10D_L0/Model.out'
    # #
    # N=24001
    # #
    # TI = 0.11
    # print 'setup'
    # s = Time.time()
    # atm_free,atm_close,atm_far,Omega_free,Omega_waked,free_speed,waked_speed = setup_atm(filename_free,filename_close,filename_far,TI=TI,N=N)
    # print Time.time()-s
    #
    # # sep = np.array([4.,7.,10.])
    # # offset = np.array([-1.,-0.75,-0.5,-0.25,0.,0.25,0.5,0.75,1.])
    #
    # sep = 4.
    # offset = 0.5
    # turbineX = np.array([0.,126.4])*sep
    # turbineY = np.array([0.,126.4])*offset
    # windDirections = np.array([270.])
    # windFrequencies = np.array([1.])
    # s = Time.time()
    # damage = farm_damage(turbineX,turbineY,windDirections,windFrequencies,atm_free,atm_close,atm_far,Omega_free,Omega_waked,free_speed,waked_speed,TI=TI,N=N)[1]
    # print "time to run: ", (Time.time()-s)/2.
    # print damage
    # # super4 = np.array([0.6465743 , 0.78860154, 0.90094437, 1.00180077, 1.0227498 , 1.00450329, 0.89131835, 0.77691189, 0.6600066 ])

    wake_radius = 40.
    rotor_diameter = 100.
    dy = np.linspace(-200.,200.,10000)
    w = np.zeros_like(dy)
    for i in range(10000):
        w[i] = rotor_amount_waked(dy[i],wake_radius,rotor_diameter)
    plt.plot(dy,w)
    plt.show()


from .. import logger
import math
import os
import shutil
import time
from typing import List
from .. import configuration, pyc_checker, utils
from ..pyc_checker.pyc import PycHandler
from ..py_package import PackageStruct, PackageDescription
from PyInstaller.utils.cliutils.archive_viewer import ArchiveViewer

import sys
from PyInstaller.archive.readers import CArchiveReader, ZlibArchiveReader, ArchiveReadError
from sgtpyutils.logger import logger
from sgtpyutils.database.filebase_database import Database

def get_archive(filename: str):
    viewer = ArchiveViewer(filename=filename,
                           interactive_mode=False,
                           recursive_mode=False,
                           brief_mode=True)
    arch = None
    msg = f'fail while extract archive file:{filename}.'
    try:
        arch = viewer._open_toplevel_archive(filename)
    except ArchiveReadError as ex:
        logger.error(
            f'{msg}make sure your file is a valid archive-python file instead of orgin-exe or others.{ex}')
        sys.exit(-10010)
    except Exception as ex:
        logger.error(f'{msg}{ex}')
        sys.exit(-10011)
    return arch
    archive_name = os.path.basename(viewer.filename)
    viewer.stack.append((archive_name, arch))
    archive_count = 0
    toc = {}
    while viewer.stack:
        archive_name, archive = viewer.stack.pop()
        archive_count += 1
        toc[archive_name] = archive
        if not viewer.recursive_mode:
            continue

        # Scan for embedded archives
        if isinstance(archive, CArchiveReader):
            for name, (*_, typecode) in archive.toc.items():
                if typecode == 'z':
                    try:
                        embedded_archive = archive.open_embedded_archive(name)
                    except Exception as e:
                        logger.warn(
                            f"Could not open embedded archive {name!r}: {e}")
                    viewer.stack.append((name, embedded_archive))
    setattr(viewer, 'toc', toc)
    return viewer

class FileTypeFlag():
    FLAG_NOTFILE = 'nofile'
    FLAG_ARCH = 'arch'
    FLAG_PYC = 'pyc'
    FLAG_TYPE_UNKNOWN = 'TYPE_UNKNOWN'

class FileType():
    FILE_EXE = 'exe'
    FILE_PE = 'pe'
    FILE_ELF = 'elf'
    FILE_PYZ = 'pyz'
    FILE_PYC = 'pyc'
    FILE_NOTFILE = 'notfile'
    FILE_UNKNOWN = 'unknown'

class CommonDump():

    @property
    def file_struct_pyc(self):
        if not self._file_struct_pyc:
            self._file_struct_pyc = PackageStruct()
        return self._file_struct_pyc

    def __init__(self):
        self.total_handled_count = 0
        self._file_struct_pyc: PackageStruct = None
        self.action_dispatch = {
            FileTypeFlag.FLAG_NOTFILE: self.handle_nofile,
            FileTypeFlag.FLAG_ARCH: self.handle_arch_file,
            FileTypeFlag.FLAG_PYC: self.handle_pyc_file,
            FileTypeFlag.FLAG_TYPE_UNKNOWN: self.handle_unknown,
        }
        self.action_map = {
            FileType.FILE_EXE: FileTypeFlag.FLAG_ARCH,
            FileType.FILE_PE: FileTypeFlag.FLAG_ARCH,
            FileType.FILE_ELF: FileTypeFlag.FLAG_ARCH,
            FileType.FILE_PYZ: FileTypeFlag.FLAG_ARCH,
            FileType.FILE_PYC: FileTypeFlag.FLAG_PYC,
            FileType.FILE_NOTFILE: FileTypeFlag.FLAG_NOTFILE,
            FileType.FILE_UNKNOWN: FileTypeFlag.FLAG_TYPE_UNKNOWN
        }

    def handle_unknown(self, target_file: str):
        return f'unkonwn file-type:{target_file}'

    def handle_nofile(self, target_file: str):
        logger.error(f'is an invalid file name! {target_file}')
        return -1

    @staticmethod
    def clear_history(target: str):
        '''remove if using default path'''
        if not os.path.exists(target):
            return
        if configuration.thread_output_directory_customed:
            return
        logger.info(f'removing output_directory:{target}')
        shutil.rmtree(target)

    @staticmethod
    def build_output_dir(clear=True):
        output_dir = configuration.thread_output_directory
        if clear:
            CommonDump.clear_history(output_dir)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        return output_dir

    def handle_pyc_file(self, target_file: str):
        output_dir = self.build_output_dir()
        new_file = f'{output_dir}{os.path.sep}{target_file}'
        shutil.copy(target_file, new_file)
        PackageStruct.decompile_pyc(new_file)
        return f'pyc file handled:{new_file}'

    def handle_arch_file(self, target_file: str):
        output_dir = self.build_output_dir()
        arch = get_archive(filename=target_file)

        self.extract_arch(arch, output_dir)
        self.file_struct_pyc.progress_check()
        return f'{self.total_handled_count} arch file(s) handled.'

    def load_plugins(self, plugin):
        self.any_invalid_plugin = False
        p_prefix = 'plugin_decompiler_enable_'

        def filter_plugin(p):
            n = f'{p_prefix}{p}'
            if n not in configuration.__dict__:
                self.any_invalid_plugin = True
                logger.error(f'no plugin named {p}')
                return None
            return [p, n]
        plugin = [filter_plugin(x) for x in plugin]
        plugin_paths = [x[1] for x in plugin if x]
        if self.any_invalid_plugin:
            keys = list(configuration.__dict__)
            all_valid_plugins = filter(lambda x: x.startswith(p_prefix), keys)
            all_valid_plugins = [x.replace(p_prefix, '')
                                 for x in all_valid_plugins]
            logger.debug(f'all valid plugins:{all_valid_plugins}')
            time.sleep(5)
        logger.debug(f'plugins loaded with {[x[0] for x in plugin if x]}')
        [setattr(configuration, x, True) for x in plugin_paths]

        if configuration.plugin_decompiler_enable_uncompyle6 and configuration.decompile_file == None:
            logger.error(
                'attention! when use uncompyle6 , you should use --decompile_file specified which file to decompile for faster task.')
            time.sleep(3)

    def statistics_status(self, is_end: bool = False):
        from sgtpyutils import timer
        if is_end:
            t = self.global_start_time
            logger.info(
                f'completed,cost {math.ceil(t.spent * 1000)}ms with result:{self.result}')
            return
        self.global_start_time = timer.create_timer()

    def main(self, target_file: str, output_directory: str, thread: int = 0, timeout: int = 10, target_file_type: str = None, session_timeout: int = 120, plugin: List = [], decompile_file: List = None, struct_headers: str = None, **args):
        self.statistics_status()
        if struct_headers:
            if isinstance(struct_headers, List):
                struct_headers = ''.join(struct_headers)
            pyc_header = bytes.fromhex(struct_headers.replace(' ', ''))
            pyc_checker.default_pyc = PycHandler(pyc_header)

        configuration.thread_count = thread  # thread
        configuration.thread_timeout = timeout

        if output_directory:
            configuration.thread_output_directory_customed = True
        else:
            output_directory = utils.paths.get_random_path('output')
        configuration.thread_output_directory = output_directory

        configuration.progress_session_timeout = session_timeout
        configuration.decompile_file = dict(
            zip(decompile_file, [True for x in decompile_file])) if decompile_file else None
        self.load_plugins(plugin)
        if not target_file:
            logger.error('target_file is required')
            return
        logger.debug(f'target file input:{target_file}\nto:{output_directory}')
        if not os.path.exists(target_file):
            logger.error('target_file not exist')

        file_type = target_file_type or self.get_filetype(target_file)
        dispatch_to = self.action_map.get(file_type, None)

        logger.debug(f'start dump target file. type:{dispatch_to}')
        if dispatch_to != FileTypeFlag.FLAG_NOTFILE:
            abs_path = os.path.abspath(target_file)
            target_dir = os.path.dirname(abs_path)
            os.chdir(target_dir)
            # convert absolute path to relative path
            target_file = os.path.basename(target_file)
            time.sleep(3)
        self.result = self.action_dispatch[dispatch_to](target_file)

        self.statistics_status(True)
        return self.result

    @staticmethod
    def get_filetype(filepath: str):
        if not isinstance(filepath, str) or not os.path.isfile(filepath):
            return FileType.FILE_NOTFILE
        header_data: bytes = None
        with open(filepath, 'rb') as f:
            header_data = f.read(0x20)
        if header_data[0:3] == b'MZ\x90':
            return FileType.FILE_EXE
        if header_data[0:4] == b'PYZ\x00':
            return FileType.FILE_PYZ
        if header_data[0:4] == b'\x7fELF':
            return FileType.FILE_ELF
        if header_data.find(bytes.fromhex('e3' + '0' * 14)) > -1:
            return FileType.FILE_PYC
        return FileType.FILE_UNKNOWN

    def extract_arch(self, arch, current_directory: str):
        if not os.path.exists(current_directory):
            os.makedirs(current_directory)
        children_package = arch.toc
        # if isinstance(children_package, dict):
        #     children_package = list(filter(
        #         lambda x: not x.startswith('_'), list(children_package)))
        # else:
        #     children_package = children_package.data
        # children_package = list(children_package)

        children_package = list(children_package.items())
        children_package = list(
            filter(lambda x: not x[0].startswith('_'), children_package))
        self.total_handled_count += len(children_package)
        for package in children_package:
            p = PackageDescription(package, arch, current_directory)
            self.file_struct_pyc.add(p)


from flask import Blueprint, jsonify, request
from db.connection import db
from helper import token_required, get_authorized_users, get_app_name
from helper import email_parameter_required, apikey_parameter_required
from regex import EMAIL_REGEX
import re

authorization = Blueprint("authorization", __name__)

def get_app_access_request(email):

	app_access_request = db.users_accounts.aggregate([
		{
			"$match":{
				"email":email
			}
		},

		{
			"$project":{
				"_id":0,
				"name":0,
				"identity_document":0,
				"email":0,
				"cellphone_id":0
			}
		}
	])

	app_access_request = list(app_access_request)
	return app_access_request[0]["groups"]

@authorization.route("/authorization_user", methods=["PUT"])
@token_required
@email_parameter_required
@apikey_parameter_required
def authorization_user(current_user):
	email = request.args.get("email")
	api_key = request.args.get('api_key')

	if email in get_authorized_users(api_key):
		msg = f"""El usuario {email} ya ha sido autorizado para accesar a la aplicación
		{get_app_name(api_key)}"""
		return jsonify({"msg":msg})

	if api_key not in get_app_access_request(email):
		msg = f"""El usuario {email} no ha solicitado accesar a la aplicación
		{get_app_name(api_key)}"""
		return jsonify({"msg":msg})

	db.developers.update_one(
		{"api_key":api_key},
		{"$push": {"user_group":email}}
	)

	app_name = get_app_name(api_key)
	msg = f"El usuario {email} ha sido autorizado para usar la aplicación {app_name}"
	return jsonify({"msg":msg})

@authorization.route("/reject_user", methods=["PUT"])
@token_required
@email_parameter_required
@apikey_parameter_required
def reject(current_user):

	email = request.args.get("email")
	api_key = request.args.get('api_key')

	if api_key not in get_app_access_request(email):
		msg = f"""El usuario {email} no ha solicitado accesar a la aplicación
		{get_app_name(api_key)}"""
		return jsonify({"msg":msg})

	db.developers.update_one(
		{"api_key":api_key},
		{"$pull": {"user_group":email}}
	)

	db.users_accounts.update_one(
		{"email":request.args.get("email")},
		{"$pull": {"groups":api_key}}
	)

	app_name = get_app_name(api_key)
	msg = f"El usuario {email} no ha sido autorizado para usar la aplicación {app_name}"
	return jsonify({"msg":msg})


# -*- coding: utf-8 -*-
import logging
import pandas as pd
import torch
from torchvision import transforms
from torch.utils.data import Dataset
import cv2
from PIL import Image
import os
import numpy as np
import joblib
import json
import random

class FundusDataset(Dataset):
    def __init__(self, set='train', labels=False, size='full',
                normalized=False, resolution=224, augmented=True):
        self.set = set
        self.labels = labels
        self.size = size
        self.norm = normalized
        self.res = resolution
        self.aug = augmented

        if self.norm:
            self.transform = transforms.Compose([
                                transforms.ToTensor(),
                                transforms.Normalize(
                                    mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225]
                                )
                            ])

        else:
            self.transform = transforms.Compose([
                                transforms.ToTensor()
                            ])

        self.data_dir = f'/nfs_home/projects/shared_projects/eye_imaging/data/'
        if self.res == 299:
            if self.aug:
                self.data_dir += '2022_eyes_biomarkers/processed_aug_299x299_final'
            else:
                self.data_dir += '2022_eyes_biomarkers/processed_299x299_final'
        elif self.res == 224:
            if self.aug:
                self.data_dir += '2022_eyes_biomarkers/processed_aug_224x224_final'
            else:
                self.data_dir += '2022_eyes_biomarkers/processed_224x224_final'
                #self.data_dir += '2022_eyes_biomarkers/processed_aug_noflip'

        self.images_path = self.data_dir#+'/'+self.set

        self.images = os.listdir(self.images_path)

        # using only participants with both eyes scans
        from collections import Counter
        counter = Counter([str(img.split('_')[0]) for img in self.images])
        self.valid_part_idx = []
        for k in list(counter.keys()):
            if counter[str(k)] == (10 if self.aug else 2):
                self.valid_part_idx.append(str(k))

        if self.size != 'full':
            self.valid_part_idx = random.sample(self.valid_part_idx, self.size)

        self.images = [img for img in self.images
                            if str(img.split('_')[0]) in self.valid_part_idx]

        if self.set == 'inference':
            if self.aug:
                self.images = [im for im in self.images if im.endswith('_0')]

        self.valid_part_idx = np.unique(
                            [str(img.split('_')[0]) for img in self.images]
        )

        self.gen_mapping()

        #self.rgb_dir = f'/nfs_home/projects/shared_projects/eye_imaging/data/2022_oct_fundus/processed/fundus'

    def gen_mapping(self):
        self.item_to_class_map = {}
        for item_idx, item in enumerate(self.valid_part_idx):
            self.item_to_class_map[str(int(item))] = item_idx

        self.class_to_items_map = {}
        self.img_to_idx_map = {}
        for item_idx, item in enumerate(self.images):
            self.img_to_idx_map[item] = item_idx
            item_class = self.item_to_class_map[item.split('_')[0]]
            if item_class not in list(self.class_to_items_map.keys()):
                self.class_to_items_map[item_class] = [item]
            else:
                self.class_to_items_map[item_class].append(item)

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        if isinstance(idx, int):
            return self.fetchitem(idx)
        elif isinstance(idx, tuple):
            return (self.fetchitem(idx[0]), self.fetchitem(idx[1]))

    def fetchitem(self, idx):
        img_filename = self.images[idx]
        img = joblib.load('/'.join([self.images_path, img_filename]))#/255
        #img = img.crop((0, 35, 0+128, 35+128))
        #img.save(f'./data/test_{img_filename}.png')
        img = self.transform(img).float()

        part_id = str(int(img_filename.split('_')[0]))
        target = self.item_to_class_map[part_id]

        return idx, img, target#, part_id, img_filename.split('_')[1]

    def classes_count(self):
        return len(list(self.item_to_class_map.values()))

    def get_sorted_dataset(self):
        return self.images

    def get_item_to_class_map(self):
        return self.item_to_class_map

    def get_class_to_item_map(self):
        return self.class_to_items_map

    def get_img_to_idx_map(self):
        return self.img_to_idx_map

    def get_part_from_idx(self, idx):
        return self.images[idx]


import re

fileinput = open('Tax Invoice Multiple Extract.txt', 'r')
fileoutput = open('Tax Invoice Consolidated Report.txt', 'w')

totalInvoice = 0

for line in fileinput:
	xval = re.findall('^Attention: (\S+\s\S+)',line)
	if xval:
		xvalSTR = ''.join(xval)
		print(xvalSTR)

	xval = re.findall('^Invoice Date: (\S+\s\S+\s\S+)',line)
	if xval:
		xvalSTR = ''.join(xval)
		print(xvalSTR)

	xval = re.findall('^Invoice Number: (\S+)',line)
	if xval:
		xvalSTR = ''.join(xval)
		print(xvalSTR)

	xval = re.findall('^Total AUD: (\S+)',line)
	if xval:
		xvalSTR = ''.join(xval)
		print(xvalSTR)
		print('-------------------')
		totalInvoice = totalInvoice + float(xvalSTR)

print('Total Invoice Amount : ${:0,.2f}'.format(totalInvoice))
fileinput.close()
fileoutput.close()


with open("puzzle_input.txt") as f:
    data = list(f)

all_bags = {}

for line in data:

    main_bag, others = line.strip().split(" bags contain ")
    inside_main_bag = []
    bags_in_main_bag = others.split(", ")
    for each_bag in bags_in_main_bag:
        description = each_bag.split(" bag")[0]
        number_of_bags = description[0]
        type_of_bags = description[2:]
        if number_of_bags != "n":  # no other
            inside_main_bag.append((type_of_bags, int(number_of_bags)))
    all_bags[main_bag] = inside_main_bag

bags_count_dict = {}

def count_bags_in(bag):
    bags_count = 0
    for type_of_bags, number_of_bags in all_bags[bag]:
        bags_count += number_of_bags + number_of_bags * count_bags_in(
            type_of_bags
        )
    return bags_count

print(count_bags_in("shiny gold"))


from django.test import TestCase, Client
from django.urls import path
from django.contrib.auth.models import User
from json import loads
from googletrans import Translator
from .models import gTranslation
from .templatetags.gtranslate import gtranslate
from .urls import urlpatterns
from .urls_auth import urlpatterns as urlpatterns_auth
from .views import gTranslate, gTranslateAuth

# Create your tests here.

class TranslationStorage_TestCase(TestCase):
    """ requires makemigration and migrate before testing """
    src = 'en'
    dest = 'fr'
    text = 'something to translate'
    translation = "quelque chose à traduire"

    def test_translation_stored_and_returned(self):
        """ Translation stored and returned from the modal correctly """
        translation = gtranslate(self.text, self.dest, self.src, False)
        gTranslation.objects.create(
            text=self.text,
            language=self.dest,
            translation=translation
        )
        self.assertEquals(gTranslation.objects.get(
            text=self.text, language=self.dest
        ).translation, translation)

    def test_automatic_translation_storing(self):
        """ Test automatic translation storage """
        translation = gtranslate(
            self.text, self.dest, self.src, cache=True
        )
        self.assertEqual(
            gTranslation.objects.get(
                text=self.text,
                language=self.dest).translation,
                translation
        )

    def test_translation_correctness(self):
        """
        Test if translation received from googleTrans is correct
        on gtranslate.views.gTranslate no authorized
        """
        translation = gtranslate(
            self.text, self.dest, self.src, False
        )
        self.assertEqual(translation, self.translation)

    def test_dynamic_translation(self):
        """ Test dynamic translation url from urls.py.urlpatterns
        /<src>/<dest>/<text>
        """
        translation = loads(
            Client().get(
                "/gtran/%s/%s/%s" % (
                    self.src,
                    self.dest,
                    self.text
                )
            ).content
        )['translation']
        self.assertEqual(translation, self.translation)

    def test_dynamic_translation_auth(self):
        """ Test is_authorized dynamic translation url from
        urls_auth.py.urlpatterns
        /<src>/<dest>/<text>
        """
        translation = Client().get(
                "/gtran_auth/%s/%s/%s" % (
                    self.src,
                    self.dest,
                    self.text
                )
            ).status_code
        self.assertEqual(translation, 302)

    def test_if_urls_loaded(self):
        """ test if urls of the view is on urls and urls_auth """
        self.assertEqual(
            path(
                '<src>/<dest>/<text>',
                gTranslate
            ).callback, urlpatterns[0].callback
        )
        self.assertEqual(
            path(
                '<src>/<dest>/<text>',
                gTranslateAuth
            ).callback, urlpatterns_auth[0].callback
        )

    def test_main_gtranslate_func(self):
        """ to test function which previous tests based on """
        T = Translator()
        translation = T.translate(self.text, self.dest, self.src).text
        self.assertEqual(
            translation, gtranslate(
                text=self.text, dest=self.dest, src=self.src
            )
        )

    def test_false_str_input_gtranslate_func(self):
        """ test raising errors of false str input """
        try:
            gtranslate(text=False, dest=self.dest, src=self.src)
        except:
            assert True is True
        try:
            gtranslate(text=self.text, dest=self.dest, src='nothing')
        except:
            assert True is True
        try:
            gtranslate(text=self.text, dest='nothing', src=self.src)
        except:
            assert True is True

    def test_getting_translation_from_cache_return(self):
        """ test returning already cached translation from modal """
        translation = gtranslate(
            text=self.text, dest=self.dest,
            src=self.src, cache=True
        )
        cached_translation = gtranslate(
            text=self.text, dest=self.dest,
            src=self.src, cache=True
        )
        assert translation == cached_translation


from lemur.plugins.base import Plugin

class SourcePlugin(Plugin):
    type = 'source'

    default_options = [
        {
            'name': 'pollRate',
            'type': 'int',
            'required': False,
            'helpMessage': 'Rate in seconds to poll source for new information.',
            'default': '60',
        }
    ]

    def get_certificates(self):
        raise NotImplemented

    @property
    def options(self):
        return list(self.default_options) + self.additional_options


"""empty message

Revision ID: 35bedde9dfb3
Revises: 4132ca0853a9
Create Date: 2017-05-12 21:37:45.213032

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '35bedde9dfb3'
down_revision = '4132ca0853a9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('articles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=64), nullable=True),
    sa.Column('content', sa.Text(), nullable=True),
    sa.Column('status', sa.String(length=1), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('create_date', sa.DateTime(), nullable=True),
    sa.Column('update_date', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_articles_create_date'), 'articles', ['create_date'], unique=False)
    op.create_index(op.f('ix_articles_update_date'), 'articles', ['update_date'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_articles_update_date'), table_name='articles')
    op.drop_index(op.f('ix_articles_create_date'), table_name='articles')
    op.drop_table('articles')
    # ### end Alembic commands ###


from chatsenselib.variables import *

def getSensorValueAux(sensor):
    if (sensor['type'] == "sensor/temperature"):
        if (sensor['location'] == "sauna"):
            return(110);
        else:
            return(21);
    elif (sensor['type'] == "sensor/humidity"):
        return(80);
    else:
        return(5);

def getSensorValue(type,location):
    actualType = "sensor/" + type;
    for sensor in mSensorDB.all():
        if (sensor['type'] == actualType and sensor['location'] == location):
            return(getSensorValueAux(sensor));

def getAllScalarQuantities():
    mScalarQuantities = []
    for sensorType in mLangMapDB.all():
        mScalarQuantities.extend(sensorType['words'])
    return mScalarQuantities

def getAllLocations():
    mLocations = []
    for sensor in mSensorDB.all():
        mLocations.append(sensor['location'])
    return mLocations


from os.path import join, basename
from glob import glob
import json
PICS_DIR = 'pics'
RECOG_DIR = 'responses'

HTML_FILENAME = 'printout.html'
htmlfile = open(HTML_FILENAME, 'w')
htmlfile.write("<title>Hello</title>")
htmlfile.write("
My name is Isha and this is my IBM Watson image analysis
")

for jsonname in glob(join(RECOG_DIR, '*.json')):
    print("Extracting", jsonname)
    j = json.load(open(jsonname))
    img = j['images'][0]
    imgname = img['image']
    htmlfile.write("
%s
" % imgname)

    imgfilename = join(PICS_DIR, imgname)
    htmlfile.write('' % imgfilename)

    bnum = 0
    scores = img['scores']
    htmlfile.write("
")

    for entry in scores:
        olist = []
        bnum +=1
        n = str(bnum) + ". "
        line = entry['classifier_id'] + " -- " + str(entry['score'])
        htmlfile.write("
%s
" % line)
    htmlfile.write("
")

htmlfile.close()


import keras
import tensorflow as tf

from train import train_model

config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
config.gpu_options.allow_growth = True
keras.backend.tensorflow_backend.set_session(tf.Session(config=config))

def main() -> None:
    batch_size = 128
    epochs = 30
    learning_rate = 0.00001
    filters = 512
    dropout = 0.5
    layers = 3
    l2_reg = 0.
    # model_name = 'rmdl_mod'  # model_name = sys.argv[2]
    dataset_name = 'cifar'  # dataset_name = sys.argv[1]

    for model_name in ['rmdl_mod']:
        try:
            train_model(
                dataset_name=dataset_name,
                model_name=model_name,
                batch_size=batch_size,
                epochs=epochs,
                learning_rate=learning_rate,
                dropout=dropout,
                l2_reg=l2_reg,
                layers=layers,
                filters=filters,
            )
        except Exception as e:
            print(e)

    # rmdl_mod
    # 1: dropout = 0.05
    # 2: dropout = 0.8
    # 3: dropout = 0.5
    # 4: dropout = 0.4
    # 5: lr = 0.0001
    # 6: 0.00001

if __name__ == "__main__":
    main()


from scapy.all import *
from scapy.layers.http import *

packets = rdpcap("cap.pcap")
for packet in packets:
    if HTTP in packet:
        if HTTPResponse in packet:
            status = packet[HTTPResponse].Status_Code
            print(status)


import unittest
from HTMLTestRunnerNew import HTMLTestRunner
from Website.test_case.model.function import *
import time

report_dir = os.path.dirname(os.path.abspath(__file__)) + r'\test_report'
test_dir = os.path.dirname(os.path.abspath(__file__)) + r'\test_case'

print('start run test case....')
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_login.py')
now = time.strftime('%Y-%m-%d %H_%M_%S')
report_name = report_dir + '/' + now + ' result.html'
print(report_name)

with open(report_name, 'wb') as file:
    runner = HTMLTestRunner(stream=file,
                            title='测试报告',
                            description='QQ邮箱登录',
                            verbosity=2,
                            tester='GanYu')
    runner.run(discover)
latest_report = latest_report(report_dir)

send_mail(latest_report)
# send_mail(latest_report)
print('test end!')


from pydantic import BaseModel,NoneStr
from datetime import datetime
from typing import List, Optional
class questions(BaseModel):
    question:str = None
    answer:str = None
    inputvalue : int = None
class services(BaseModel):
    service_category: int = None
    services: Optional[List[NoneStr]] = None
class hospitalinfra(BaseModel):
    site_id :int = None
    questionary:List[questions] = None
    service: List[services] = None
    certifications : List[str] = None
    equipment : List[str] = None
    created_by_id :int = None
    others_certifications:str = None
class updatehospitalinfra(BaseModel):
    questionary:List[questions] = None
    service: List[services] = None
    certifications : Optional[List[NoneStr]] = None
    equipment : Optional[List[NoneStr]] = None
    others_certifications:str = None
    updated_by_id :int = None
class Config:
    orm_mode = True