"""Task 5: Map Plotting"""

import rasterio
import numpy
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
import numpy as np
import cartopy.crs as ccrs

def map_plot(user_location, user_node, high_point, high_node, dataset, shortest_path_gpd, flood_poly=False):
    """This function plots a map of the user_location, highest point and shortest path gpd df
    over an OS Explorer and elevation raster basemap.
    """
    print("\nPlotting your shortest route to higher ground...")
    # background and the map extent
    background = rasterio.open('data/background/raster-50k_2724246.tif')
    back_array = background.read(1)
    palette = np.array([value for key, value in background.colormap(1).items()])
    background_image = palette[back_array]

    bg_extent = [background.bounds.left, background.bounds.right, background.bounds.bottom, background.bounds.top]
    el_extent = [dataset.bounds.left, dataset.bounds.right, dataset.bounds.bottom, dataset.bounds.top]

    display_extent = [((user_location.x + high_point.x) / 2) - 5000,
                      ((user_location.x + high_point.x) / 2) + 5000,
                      ((user_location.y + high_point.y) / 2) - 5000,
                      ((user_location.y + high_point.y) / 2) + 5000]
    # Task 5: Map Plotting
    # defining the walking path between the user and the nearest NIT node
    # and defining the walking path between highest point and its nearest NIT node
    waking_route_user_node_lons = [user_location.x, user_node.x]
    waking_route_user_node_lats = [user_location.y, user_node.y]
    waking_route_highest_point_lons = [high_point.x, high_node.x]
    waking_route_highest_point_lats = [high_point.y, high_node.y]

    # Set up figure and extent
    fig = plt.figure(figsize=(3, 3), dpi=300)
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.OSGB())
    ax.set_extent(display_extent, crs=ccrs.OSGB())

    # imshow for the background
    ax.imshow(background_image, origin="upper", extent=bg_extent, zorder=0)

    # imshow for the elevation raster
    plt.imshow(dataset.read(1), extent=el_extent, cmap='gist_earth', origin='upper', zorder=0, alpha=0.65,
               resample='True', vmax=numpy.amax(dataset.read(1)), vmin=numpy.amin(dataset.read(1)))

    if flood_poly:
        for i in flood_poly.geoms:
            plt.plot(*i.exterior.xy, color="darkgray", linewidth=0.5, alpha=0.7)

    # plotting the shortest path
    shortest_path_gpd.plot(ax=ax, edgecolor="orangered", linewidth=0.5, zorder=2, label='ITN shortest path')

    # scattering the user point the highest elevation point
    ax.scatter(user_location.x, user_location.y, s=1.5, c='r', label='Your location', marker="*")
    ax.scatter(high_point.x, high_point.y, s=1.5, c='k', label='Highest_point', marker='*')
    # user_location.xy()[1] arrow
    x, y, arrow_length = 0.9, 0.95, 0.18
    ax.annotate('N', xy=(x, y), xytext=(x, y - arrow_length), arrowprops=dict(facecolor='black', width=2, headwidth=8),
                ha='center', va='center', fontsize=9, xycoords=ax.transAxes)

    # adding scale bar
    scalebar = AnchoredSizeBar(ax.transData, 1000, '1 km', 'lower center',
                               pad=0.1, color='black', frameon=False, size_vertical=1)

    ax.add_artist(scalebar)

    # 6) plotting the color bar
    cbar = plt.colorbar(fraction=0.03, pad=0.03)
    cbar.set_label(r"Elevation", size=8)
    cbar.ax.tick_params(labelsize=5)
    # 7) plotting the walking paths
    ax.plot(waking_route_user_node_lons, waking_route_user_node_lats, c='k', label='walking route', linewidth=0.5,
            linestyle='dashed')

    link_patch = mlines.Line2D([], [], color='orangered', label='ITN Shortest Path', linewidth=0.5)
    walk_patch = mlines.Line2D([], [], color='k', label='Walking route', linewidth=0.5, linestyle='dashed')
    user_star = mlines.Line2D([], [], color='r', marker="*", linestyle='None', markersize=2, label='Your location')
    high_point = mlines.Line2D([], [], color='k', marker='*', linestyle='None', markersize=2, label='Highest point')

    # Dynamic legend - can handle absence of flood_poly
    if flood_poly:
        flood_patch = mlines.Line2D([], [], color='darkgray', label='Flood line', linewidth=0.5)
        plt.legend(fontsize=3, loc=2, handles=[user_star, high_point, walk_patch, link_patch, flood_patch],
                   title="Legend")
    else:
        plt.legend(fontsize=3, loc=2, handles=[user_star, high_point, walk_patch, link_patch], title="Legend")
    ax.plot(waking_route_highest_point_lons, waking_route_highest_point_lats, c='k', label='walking route',
            linewidth=0.5, linestyle='dashed')

    plt.show()


import struct

import numpy as np

from ..utils.enum_utils import IntEnum
from .defs import *

class CovarianceType(IntEnum):
    COVARIANCE_TYPE_UNKNOWN = 0
    COVARIANCE_TYPE_APPROXIMATED = 1
    COVARIANCE_TYPE_DIAGONAL_KNOWN = 2
    COVARIANCE_TYPE_KNOWN = 3

class ROSPoseMessage(MessagePayload):
    """!
    @brief ROS `Pose` message (@ref MessageType::ROS_POSE)

    The relative change in ENU position since the time of the first @ref PoseMessage, resolved in the local ENU frame at
    the time of the first @ref PoseMessage.
    """
    MESSAGE_TYPE = MessageType.ROS_POSE
    MESSAGE_VERSION = 0

    _FORMAT = '< 3d 4d'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        # The time of the message, in P1 time (beginning at power-on)
        self.p1_time = Timestamp()
        # relative ENU position (see ros.h messages)
        self.position_rel_m = np.full((3,), np.nan)
        # The platform orientation, represented as a quaternion with the scalar
        # component last (x, y, z, w)
        self.orientation = np.full((4,), np.nan)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(ROSPoseMessage._FORMAT, buffer, offset,
                         self.position_rel_m[0], self.position_rel_m[1], self.position_rel_m[2],
                         self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3])

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.position_rel_m[0], self.position_rel_m[1], self.position_rel_m[2],
         self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3]) = \
            struct.unpack_from(ROSPoseMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSPoseMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS Pose Message @ %s\n' % str(self.p1_time)
        string += '  Position : %.6f, %.6f, %.6f \n' % tuple(self.position_rel_m)
        string += '  Orientation: %.12f, %.12f, %.12f, %.12f \n' % tuple(self.orientation)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSPoseMessage._SIZE

    @classmethod
    def to_numpy(cls, messages):
        result = {
            'p1_time': np.array([float(m.p1_time) for m in messages]),
            'position_rel_m': np.array([m.position_rel_m for m in messages]).T,
            'orientation': np.array([m.orientation for m in messages]).T,
        }
        return result

class ROSGPSFixMessage(MessagePayload):
    """!
    @brief ROS `GPSFix` message (MessageType::ROS_GPS_FIX).
    """
    MESSAGE_TYPE = MessageType.ROS_GPS_FIX
    MESSAGE_VERSION = 0

    _FORMAT = '< ddd ddd ddd d ddddd dddddddddd 9d B 3B'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        self.p1_time = Timestamp()
        # Geodetic Position (expressed using the WGS-84 ellipsoid)
        # Geodetic latitude (in degrees)
        self.latitude_deg = np.nan
        # Geodetic longitude (in degrees)
        self.longitude_deg = np.nan
        # Altitude above the ellipsoid (in meters)
        self.altitude_m = np.nan

        # Velocity
        # The vehicle direction from north (in degrees)
        self.track_deg = np.nan
        # The vehicle ground speed (in meters/second)
        self.speed_mps = np.nan
        # The vehicle vertical speed (in meters/second)
        self.climb_mps = np.nan

        # Vehicle Orientation (not supported)
        self.pitch_deg = np.nan
        self.roll_deg = np.nan
        self.dip_deg = np.nan

        # The GPS time of the message (in seconds), referenced to 1980/1/6
        self.gps_time = np.nan

        # Dilution Of Precision
        self.gdop = np.nan  # Geometric (position + time) DOP
        self.pdop = np.nan  # Positional (3D) DOP
        self.hdop = np.nan  # Horizontal DOP
        self.vdop = np.nan  # Vertical DOP
        self.tdop = np.nan  # Time DOP

        # Measurement Uncertainty (95% Confidence)
        # Spherical position uncertainty (in meters) [epe]
        self.err_3d_m = np.nan
        # Horizontal position uncertainty (in meters) [eph]
        self.err_horiz_m = np.nan
        # Vertical position uncertainty (in meters) [epv]
        self.err_vert_m = np.nan
        self.err_track_deg = np.nan  # Track uncertainty (in degrees) [epd]
        # Ground speed uncertainty (in meters/second) [eps]
        self.err_speed_mps = np.nan
        # Vertical speed uncertainty (in meters/second) [epc]
        self.err_climb_mps = np.nan
        self.err_time_sec = np.nan  # Time uncertainty (in seconds) [ept]
        self.err_pitch_deg = np.nan  # Pitch uncertainty (in degrees)
        self.err_roll_deg = np.nan  # Roll uncertainty (in degrees)
        self.err_dip_deg = np.nan   # Dip uncertainty (in degrees)

        # Position Covariance
        """
            The 3x3 position covariance matrix (in m^2), resolved in the local ENU
            frame. Values are stored in row-major order.
        """
        self.position_covariance_m2 = np.full((9,), np.nan)
        # The method in which @ref position_covariance_m2 was populated
        self.position_covariance_type = CovarianceType.COVARIANCE_TYPE_UNKNOWN

        self.reserved = np.full((3,), 0, np.uint8)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(ROSGPSFixMessage._FORMAT, buffer, offset,
                         self.latitude_deg,
                         self.longitude_deg,
                         self.altitude_m,
                         self.track_deg,
                         self.speed_mps,
                         self.climb_mps,
                         self.pitch_deg,
                         self.roll_deg,
                         self.dip_deg,
                         self.gps_time,
                         self.gdop,
                         self.pdop,
                         self.hdop,
                         self.vdop,
                         self.tdop,
                         self.err_3d_m,
                         self.err_horiz_m,
                         self.err_vert_m,
                         self.err_track_deg,
                         self.err_speed_mps,
                         self.err_climb_mps,
                         self.err_time_sec,
                         self.err_pitch_deg,
                         self.err_roll_deg,
                         self.err_dip_deg,
                         self.position_covariance_m2[0], self.position_covariance_m2[1], self.position_covariance_m2[2],
                         self.position_covariance_m2[3], self.position_covariance_m2[4], self.position_covariance_m2[5],
                         self.position_covariance_m2[6], self.position_covariance_m2[7], self.position_covariance_m2[8],
                         self.position_covariance_type,
                         self.reserved[0], self.reserved[1], self.reserved[2])

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.latitude_deg,
         self.longitude_deg,
         self.altitude_m,
         self.track_deg,
         self.speed_mps,
         self.climb_mps,
         self.pitch_deg,
         self.roll_deg,
         self.dip_deg,
         self.gps_time,
         self.gdop,
         self.pdop,
         self.hdop,
         self.vdop,
         self.tdop,
         self.err_3d_m,
         self.err_horiz_m,
         self.err_vert_m,
         self.err_track_deg,
         self.err_speed_mps,
         self.err_climb_mps,
         self.err_time_sec,
         self.err_pitch_deg,
         self.err_roll_deg,
         self.err_dip_deg,
         self.position_covariance_m2[0], self.position_covariance_m2[1], self.position_covariance_m2[2],
         self.position_covariance_m2[3], self.position_covariance_m2[4], self.position_covariance_m2[5],
         self.position_covariance_m2[6], self.position_covariance_m2[7], self.position_covariance_m2[8],
         self.position_covariance_type,
         self.reserved[0], self.reserved[1], self.reserved[2]) = \
            struct.unpack_from(ROSGPSFixMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSGPSFixMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS GPSFix Message @ %s\n' % str(self.p1_time)
        string += '  Position : %.8f, %.8f, %.2f \n' % (
            self.latitude_deg, self.longitude_deg, self.altitude_m)
        string += '  Velocity: %.6f, %.6f, %.6f\n' % (
            self.track_deg, self.speed_mps, self.climb_mps)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSGPSFixMessage._SIZE

class ROSIMUMessage(MessagePayload):
    """!
    @brief ROS `Imu` message (@ref MessageType::ROS_IMU)
    """
    MESSAGE_TYPE = MessageType.ROS_IMU
    MESSAGE_VERSION = 0

    _FORMAT = '< 4d 9d 3d 9d 3d 9d'
    _SIZE: int = struct.calcsize(_FORMAT)

    def __init__(self):
        self.p1_time = Timestamp()
        # The platform orientation, represented as a quaternion with the scalar
        # component last (x, y, z, w)
        self.orientation = np.full((4,), 0.0)
        # Orientation covariance matrix. Set to -1 if not available
        self.orientation_covariance = np.full((9,), -1.0)
        # Vehicle x/y/z rate of rotation (in radians/second), resolved in the body
        # frame
        self.angular_velocity_rps = np.full((3,), 0.0)
        # Vehicle rate of rotation covariance matrix. Set to -1 if not available
        self.angular_velocity_covariance = np.full((9,), -1.0)
        # Vehicle x/y/z linear acceleration (in meters/second^2), resolved in the
        # body frame
        self.acceleration_mps2 = np.full((3,), 0.0)
        # Vehicle x/y/z acceleration covariance matrix. Set to -1 if not available
        self.acceleration_covariance = np.full((9,), -1.0)

    def pack(self, buffer: bytes = None, offset: int = 0, return_buffer: bool = True) -> (bytes, int):
        if buffer is None:
            buffer = bytearray(self.calcsize())

        initial_offset = offset

        offset += self.p1_time.pack(buffer, offset, return_buffer=False)

        struct.pack_into(
            ROSIMUMessage._FORMAT, buffer, offset,
            self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3],
            self.orientation_covariance[0], self.orientation_covariance[1], self.orientation_covariance[2],
            self.orientation_covariance[3], self.orientation_covariance[4], self.orientation_covariance[5],
            self.orientation_covariance[6], self.orientation_covariance[7], self.orientation_covariance[8],
            self.angular_velocity_rps[0], self.angular_velocity_rps[1], self.angular_velocity_rps[2],
            self.angular_velocity_covariance[0], self.angular_velocity_covariance[1], self.angular_velocity_covariance[2],  # noqa
            self.angular_velocity_covariance[3], self.angular_velocity_covariance[4], self.angular_velocity_covariance[5],  # noqa
            self.angular_velocity_covariance[6], self.angular_velocity_covariance[7], self.angular_velocity_covariance[8],  # noqa
            self.acceleration_mps2[0], self.acceleration_mps2[1], self.acceleration_mps2[2],
            self.acceleration_covariance[0], self.acceleration_covariance[1], self.acceleration_covariance[2],
            self.acceleration_covariance[3], self.acceleration_covariance[4], self.acceleration_covariance[5],
            self.acceleration_covariance[6], self.acceleration_covariance[7], self.acceleration_covariance[8],
            )

        if return_buffer:
            return buffer
        else:
            return offset - initial_offset

    def unpack(self, buffer: bytes, offset: int = 0, message_version: int = MessagePayload._UNSPECIFIED_VERSION) -> int:
        initial_offset = offset

        offset += self.p1_time.unpack(buffer, offset)
        (self.orientation[0], self.orientation[1], self.orientation[2], self.orientation[3],
         self.orientation_covariance[0], self.orientation_covariance[1], self.orientation_covariance[2],
         self.orientation_covariance[3], self.orientation_covariance[4], self.orientation_covariance[5],
         self.orientation_covariance[6], self.orientation_covariance[7], self.orientation_covariance[8],
         self.angular_velocity_rps[0], self.angular_velocity_rps[1], self.angular_velocity_rps[2],
         self.angular_velocity_covariance[0], self.angular_velocity_covariance[1], self.angular_velocity_covariance[2],
         self.angular_velocity_covariance[3], self.angular_velocity_covariance[4], self.angular_velocity_covariance[5],
         self.angular_velocity_covariance[6], self.angular_velocity_covariance[7], self.angular_velocity_covariance[8],
         self.acceleration_mps2[0], self.acceleration_mps2[1], self.acceleration_mps2[2],
         self.acceleration_covariance[0], self.acceleration_covariance[1], self.acceleration_covariance[2],
         self.acceleration_covariance[3], self.acceleration_covariance[4], self.acceleration_covariance[5],
         self.acceleration_covariance[6], self.acceleration_covariance[7], self.acceleration_covariance[8],
         ) = \
            struct.unpack_from(ROSIMUMessage._FORMAT,
                               buffer=buffer, offset=offset)
        offset += ROSIMUMessage._SIZE
        return offset - initial_offset

    def __repr__(self):
        return '%s @ %s' % (self.MESSAGE_TYPE.name, self.p1_time)

    def __str__(self):
        string = 'ROS IMU Message @ %s\n' % str(self.p1_time)
        string += '  Orientation: %.6f, %.6f, %.6f, %.6f \n' % tuple(
            self.orientation)
        string += '  Angular Velocity: %.6f, %.6f, %.6f \n' % tuple(
            self.angular_velocity_rps)
        string += '  Acceleration: %.6f, %.6f, %.6f \n' % tuple(
            self.acceleration_mps2)
        return string

    @classmethod
    def calcsize(cls) -> int:
        return Timestamp.calcsize() + ROSIMUMessage._SIZE


from itertools import combinations

def part1():
    with open('input.txt', 'r') as file:
        input_numbers = file.readlines()
        input_numbers = list(map(int, input_numbers))

    combs_2 = combinations(input_numbers, 2)
    combs_3 = combinations(input_numbers, 3)

    for comb in combs_2:
        if sum(comb) == 2020:
            print('Part 1 answer:', comb[0] * comb[1])
            break

    for comb in combs_3:
        if sum(comb) == 2020:
            print('Part 2 answer:', comb[0] * comb[1] * comb[2])
            break

if __name__ == '__main__':
    part1()


import os
import time

from datetime import datetime, timedelta
from typing import Optional

from rich.console import Console
from rich.table import Table
from rich.text import Text
import yaml

from tsdapiclient.tools import (check_if_exp_is_within_range,
                                check_if_key_has_expired, debug_step,
                                get_config_path, get_claims,
                                check_if_key_has_expired,)

SESSION_STORE = get_config_path() + '/session'

def session_file_exists() -> bool:
    return False if not os.path.lexists(SESSION_STORE) else True

def session_is_expired(env: str, pnum: str, token_type: str) -> bool:
    if not session_file_exists():
        return True
    token = session_token(env, pnum, token_type)
    debug_step(f'found token: {token}')
    if not token:
        return True
    if check_if_key_has_expired(token):
        debug_step('session expired')
        return True
    else:
        debug_step('session has not expired')
        return False

def session_expires_soon(env: str, pnum: str, token_type: str, minutes: int = 10) -> bool:
    if not session_file_exists():
        return None
    token = session_token(env, pnum, token_type)
    if not token:
        return False
    target_time = datetime.utcnow() + timedelta(minutes=minutes)
    upper = int(time.mktime(target_time.timetuple()))
    lower = int(time.time())
    if check_if_exp_is_within_range(token, lower=lower, upper=upper):
        debug_step(f'session will expire in the next {minutes} minutes')
        return True
    else:
        debug_step('session will not expire soon')
        return False

def session_read(session_store: str = SESSION_STORE) -> dict:
    with open(session_store, 'r') as f:
        data = yaml.load(f, Loader=yaml.Loader)
    return data

def session_update(
    env: str,
    pnum: str,
    token_type: str,
    token: str,
    refresh_token: Optional[str] = None,
) -> None:
    default = {
        'prod': {},
        'alt': {},
        'test': {},
        'ec-prod': {},
        'ec-test': {},
        'dev': {},
    }
    if not session_file_exists():
        debug_step('creating new tacl session store')
        data = default
    try:
        data = session_read()
    except FileNotFoundError:
        data = default
    target = data.get(env, {}).get(pnum, {})
    target[token_type] = token
    target[f'{token_type}_refresh'] = refresh_token
    if not data.get(env):
        data[env] = {}
    data[env][pnum] = target
    debug_step('updating session')
    with open(SESSION_STORE, 'w') as f:
        f.write(yaml.dump(data, Dumper=yaml.Dumper))

def session_token(env: str, pnum: str, token_type: str) -> str:
    data = session_read()
    return data.get(env, {}).get(pnum, {}).get(token_type)

def session_refresh_token(env: str, pnum: str, token_type: str) -> str:
    data = session_read()
    return data.get(env, {}).get(pnum, {}).get(f'{token_type}_refresh')

def session_clear() -> None:
    data = {
        'prod': {},
        'alt': {},
        'test': {},
        'ec-prod': {},
        'ec-test': {},
        'dev': {},
    }
    with open(SESSION_STORE, 'w') as f:
        f.write(yaml.dump(data, Dumper=yaml.Dumper))

def session_print(session_file: str = SESSION_STORE) -> None:
    console = Console()
    try:
        data = session_read()
    except FileNotFoundError:
        print("No session file found")
        exit(1)

    table = Table(title=f"{__package__} session details", show_lines=True)
    table.add_column("Environment")
    table.add_column("Project")
    table.add_column("User")
    table.add_column("Groups")
    table.add_column("Type")
    table.add_column("Expiry")

    for env in data:
        for project in data[env].keys():
            for token_type in data[env][project].keys():
                token = data[env][project][token_type]
                if token:
                    claims = get_claims(token)
                    exp = claims['exp']
                    expiry = Text(datetime.fromtimestamp(exp).strftime('%Y-%m-%d %H:%M:%S'))
                    if check_if_key_has_expired(token):
                        expiry.stylize('bold red')
                    user = claims.get('user')
                    table.add_row(
                        env,
                        project,
                        user,
                        f"{', '.join(claims.get('groups', []))}",
                        claims.get("name", token_type),
                        expiry,
                    )
    if table.rows:
        console.print(table)
    else:
        console.print("No sessions found")


import string
import collections
import random
from wordcloud import WordCloud
import matplotlib.pyplot as plt

class TrieNode():
    def __init__(self):
        self.children = {}
        self.isEnd = False

class Trie():
    def __init__(self):
        # Initialising the trie structure.
        self.root = TrieNode()
        self.word_list = []

    def add_Child(self, word):
        node = self.root
        if len(word) == 0:
            self.isEnd = True
            return
        for ch in word:
            if not node.children.get(ch):
                node.children[ch] = TrieNode()
            node = node.children[ch]
        node.isEnd = True

    def search_word(self,startOfSentence):
        words = startOfSentence.split(" ")
        prefix = words[-1]
        prev = startOfSentence[:-len(prefix)]
        node = self.root
        not_found = False
        temp_word = ''
        for ch in prefix:
            if not node.children.get(ch):
                not_found = True
                break

            temp_word += ch
            node = node.children[ch]
        if not_found:
            return 0
        elif node.isEnd and not node.children:
            return -1
        self.DFS(node, temp_word, prev)
        return self.word_list

    def DFS(self, node, word,prev):
        if node.isEnd:
            self.word_list.append(prev + word)

        for ch,node in node.children.items():
            self.DFS(node, word + ch,prev)

class project:
    def __init__(self):
        with open('1000_common_words.txt', 'r') as file2:
            self.common = file2.read().replace('\\n', '')
        with open('sherlock.txt', 'r') as file:
            self.text = file.read().replace('\\n', '')

    def text_to_string(self):
     # split into words by white space
        self.words = self.text.split()

        # remove punctuation from each word

        table = str.maketrans('', '', string.punctuation)
        self.filtered_words = [w.translate(table) for w in self.words]

        #replace common words
        self.filtered_words_common = [word for word in self.filtered_words if word.lower() not in self.common]

        # Counter
        self.count_All = collections.Counter(self.filtered_words)

        # Counter chapter wise
        self.Chapters = self.text.split('ADVENTURE')
        self.word_Chapters =[]
        for i in range(1,len(self.Chapters)):
            word = self.Chapters[i].split()
            word = [w.translate(table) for w in word]
            self.word_Chapters.append(word)
        self.count_ChapterWise = [collections.Counter(text) for text in self.word_Chapters]

    def getTotalNumberOfWords(self):
        return len(self.filtered_words)

    def getTotalUniqueWords(self):
        return len(self.count_All)

    def get20MostFrequentWords(self):
        return list(self.count_All.most_common(20))

    def get20MostInterestingFrequentWords(self):
        self.unique_remove_common = collections.Counter(self.filtered_words_common)
        return list(self.unique_remove_common.most_common(20))

    def get20LeastFrequentWords(self):
        return list(self.count_All.most_common()[:-20-1:-1])

    def getFrequencyOfWord(self,word):
        self.count =[]
        for i in range(len(self.word_Chapters)):
            self.count.append(self.count_ChapterWise[i][word])
        return self.count

    def getChapterQuoteAppears(self, quote):
        for i in range(len(self.Chapters)):
            if quote in self.Chapters[i].replace('\\n', ''):
                return "Chapter:"+ str(i)
        return "Quote Not Found"

    def generateCache(self):
        self.cache ={}
        def pair():
            for i in range(len(self.filtered_words)-2):
                yield (self.filtered_words[i], self.filtered_words[i+1])
        for w1,w2 in pair():
            if w1 in self.cache:
                self.cache[w1].append(w2)
            else:
                self.cache[w1] =[w2]

    def generateSentence(self, curWord, size =22):
        gen_words =[]
        random_index =0
        self.generateCache()
        for i in range(size):
            gen_words.append(curWord)
            random_index= random.randint(0,len(self.cache[curWord])-1)
            curWord = self.cache[curWord][random_index]
        return ' '.join(gen_words)

    def getAutocompleteSentence(self, startOfSentence):
        trie = Trie()
        for word in self.filtered_words:
            trie.add_Child(word)
        return trie.search_word(startOfSentence)
p = project()
p.text_to_string()
print("----------getTotalNumberOfWords----------")
print(p.getTotalNumberOfWords())
print()
print("------------getTotalUniqueWords------------")
print(p.getTotalUniqueWords())
print()
print("------------get20MostFrequentWords------------")
print(p.get20MostFrequentWords())
print()
print("------------get20MostInterestingFrequentWords------------")
print(p.get20MostInterestingFrequentWords())
print()
print("------------get20LeastFrequentWords---------------")
print(p.get20LeastFrequentWords())
print()
print("------------getFrequencyOfWord------------")
print(p.getFrequencyOfWord('Watson'))
print()
print("------------getChapterQuoteAppears------------")
print(p.getChapterQuoteAppears("There is nothing more deceptive than an obvious fact"))
print()
print("------------generateSentence----------------")
print(p.generateSentence('Sherlock'))
print()
print("------------getAutocompleteSentence------------")
print(p.getAutocompleteSentence('I have no do'))

# Visualization
d1 = dict(p.get20MostFrequentWords())
d2 = dict(p.get20MostInterestingFrequentWords())
d3 = dict(p.get20LeastFrequentWords())
wordcloud = WordCloud()
wordcloud.generate_from_frequencies(frequencies=d2)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

f, ax = plt.subplots()

freq = p.getFrequencyOfWord('Sherlock')
chapter = [1,2,3,4,5,6,7,8,9,10,11,12]
p = plt.bar(chapter, freq,color='#2e8b57')
plt.title('Usage of "Sherlock" per chapter')
plt.xlabel("Chapters")
plt.ylabel("Frequency")
ax.set_xticks(range(1, 13))
ax.set_xticklabels(chapter)
p[8].set_color('#8B0000')
p[9].set_color('#8B0000')
p[10].set_color('#8B0000')
p[11].set_color('#8B0000')
plt.show()


#!/usr/bin/env python3

import requests
import json
from pyproj import Transformer
import string

DEFAULT_STRASSEN_SAVE = 'strassen.json'
DEFAULT_ADRESSEN_SAVE = 'adressen.json'

def getCoordinateFromBbox(bbox):
    return [(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2]

def OSM_LongLat(osm):
    transformer = Transformer.from_crs("epsg:5972", "epsg:4326")
    lonLat = transformer.transform(osm[0], osm[1])
    return {
        'lon': lonLat[1],
        'lat': lonLat[0]}

def isAddress(a):
    return 'search_category' in a and a['search_category'] == '05_Adressen'

def isInStreet(a, street):
    return 'displaytext' in a and a['displaytext'].startswith(f'{street} ')

def formatAddress(a):
    return {
        'name': a['displaytext'][:-len(' (Adresse)')],
        'coord': OSM_LongLat(getCoordinateFromBbox(a['bbox']))
    }

def isStrasse(a):
    return 'search_category' in a and a['search_category'] == '01_Strassen'

def getAdressenInStrasse(strasse):
    response = requests.get(
    url="https://mapfinder.jena.de/search",
    params={
        'query':                strasse,
        'searchTables':         [],
        'searchFilters':        [],
        'searchArea':           'Jena',
        'searchCenter':         '',
        'searchRadius':         '',
        'topic':                'stadtplan',
        'resultLimit':          500,
        'resultLimitCategory':  5})

    return [formatAddress(a) for a in response.json()["results"][1:] if isAddress(a) and isInStreet(a, strasse)]

def getStrassen(letter):
    response = requests.get(
    url="https://mapfinder.jena.de/search",
    params={
        'query':                letter,
        'searchTables':         [],
        'searchFilters':        [],
        'searchArea':           'Jena',
        'searchCenter':         '',
        'searchRadius':         '',
        'topic':                'stadtplan',
        'resultLimit':          500,
        'resultLimitCategory':  5})

    return [a['displaytext'][:-len(" (Strasse)")] for a in response.json()["results"][1:] if isStrasse(a)]

def getAddresse(address):
    response = requests.get(
        url="https://mapfinder.jena.de/search",
        params={
            "query":                address,
            "searchTables":         [],
            "searchFilters":        [],
            "searchArea":           "Jena",
            "searchCenter":         "",
            "searchRadius":         "",
            "topic":                "stadtplan",
            "resultLimit":          5,
            "resultLimitCategory":  5})
    adressen = list(filter(
        lambda a: ("search_category" in a) and (a["search_category"] == "05_Adressen"),
        response.json()["results"]))

    if len(adressen) == 0:
        raise Exception(f"Can't find the adress '{address}'")
    if len(adressen) > 3:
        raise Exception(f"Can't find one distinct adress for '{address}'")

    return OSM_LongLat(getCoordinateFromBbox(adressen[0]["bbox"]))

def getAllStrassen():
    strassen = []
    for letter in string.ascii_uppercase:
        for strasse in getStrassen(letter):
            if strasse not in strassen:
                yield strasse
                strassen.append(strasse)

def saveAllStrassen(filename=DEFAULT_STRASSEN_SAVE):
    with open(filename, "w") as f:
        json.dump(list(getAllStrassen()), f)

def loadAllStrassen(filename=DEFAULT_STRASSEN_SAVE):
    with open(filename, "r") as f:
        strassen = json.load(f)
    return strassen

def getAllAdressenFromStrassen(strassen):
    for strasse in strassen:
        adressenInStrasse = getAdressenInStrasse(strasse)
        print(f'{strasse}: {len(adressenInStrasse):3d} Adressen')
        for adresse in adressenInStrasse:
            yield adresse

def saveAllAdressenFromStrassen(strassen, database):
    for adresse in getAllAdressenFromStrassen(strassen):
        database.insert(adresse)

if __name__ == '__main__':
    import addressDb

    saveAllStrassen()
    database = addressDb.addressDb()
    saveAllAdressenFromStrassen(loadAllStrassen(), database)


from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django_project.exhibition.models import Exhibition, Picture
import os
import logging

logger = logging.getLogger('exhibition')

@receiver(post_delete, sender=Exhibition)
def auto_delete_link_on_delete(sender, instance, **kwargs):

    if instance.link:
        if os.path.isfile(instance.link.path):
            os.remove(instance.link.path)

            logger.info(f'Exhibition link deleted {instance.id}')

@receiver(post_delete, sender=Picture)
def auto_delete_link_on_delete(sender, instance, **kwargs):

    if instance.link:
        if os.path.isfile(instance.link.path):
            os.remove(instance.link.path)

            logger.info(f'Picture link deleted {instance.id}')


#!/usr/bin/env python
import os
import sys
import csv
import time
import coards
import datetime
import numpy as np
from pupynere import netcdf_file

class ParserWriter(object):
    '''
    Handle all information needed to parse LR Waveglider CSV files and produce
    NetCDF for each of them
    '''

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

    def write_nc(self):
        esec_list = []
        ex = ['col1', 'col2','col3','latitude','longitude'] #Colum labels
#        ex = csv.reader(open('info.txt'))
        reader = csv.DictReader(open('test.txt')) #open de the file and save the data in reader
        #Do a list of each label with the data on it
        for r in reader:
            gmtDTString = r['Time'] #read the variable time and save it in CF-1.0 nercdf convention
            tt = time.strptime(gmtDTString, '%Y-%m-%d %H:%M:%S')
            diff = datetime.datetime(*tt[:6]) - datetime.datetime(1970,1,1,0,0,0)
            esec_list.append(diff.days * 86400 + diff.seconds)
            #Create the list of all the variable
            for v in ex:
                exec('%s_list = []' % (v))
                exec("%s_list.append(r['%s'])" % (v,v,))
#======================================================================
#       Create the NetCDF file
        outFile='foo1.nc'
        self.ncFile = netcdf_file(outFile, 'w')
        self.outFile = outFile

        # Trajectory dataset, time is the only netCDF dimension
        self.ncFile.createDimension('TIME', len(esec_list))

        self.time = self.ncFile.createVariable('TIME', 'float64', ('TIME',))
        self.time.units = 'seconds since 1970-01-01'
        self.time[:] = esec_list

        # Write  variables
        for v in ex:
            ncVar = v.replace(' ', '_', 42)
            # Only Latitude, Longitude, Depth, and Time variables are upper case to match other Glider data
            print(v)
            if v == 'Latitude' or v == 'Longitude':
                exec("self.%s = self.ncFile.createVariable('%s', 'float64', ('TIME',))" % (ncVar.lower(), ncVar.upper(), ))
            else:
                exec("self.%s = self.ncFile.createVariable('%s', 'float64', ('TIME',))" % (ncVar.lower(), ncVar, ))
            exec("self.%s.long_name = '%s'" % (ncVar.lower(), v, ))
            exec("self.%s[:] = %s_list" % (ncVar.lower(), ncVar, ))

        # Fudge up a depth variable with a value of zero
        self.depth = self.ncFile.createVariable('DEPTH', 'float64', ('TIME',))
        self.depth.long_name = 'Depth'
        self.depth.standard_name = 'depth'
        self.depth.units = 'm'
        self.depth[:] = np.zeros(len(self.time[:]))

        self.add_global_metadata()

        self.ncFile.close()

        # End write_pco2()

    def add_global_metadata(self):
        '''
        This is the main advantage of using a class for these methods.  This method uses the
        instance variables to write metadata specific for the data that are written.
        '''

        iso_now = datetime.datetime.now().isoformat()

        self.ncFile.title = ''
        self.ncFile.netcdf_version = '3.6'
        self.ncFile.Convention = 'CF-1.4'
        self.ncFile.date_created = iso_now
        self.ncFile.date_update = iso_now
        self.ncFile.date_modified = iso_now
        self.ncFile.cdm_data_type = 'trajectory'
        self.ncFile.CF_featureType = 'trajectory'
        self.ncFile.data_mode = 'R'
        self.ncFile.geospatial_lat_min = np.min(self.latitude[:])
        self.ncFile.geospatial_lat_max = np.max(self.latitude[:])
        self.ncFile.geospatial_lon_min = np.min(self.longitude[:])
        self.ncFile.geospatial_lon_max = np.max(self.longitude[:])
        self.ncFile.geospatial_lat_units = 'degree_north'
        self.ncFile.geospatial_lon_units = 'degree_east'

        self.ncFile.geospatial_vertical_min= np.min(self.depth[:])
        self.ncFile.geospatial_vertical_max= np.max(self.depth[:])
        self.ncFile.geospatial_vertical_units = 'm'
        self.ncFile.geospatial_vertical_positive = 'down'

        self.ncFile.time_coverage_start = coards.from_udunits(self.time[0], self.time.units).isoformat()
        self.ncFile.time_coverage_end = coards.from_udunits(self.time[-1], self.time.units).isoformat()

        self.ncFile.distribution_statement = 'Any use requires prior approval from the MBARI CANON PI: Dr. Francisco Chavez'
        self.ncFile.license = self.ncFile.distribution_statement
        self.ncFile.useconst = 'Not intended for legal use. Data may contain inaccuracies.'
        self.ncFile.history = 'Created by "%s" on %s' % (' '.join(sys.argv), iso_now,)

if __name__ == '__main__':

    # Accept arguement of data directory name, e.g. /mbari/Tracking/gliders
    dataDir = '/home/flopez/dev/stoqshg/loaders/MarMenor/toNetCDF'
    netc = ParserWriter(parentDir=dataDir)
    netc.write_nc()


import numpy as np
import math
import tensorflow as tf
import matplotlib.pyplot as plt

#
# A ideia para mudança deste algoritmo é:
#   -> Usar uma base de dados ou usar dados gravados em arquivos do tipo Text.
#   -> Usar para fazer para treinar o modelo de preedição usando uma forma de regressão que pode ser Linear ou polinomial.
#   -> Pesquisa uma forma de receber dados constatemente sem ter que passar no modelo de treinamento
#   -> Otimizar código, usar boas práticas de programação para diminuir a complexidade do algoritmo.
#

def normalize(array):
    return (array - array.mean()) / array.std()

arq1 = open('dados/temperatura.txt', 'r')
arq2 = open('dados/velocidadeDoAr.txt', 'r')

dado1 = arq1.readlines()
dado2 = arq2.readlines()

# convert data
house_size = np.asarray([float(i) for i in dado1])
house_price = np.asarray([float(i) for i in dado2])

num_house = len(house_size)

print(len(house_size))
print(len(house_price))

# ake pegaremós 70% dos dados randômicos gerados para treinar o modelo de predição inteligênte
num_train_samples = math.floor(num_house * 0.7)

# Dados de treinamento 70%
train_house_size = np.asarray(house_size[:num_train_samples])
train_price = np.asarray(house_price[:num_train_samples])
train_house_size_norm = normalize(train_house_size)
train_price_norm = normalize(train_price)

# 30% dos valores gerados para o teste
test_house_size = np.asarray(house_size[num_train_samples:])
test_house_price = np.asarray(house_price[num_train_samples:])

#######################################################################################################################
#######################################################################################################################

tf_house_size = tf.placeholder(tf.float32, name='house_size')
tf_price = tf.placeholder(tf.float32, name='price')

tf_size_factor = tf.Variable(np.random.randn(), name='size_factor')
tf_price_offset = tf.Variable(np.random.randn(), name='price_offset')

tf_price_pred = tf_size_factor * tf_house_size + tf_price_offset

tf_cost = tf.reduce_sum(tf.pow(tf_price_pred - tf_price, 2)) / (2 * num_train_samples)

learning_rate = 0.01 #taxa de aprendizagem, valor ajustável. Busca o melhor possível.
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(tf_cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    num_training_iter = 2000
    for iteration in range(num_training_iter):
        for (x, y) in zip(train_house_size_norm, train_price_norm):
            sess.run(optimizer, feed_dict={tf_house_size: x, tf_price: y})

    training_cost = sess.run(tf_cost, feed_dict={tf_house_size: train_house_size_norm, tf_price: train_price_norm})

    def normalize_single_value(value, array):
        return (value - array.mean()) / array.std()

    def denormalize(value, array):
        return value * array.std() + array.mean()

    dados = []

    for (size, price) in zip(test_house_size, test_house_price):
        value = normalize_single_value(size, house_size)
        price_prediction = sess.run(tf_price_pred, feed_dict={tf_house_size:value})
        price_prediction = denormalize(price_prediction, house_price )
        dados.append(price_prediction)
        print("House size:",size, " Original price:", price, " Price Prediction:", price_prediction, "Diff:", (price_prediction - price))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(test_house_price, color='lightblue', linewidth=3)
ax.plot(dados, color='darkgreen')
plt.show()


# coding=utf-8
from django.conf import settings
from django.db.models import Q
from django.shortcuts import redirect as redirect_to_response
from django.shortcuts import render as render_django

def get_subdomain(request):
    """ Obtiene el subdominio con el nombre corto que utiliza la empresa """
    # host = request.META.get('HTTP_REFERER')
    # print("---------------------------- HOST-")
    # print(host)
    # print("*************************get_host*")
    # print(request.get_host())
    # print("///////////////////////////////////////////////////////////////////////////////////")
    #produccion
    # host = request.get_host()
    # if host != settings.LOCAL_DIRECCION:
    #     i = host.find(settings.URL)
    #     subdomain = host.split('.')
    #     if i == 0:
    #         subdomain = ''
    #     elif i > 0:
    #         subdomain = subdomain[0]
    #     return subdomain if subdomain != '' else None
    #
    # #local
    # else:

    host = request.META.get('HTTP_REFERER')
    if host:
        i = host.find(settings.URL)
        http_index = host.find('//')
        subdomain = host[http_index+2:i - 1].split('.')
        if subdomain[0] == 'www':
            subdomain = subdomain[1]
        else:
            subdomain = subdomain[0]
        return subdomain if subdomain != '' else None

    else:
        host = request.get_host()
        if host != settings.LOCAL_DIRECCION:
            i = host.find(settings.URL)
            subdomain = host.split('.')
            if i == 0:
                subdomain = ''
            elif i > 0:
                subdomain = subdomain[0]
            return subdomain if subdomain != '' else None

        else:
            return None

        # http_index = host.find('//')
        # subdomain = host[http_index+2:i - 1].split('.')
        # if subdomain[0] == 'www':
        #     subdomain = subdomain[1]
        # else:
        #     subdomain = subdomain[0]
        # return subdomain if subdomain != '' else None

    # if i > 0:
    #     host = host[0:i - 1]
    #     if host != 'www':
    #         host = host.replace('www.','').replace(settings.HTTP,'')
    #         return host
    # return None

def render(request, template, dict={}):
    dict.update({'settings': settings})
    return render_django(request, template, context=dict)

def redirect(to, dict=None):
    return redirect_to_response(to, dict)


from taxondb import TaxonomyDBCreator, TaxonomyDBFinder
import os

current_dir = os.path.dirname(__file__)

def test_taxondb():

    temp_file = os.path.join(current_dir, 'taxon.sqlite')
    taxon_creator = TaxonomyDBCreator()
    taxon_creator.connect(temp_file, is_new_db=True)
    taxon_creator.create()
    taxon_creator.close()

    result = ({'species': 9606, 'genus': 9605, 'family': 9604, 'order': 9443, 'class': 40674,
               'phylum': 7711, 'kingdom': 33208, 'superkingdom': 2759},
              {9606: 'Homo sapiens', 9605: 'Homo', 9604: 'Hominidae', 9443: 'Primates',
               40674: 'Mammalia', 7711: 'Chordata', 33208: 'Metazoa', 2759: 'Eukaryota'})

    taxon_finder = TaxonomyDBFinder()
    taxon_finder.connect(temp_file)
    search_result = taxon_finder.find_taxid_parents_simple(9606)
    assert search_result == result

    assert taxon_finder.find_taxid_childrens(9605) == {741158, 1425170, 63221, 9606}

    os.unlink(temp_file)


#!/usr/bin/python

"""
This program for calculating the expectation maximization
"""

import os
import sys
import subprocess
import re
import scipy.stats
import numpy

def dotProduct(X1, X2):
	return numpy.dot(X1 , X2)

def eclidieanDistance(X1, X2):
	return numpy.linalg.norm(X1 - X2)

##input data

path_to_inputData = ""
path_to_SVM_Data = ""

svmData = open(path_to_SVM_Data)
DataSVM = svmData.read()
DataSVM = DataSVM.split("\n")
DataSVM = DataSVM[0:-1]

for i in range(len(DataSVM)):
	m1 = re.search('\+?-?', DataSVM[i])
	m2 = re.search('[0-9]+.[0-9]*', DataSVM[i])
	m3 = re.search('<.*>', DataSVM[i])
	#print (m.group(0))
	#print (i)
	#print(DataSVM[i])
	DataSVM[i] = (float(m2.group(0)), m3.group(0) )

weightRange = [1.0 , 0.99]

for svm in DataSVM:
	if(svm[0] <= weightRange[0] and svm[0] >= weightRange[1]):
		print(svm[1])


import matplotlib.pyplot as plt
import numpy as np
import math
import csv
EWx = list()
EWy = list()
NSx = list()
NSy = list()
UDx = list()
UDy = list()
with open('elcentro_EW.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		EWx.append(float(row[0]))
		EWy.append(float(row[1]))
with open('elcentro_NS.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		NSx.append(float(row[0]))
		NSy.append(float(row[1]))
with open('elcentro_UP.dat', 'r') as csvfile:
	plots = csv.reader(csvfile, delimiter=" ")
	for row in plots:
		UDx.append(float(row[0]))
		UDy.append(float(row[1]))
plt.subplot(3,1,1)
plt.plot(EWx, EWy)
plt.title("EW")
plt.ylabel("Acceleration[g]")
plt.subplot(3,1,2)
plt.plot(NSx, NSy)
plt.title("NS")
plt.ylabel("Acceleration[g]")
plt.subplot(3,1,3)
plt.plot(UDx, UDy)
plt.ylabel("Acceleration[g]")
plt.title("UD")
plt.xlabel("Time[sec]")
plt.show()
PGA = max(NSy)
intesity = 3*(math.log10(PGA)+0.5)
print(PGA)
print(intesity)
FNSy = np.fft.fft(NSy)
freq = np.linspace(0.0,1.0/(2.0*(NSx[1]-NSx[0])),len(FNSy)/2)
plt.figure()
plt.plot(freq, 2.0/len(FNSy)*np.abs(FNSy[:len(FNSy)//2]))
plt.ylabel("Magnitude")
plt.xlabel("Frequency[Hz]")
plt.show()
PerdominantFrequency = max(FNSy)
print(np.abs(PerdominantFrequency))
plt.specgram(NSy,Fs=int(1.0/(NSx[1]-NSx[0])),NFFT=1024,noverlap=900)
plt.show()


from discord import Embed
from discord_ui import Button
from helpers.exceptions import NotEnoughPlayerException, ZeroRollException
from helpers.kbbi import AutentikasiKBBI, KBBI, TidakDitemukan
from os import path
from operator import itemgetter
from random import randint
import json
import requests

class SambungKataPlayer:
    def __init__(self):
        self.players = []
        self.ready = []
        self.active_player = []
        self.hasbi_playing = False
        self.active_index = 0

    def already_joined(self, player):
        return any(_player['id'] == player.id for _player in self.players)

    def add_player(self, player, is_hasbi: bool=False):
        if not self.already_joined(player):
            self.players.append({'player': player, 'id': player.id, 'roll': 3, 'point': 0})

        if is_hasbi:
            self.hasbi_playing = is_hasbi

    def remove_player(self, player):
        if self.already_joined(player):
            self.players.remove(self.get_player(player))

    def get_active_player(self):
        return self.players[self.active_index % len(self.players)]

    def next_player(self):
        self.active_index += 1
        return self.get_active_player()

    def start(self, player):
        if player in self.ready and len(self.players) < 2: raise NotEnoughPlayerException
        if self.already_joined(player): self.ready.append(self.get_player(player))

    def get_player(self, player):
        return [_player for _player in self.players if _player['id'] == player.id][0]

    def get_roll(self, player):
        return [_player['roll'] for _player in self.players if _player['id'] == player.id][0]

    def get_point(self, player):
        return [_player['point'] for _player in self.players if _player['id'] == player.id][0]

    def add_point(self, player, point):
        _player = self.get_player(player)
        _player['point'] += point

    def deduct_roll(self, player):
        _player = self.get_player(player)
        _player['roll'] -= 1 if _player['roll'] > 0 else 0

class KBBIServiceWrapper:
    url = 'https://kbbi-service.digibitstudio.repl.co/api/'

    def __init__(self, cookie_path: str='storage/kuki_kbbi.json', word_list_path: str='storage/kata-dasar-indonesia.txt'):
        super().__init__()
        self.cookie_path = cookie_path
        self.word_list_path = word_list_path
        self.words = []
        self.auth_kbbi = self.login_kbbi()

    def fetch_all_words(self):
        with open(self.word_list_path, 'r') as file:
            for line in file:
                line = line.strip('\n')
                self.words.append(line.rstrip())
                # pos = re.findall(r'\(.*?\)', line)
                # self.words.append(line.replace(str(pos[0]), '').rstrip().lower())

        return self.words

    def get_random_words(self):
        if not len(self.words):
            self.fetch_all_words()

        return self.words[randint(0, len(self.words)-1)]

    def get_word_from_kbbi(self, word: str):
        try:
            if not self.auth_kbbi: self.login_kbbi()
            kata = KBBI(word, self.auth_kbbi)
            return kata.serialisasi()
        except TidakDitemukan:
            return False

    def login_kbbi(self):
        try:
            if path.exists(self.cookie_path):
                auth = AutentikasiKBBI(lokasi_kuki=self.cookie_path)
                auth.ambil_kuki()
            else:
                auth = AutentikasiKBBI('valdain.shadow@gmail.com', 'iamrobohasbi', lokasi_kuki=self.cookie_path)
                auth.simpan_kuki()
            return auth
        except:
            return None

    def get_last_syllable(self, word):
        kata = word['entri'][0]['nama']

        return kata.split('.')[-1]

    def get_name(self, kbbi_word):
        base = self.get_pronounciation(kbbi_word)
        return base.replace('.', '')

    def get_pronounciation(self, kbbi_word):
        return kbbi_word['entri'][0]['nama']

    def get_makna(self, kbbi_word):
        return [entri['makna'] for entri in kbbi_word['entri']]

    def get_base_word(self, kbbi_word):
        return kbbi_word['entri'][0]['kata_dasar'] or ''

    def insert_word(self, kbbi_word, **kwargs):
        name = self.get_name(kbbi_word)
        pronounciation = self.get_pronounciation(kbbi_word)
        base_word = self.get_base_word(kbbi_word)

        mutation = '''
        mutation($input: WordInput!){
            createWord(input: $input){
                word{
                    id
                }
                code
            }
        }
        '''
        variables = {
            'input': {'name': name, 'pronounciation': pronounciation, 'baseWord': base_word}
        }

        response = requests.post(url=self.url, json={'query': mutation, 'variables': variables})
        data = json.loads(response.text)

        return int(data['data']['createWord']['word']['id'])

    def insert_meaning(self, kbbi_word, word_id):
        entri = kbbi_word['entri']
        meaning_count = 0

        for data in entri:
            for makna in data['makna']:
                submakna = ', '.join(makna['submakna'])
                list_pos = []
                for pos in makna['kelas']:
                    list_pos.append({'code': pos['kode'], 'verbose': pos['nama'], 'description': pos['deskripsi']})

                mutation =   '''
                mutation($input: MeaningInput!){
                    createMeaning(input: $input){
                        meaning{
                            id
                        }
                        code
                    }
                }
                '''
                variables = {
                    'input': {'word': word_id, 'meaning': submakna, 'pos': list_pos}
                }

                response = requests.post(url=self.url, json={'query': mutation, 'variables': variables})
                meaning_count += 1

        return meaning_count

class SambungKataHelper(SambungKataPlayer, KBBIServiceWrapper):
    def __init__(self, cookie_path: str='storage/kuki_kbbi.json', word_list_path: str='storage/kata-dasar-indonesia.txt'):
        SambungKataPlayer.__init__(self)
        KBBIServiceWrapper.__init__(self, cookie_path=cookie_path, word_list_path=word_list_path)
        self.current_word = ''
        self.started = False

    def check_start_game(self):
        self.started =  len(self.players) > 1 and ((self.hasbi_playing and len(self.players)-1 == len(self.ready)) or (len(self.players) == len(self.ready)))

        return self.started

    def roll(self, player):
        for data in self.player_data:
            if data['id'] == player.id:
                if data['roll'] > 0:
                    data['roll'] -= 1
                else:
                    raise ZeroRollException

        return self.get_random_words()

    def answer(self, word):
        self.add_point(self.get_active_player()['player'], len(word))
        self.next_player()

        return word

    def generate_not_enough_player_embed(self):
        embed = Embed(title='Tidak Bisa Memulai Permainan', color=0xff0000)
        embed.add_field(name='\u200b', value='Butuh setidaknya dua pemain untuk memulai permainan')

        return embed

    def generate_lobby_embed(self, players: list):
        string_players = ''
        for player in players:
            string_players += f'<:reply:885797725327745085>{player["player"].mention}\n'

        components = [
            Button(custom_id='sk_start', label='Start', color='green'),
            Button(custom_id='sk_join', label='Join', color='blurple'),
            Button(custom_id='sk_hasbi', label='Ajak Hasbi', color='blurple'),
            Button(custom_id='sk_exit', label='Exit', color='red')
        ]

        embed = Embed(title='Hasbi Sambung Kata', color=randint(0, 0xffffff))
        embed.add_field(name='Players:', value=string_players or 'Belum Ada Pemain')
        embed.set_footer(text=f'{len(self.ready)}/{len(self.players)-1 if self.hasbi_playing else len(self.players)} Pemain Sudah Menekan Tombol Start' if len(self.players) >= 2 else '')

        return embed, components

    def generate_round_embed(self, word):
        makna_string = ''
        player_string = ''

        for entri in word['entri']:
            for makna in entri['makna'][0:3]:
                makna_string += '<:reply:885797725327745085>'
                for kelas in makna['kelas']:
                    makna_string += f'({str(kelas["kode"])}) '
                makna_string += f'{" ".join(makna["submakna"])}\n'

        for player in self.players:
            player_string += '<:reply:885797725327745085>'
            if player == self.get_active_player():
                player_string += '<:next_arrow:885775938871263302> '
            player_string += f'{player["player"].mention} | Roll: {self.get_roll(player["player"])} | Point: {self.get_point(player["player"])}\n'

        embed = Embed(title='Hasbi Sambung Kata', color=randint(0, 0xffffff))
        embed.add_field(name=str(word['entri'][0]['nama']).upper(), value=makna_string)
        embed.add_field(name='Player', value=player_string, inline=False)

        return embed

    def hasbi_ai(self, question):
        data = []
        last_syllable = self.get_last_syllable(question)
        possible_answer = [word for word in self.words if word.startswith(last_syllable)]

        if not possible_answer: return 'roll'

        for answer in possible_answer:
            kbbi_word = self.get_word_from_kbbi(answer)
            last_syllable = self.get_last_syllable(kbbi_word)
            data.append(
                {
                    'word': answer,
                    'point': len(answer),
                    'possible_next': len([word for word in self.words if word.startswith(last_syllable)])
                }
            )

        sorted_data = sorted(data, key=itemgetter('possible_next'))
        same_possible_next = [data for data in sorted_data if data['possible_next'] == sorted_data[0]['possible_next']]

        if len(same_possible_next) > 1:
            sorted_data = sorted(sorted_data, key=itemgetter('point'), reverse=True)

        for data in sorted_data:
            if self.get_word_from_kbbi(data['word']): return self.answer(data['word'])


# -*- coding: gb18030 -*-

import BigWorld
import cschannel_msgs
import ShareTexts as ST
from bwdebug import *
from NormalActivityManager import NormalActivityManager

class YayuMgr( BigWorld.Base, NormalActivityManager ):

	def __init__(self):
		"""
		"""
		BigWorld.Base.__init__( self )
		self.noticeMsg 			= cschannel_msgs.BCT_YAYU_BEGIN_NOTIFY_0
		self.startMsg 			= cschannel_msgs.BCT_YAYU_BEGIN_NOTIFY
		self.endMgs 			= cschannel_msgs.BCT_YAYU_END_NOTIFY
		self.errorStartLog 		= cschannel_msgs.YA_YU_VOICE2
		self.errorEndLog 		= cschannel_msgs.YA_YU_VOICE3
		self.globalFlagKey		= "AS_yayuStart"
		self.spawnMonsterCount  = 40
		self.managerName 		= "YayuMgr"
		self.crondNoticeKey		= "yayu_start_notice"
		self.crondStartKey		= "yayu_Start"
		self.crondEndKey		= "yayu_End"
		NormalActivityManager.__init__( self )


import cv2, numpy as np

img1 = cv2.imread('../img/taekwonv1.jpg')
img2 = cv2.imread('../img/figures.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

# ORB 추출기 생성
detector = cv2.ORB_create()
# 키 포인트와 서술자 추출
kp1, desc1 = detector.detectAndCompute(gray1, None)
kp2, desc2 = detector.detectAndCompute(gray2, None)

# 인덱스 파라미터 설정 ---①
FLANN_INDEX_LSH = 6
index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 6,
                   key_size = 12,
                   multi_probe_level = 1)
# 검색 파라미터 설정 ---②
search_params=dict(checks=32)
# Flann 매처 생성 ---③
matcher = cv2.FlannBasedMatcher(index_params, search_params)
# 매칭 계산 ---④
matches = matcher.match(desc1, desc2)
# 매칭 그리기
res = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, \
            flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# 결과 출력
cv2.imshow('Flann + ORB', res)
cv2.waitKey()
cv2.destroyAllWindows()


# Sort List
# https://www.interviewbit.com/problems/sort-list/
#
# Sort a linked list in O(n log n) time using constant space complexity.
#
# Example :
#
# Input : 1 -> 5 -> 4 -> 3
#
# Returned list : 1 -> 3 -> 4 -> 5
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:

    def divide(self, A):
        """ Divade in two lists """
        slow = fast = A
        while slow and fast and fast.next:
            fast = fast.next.next
            if fast is None:
                slow.next, slow = None, slow.next
            else:
                slow = slow.next
        else:
            if fast and not fast.next:
                slow.next, slow = None, slow.next

        return slow

    def min(self, A, B):
        """ Find Min node """
        if A is None:
            return B
        elif B is None:
            return A
        else:
            return A.val < B.val and A or B

    def mergeTwoLists(self, A, B):
        """ Merge two sorted list """
        head = tmp = self.min(A, B)

        if head == None:
            return None

        A = A.next if tmp is A else A
        B = B.next if tmp is B else B

        while A or B:
            minn = self.min(A, B)
            tmp.next = minn
            tmp = minn

            A = A.next if tmp is A else A
            B = B.next if tmp is B else B
        return head

    def merge_sort(self, A):
        """ Merge sort """
        B = self.divide(A)

        if B is None:
            return A

        first = self.merge_sort(A)
        second = self.merge_sort(B)

        return self.mergeTwoLists(first, second)

    # @param A : head node of linked list
    # @return the head node in the linked list
    def sortList(self, A):
        return self.merge_sort(A)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


from utils.http.XBAPIView import *
from .serializers import *
from utils.pay.wx.xcu import Openid

class OpenidViewSet(XBListModelMixin):
    serializer_class = OpenIdSerializers
    # 获取用户openid(用户注册或者微信登陆)
    many = False
    def get_queryset(self):

        code = self.request.query_params.get("code", 0)
        if code:
            data = Openid(code=code).openid()
            if data:
                return data
            else:
                None
        else:
            return None


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x, left=None, right=None):
        self.val = x
        self.left = left
        self.right = right

class Solution:
    def sumNumbers(self, root: TreeNode) -> int:
        num_list = []
        def dfs(node, exist_nums, num_list):
            if not node and not exist_nums:
                return
            if not node.left and not node.right:
                num_list.append(exist_nums + [node.val])
                return
            if node.left:
                dfs(node.left, exist_nums+[node.val], num_list)
            if node.right:
                dfs(node.right, exist_nums+[node.val], num_list)
        dfs(root, [], num_list)
        res = 0
        for nums in num_list:
            for i in range(len(nums)):
                res += nums[i] * (10 ** (len(nums)-1-i))
        return res

# [4,9,0,5,1]
t = TreeNode(4, TreeNode(9, TreeNode(5), TreeNode(1)), TreeNode(0))
print(Solution().sumNumbers(t))


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

class AdalineGD(object):
    """
    매개 변수
    ---------
    eta : float
        학습률 ( 0.0 ~ 1.0)
    n_iter : int
        훈련 데이터셋 반복 횟수
    random_state : int
        가중치 무작위 초기화를 위한 난수 생성 시드

    속성
    ---------
    w_ :1d-array
        학습된 가중치
    errors_ : list
        에포크마다 누정된 분류 오류
    """
    def  __init__(self, eta = 0.01, n_iter = 50, random_state = 1):
        self.eta = eta
        self.n_iter = n_iter
        self.random_state = random_state

    "훈련 데이터 학습"
    def fit(self, X, y):
        """
        매개변수
        ----------
        X : {array-like}, shape = [n_samples, n_features]
            n_sample개의 샘플과 n_features개의 특성으로 이루어진 훈련 데이터
        y : array-like, shape = [n_samples]
            타켓 값

        반환값
        ----------
        self : object
        """
        rgen = np.random.RandomState(self.random_state)
        self.w_ = rgen.normal(loc = 0.0, scale = 0.01, size = 1 + X.shape[1])
        self.cost_ = []

        "최종입력 -> 활성화 -> 출력 순으로 진행"
        for i in range(self.n_iter):
            net_input = self.net_input(X)               #"최종 입력 계산"
            output = self.activation(net_input)         #"선형 활성화 계산"
            errors = ( y - output )                     #"오차 계산 (실제 값 - 예측 값)"
            self.w_[1:] += self.eta * X.T.dot(errors)   #"배열의 T 속성 : 2차원 배열의 전치(transpose) 연산이며 행과 열을 바꾸는 작업이다."
                                                        #"가중치 변화량 : 학습률 * 비용 함수의 기울기"
                                                        #"X.T.dot(errors) : 특성 행렬과 오차 백터 간의 행렬-백터 곱"
            self.w_[0] += self.eta * errors.sum()       #"절편에 errors수를 더한다."
            cost = (errors ** 2).sum() / 2.0            #"비용 함수 =  ½Σ(실제 값 - 계산한 값)^2"
            self.cost_.append(cost)                     #"현제 시도에서의 비용 함수 값 저장"
        return self

    "최종 입력 계산"
    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    "선형 활성화 계산, "
    def activation(self, X):
        return X    #"선형 활성화 단계를 보여주기 위함"

    "단위 계단 함수를 사용하여 클레스 레이블을 반환"
    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, -1)

"pandas 라이브러리를 사용하여 UCI 머신 러닝 저장소에서 붓꽃 데이터 셋을 로드"
"df = pd.read_csv('https://archive.ics.uci.edu/ml' 'machine-learning-databases/iris/iris.data', header = None)"
df = pd.read_csv('../iris.data', header = None)

"100개의 훈련 샘플에서 첫 번째 열과 세 번째 열을 추출하여 특성 행렬 X에 저장"
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

X = df.iloc[0:100, [0, 2]].values

plt.scatter(X[:50, 0], X[:50, 1], color = 'red', marker = 'o', label = 'setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color = 'blue', marker = 'x', label = 'versicolor')
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc = 'upper left')
plt.show()

"Adaline 알고리즘 훈련"
"epoch 대비 잘못 분류된 오차를 그래프로 그린다"
fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (10, 4))
ada1 = AdalineGD(eta = 0.01, n_iter=10).fit(X, y)
ax[0].plot(range(1, len(ada1.cost_) + 1), np.log10(ada1.cost_), marker = 'o')
ax[0].set_xlabel('Epochs')
ax[0].set_ylabel('log(Sum-squared-error)')
ax[0].set_title('Adaline - Leaning rate 0.01')

ada2 = AdalineGD(eta = 0.00001, n_iter=10).fit(X, y)
ax[1].plot(range(1, len(ada2.cost_) + 1), np.log10(ada2.cost_), marker = 'x')
ax[1].set_xlabel('Epochs')
ax[1].set_ylabel('log(Sum-squared-error)')
ax[1].set_title('Adaline - Leaning rate 0.00001')
plt.show()
"학습률이 너무 크면 비용 전역 최소값을 지나치키 떄문에 함수를 최소화 하지 못하고 비용을 매 시도 마다 커진다."
"학습률이 너무 작으면 전역 최소값에 수렴하기 위해서는 많은 시도가 필요하다."

"데이터 표준화 ( 특성 스케일을 조정 하기 위해 )"
"경사 하강법은 특성 스케일을 조정하여 빠르게 전역 최소값으로 수렴하도록 할 수 있다."
"X_std : 표준화된 데이터"
X_std = np.copy(X)
X_std[:, 0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X_std[:, 1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()

"데이터셋의 결정 경계 시각화"
def plot_decision_regions(S, y, classifier, resolution = 0.02):
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    x1_min, x1_max = S[:, 0].min() - 1, S[:, 0].max() + 1
    x2_min, x2_max = S[:, 1].min() - 1, S[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))

    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha = 0.3, cmap = cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x = S[y == cl, 0], y = S[y == cl, 1], alpha=0.8, c=colors[idx], marker=markers[idx], label=cl, edgecolors='black')

ada3 = AdalineGD(eta = 0.01, n_iter=15)
ada3.fit(X_std, y)

plot_decision_regions(X_std, y, classifier = ada3)
plt.title('Adaline - Gradient Descent')
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
plt.legend(loc = 'upper left')
plt.tight_layout()
plt.show()

plt.plot(range(1, len(ada3.cost_) + 1), np.log10(ada3.cost_), marker = 'o')
plt.xlabel('Epochs')
plt.ylabel('Sum-squared-error')
plt.show()


import motor.motor_asyncio
from decouple import config

def mongo_client():
    database_url = config("DATABASE_URL")
    return motor.motor_asyncio.AsyncIOMotorClient(database_url).lightning

db = mongo_client()

def mongo_query(f):
    async def decorator(*args, **kwargs):
        result = await f(*args, **kwargs)
        return result or {}

    return decorator

@mongo_query
async def config_guild(guild_id, channel_id):
    return await db.guild_config.update_one(
        {"guild_id": guild_id},
        {
            "$set": {
                "guild_id": guild_id,
                "channel_id": channel_id,
            }
        },
        upsert=True,
    )

async def get_guild_config(guild_id):
    return await db.guild_config.find_one({"guild_id": guild_id})

@mongo_query
async def check_in_progress_lightning_talk(guild_id):
    return await db.lightning_talks.find_one(
        {
            "guild_id": guild_id,
            "in_progress": True,
        }
    )

@mongo_query
async def create_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.insert_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
            "speakers_queue": [],
            "speakers": {},
            "in_progress": True,
            "open_registration": True,
            # TODO: created_at, updated_at
        }
    )

@mongo_query
async def close_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.update_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
        },
        {
            "$set": {"open_registration": False},
        },
    )

@mongo_query
async def close_lightning_talk(guild_id, message_id):
    return await db.lightning_talks.update_one(
        {
            "guild_id": guild_id,
            "message_id": message_id,
        },
        {
            "$set": {"open_registration": False},
        },
    )

@mongo_query
async def set_speakers_order(lt, speakers):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$set": {"speakers": speakers},
        },
    )

@mongo_query
async def add_speaker_to_queue(lt, user):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$push": {"speakers_queue": user},
        },
    )

@mongo_query
async def remove_speaker_from_queue(lt, user):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$pull": {"speakers_queue": user},
        },
    )

@mongo_query
async def invite_speaker(lt, user_id, invite_message_id):
    lt["speakers"][str(user_id)]["invited"] = True
    lt["speakers"][str(user_id)]["invite_message_id"] = invite_message_id
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {
            "$set": {"speakers": lt["speakers"]},
        },
    )

@mongo_query
async def finish_lightning_talk(lt):
    return await db.lightning_talks.update_one(
        {
            "guild_id": lt["guild_id"],
            "message_id": lt["message_id"],
        },
        {"$set": {"in_progress": False}},
    )


# Lưu tên các đảng phái chính trị
#name, full_name, image, object_id, weight, keywords

from api.models.nodes import NodeObject
from connect import neo4j_connect

def create_party(partys):
    party = NodeObject("Party")
    party.name = partys.get('name')
    print(party.name)
    party.full_name = partys.get("full_name")
    print(party.full_name)
    party.image = partys.get("image")
    party.object_id = partys.get("object_id")
    party.weight = partys.get("weight")
    party.keywords = party.get("keywords")
    # print(party.keywords)
    graph = neo4j_connect()
    graph.create(party)
    graph.push(party)
    return party.__node__

def update_party(partys, key):
    party = NodeObject("Party")
    party.name = key
    print(party.name)
    party.full_name = partys.get("full_name")
    print(party.full_name)
    party.image = partys.get("image")
    party.object_id = partys.get("object_id")
    party.weight = partys.get("weight")
    party.keywords = partys.get("keywords")
    # print(party.keywords)
    graph = neo4j_connect()
    graph.nodes.match("Party")
    graph.merge(party)
    graph.push(party)
    return party.__node__

def find_all_party():
    graph = neo4j_connect()
    return list(graph.nodes.match("Party"))

def find_party_with_key(key):
    graph = neo4j_connect()
    party = graph.nodes.match("Party").where("_.name = '{name}'".format(name=key)).first()
    graph.exists(party)
    return party

def delete_party(key):
    graph = neo4j_connect()
    party = graph.nodes.match("Party").where("_.name = '{name}'".format(name=key)).first()
    graph.delete(party)
    graph.exists(party)
    # graph.push(party)
    return ("Success")


perguntas = {
    'Pergunta 1': {
        'pergunta': 'Quanto é 2+2? ',
        'respostas': {'a': '1', 'b': '4', 'c': '5', },
        'resposta_certa': 'b',
    },
    'Pergunta 2': {
        'pergunta': 'Quanto é 3*2? ',
        'respostas': {'a': '4', 'b': '10', 'c': '6', },
        'resposta_certa': 'c',
    },
}

print()

respostas_certas = 0
for pk, pv in perguntas.items():  # acessando as chaves (pergunta 1, pergunta 2) e os valores
    print(f'{pk}: {pv["pergunta"]}')

    print('Respostas: ')
    for rk, rv in pv['respostas'].items():  # 'respostas' tem um dicionário, estamos acessando ele
        print(f'[{rk}]: {rv}')

    resposta_user = input('Qual a resposta? ')

    if resposta_user == pv['resposta_certa']:
        print("Você acertou!!")
        respostas_certas += 1
    else:
        print("Você errou...")

    print()

qtd_perguntas = len(perguntas)
porc = respostas_certas / qtd_perguntas * 100

print(f'Você acertou {respostas_certas} respostas.')
print(f'Sua porcentagem de acerto foi de {porc}%.')


# -*- coding: utf-8 -*-
"""
Created on Sun Dec 10 13:15:12 2017

@author: abaena
"""

#******************************************************************************
#Add logmapper-agent directory to python path for module execution
#******************************************************************************
if __name__ == '__main__':
    import os, sys
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..','..')))
#******************************************************************************

import logging
import threading
from enum import Enum
import datetime
import time

import logmappercommon.utils.logmapper_util as lmutil

logger = logging.getLogger(__name__)

#%%
#==============================================================================
#
#==============================================================================

class ThreadState(Enum):
    CREATED = 0
    STARTING = 1
    PROCESSING = 2
    IDLE = 3
    ENDING=4
    ENDED=5
    ERROR = 99

class OperationMode(Enum):
    NORMAL = 0
    TEST = 1

class ActionState(Enum):
    IDLE = 0
    PENDING = 1
    EXECUTING = 2

class LogMapperThread(threading.Thread):
    """
    ===========================================================================
    Thread Class for general purpose
    ===========================================================================

    **Methods**:
        start: (from Thread): Start Thread. Call run method
        stopRun: safe stop of the thread
    **Attributes**:
        None
    """
    def __init__(self, category, key, operationMode = OperationMode.NORMAL):
        threading.Thread.__init__(self)
        self.setName(category+key)
        self.key = key
        self.operationMode = operationMode
        self._stopRun = False
        self._state = ThreadState.CREATED
        self.errorDetail = None
        self._action = None
        self._actionState = ActionState.IDLE
        self.startDate = None
        self.endDate = None
        self.loopCount = 0
        self.loopCountBefore = 0
        self.bytesProcessed = 0
        self.bytesProcessedBefore = 0
        self.recordsProcessed = 0
        self.recordsProcessedBefore = 0
        self.fails = 0
        self.failsBefore = 0
        self.currentDate = None
        self.nextSaveDataDate = None
        self.enableDataSend = True

    def getCounters(self):
        d = {
                'count' : self.loopCount - self.loopCountBefore,
                'bytes' : self.bytesProcessed - self.bytesProcessedBefore,
                'records' : self.recordsProcessed - self.recordsProcessedBefore,
                'fails' : self.fails - self.failsBefore,
                }
        self.loopCountBefore = self.loopCount
        self.bytesProcessedBefore = self.bytesProcessed
        self.recordsProcessedBefore = self.recordsProcessed
        self.failsBefore = self.fails
        return d

    def stopRun(self):
        """
        Ask thread end safely
        """
        logger.debug("stopRun:"+self.name)
        self._stopRun = True

    def setState(self, state):
        logger.debug("setState:"+self.name+':'+str(state))
        self._state = state

    def getState(self):
        logger.debug("getState:"+self.name+':'+str(self._state))
        return self._state

    def setStateError(self, errorDetail):
        logger.error("setStateError:"+errorDetail)
        self.setState(ThreadState.ERROR)
        self.errorDetail = errorDetail

    def startAction(self, action):
        logger.debug("setAction:"+action)
        if self._actionState != ActionState.IDLE:
            return "BUSY"
        self._action = action
        self._actionState = ActionState.PENDING
        return "OK"

    def getActionState(self):
        logger.debug("getActionState:"+str(self._actionState))
        return (self._action, self._actionState)

    def run(self):
        logger.debug ("Start Thread:" + self.name)
        try:
            self.setState(ThreadState.STARTING)
            self.startDate=datetime.datetime.now()
            self.currentDate = datetime.date.today()
            self.nextSaveDataDate = lmutil.getNextLogMapperIntervalDate(self.startDate)
            self.process()
            self.setState(ThreadState.ENDED)
            self.endDate = datetime.datetime.now()
        except Exception as exc:
            self.setStateError(str(exc))
            logger.exception("Exception in "+self.name)

        logger.debug ("End Thread:" + self.name)

    def process(self):
        logger.debug("process:")

        self.setState(ThreadState.PROCESSING)
        while not self._stopRun:
            self.loopCount += 1
            time.sleep(10)
        self.setState(ThreadState.ENDING)

#%%
#==============================================================================
#
#==============================================================================

def getThread(name):
    """
    ===========================================================================
    Search in All Active Threads. Return the Thread Class with the same name
    ===========================================================================

    **Args**:
        name (str): name of the thread that is looking
    **Returns**:
        Thread object. None if it is not found
    """
    logger.debug('getThread:'+name)
    for t in threading.enumerate():
        if t.getName() == name:
           return t
    return None

#%%
"""
*******************************************************************************
Module Execution
This code helps to developer to know the usage of the module
*******************************************************************************
"""
if __name__ == '__main__':
    print('Start module execution:')
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%H:%M:%S')


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.contrib.auth.models import Group
from zope.interface import named

class CustomUser(AbstractUser):
    ROLE = [
        (1, "Regular"),
        (2, "Administrator"),
    ]
    phone = models.CharField(max_length=10)
    role = models.PositiveSmallIntegerField(choices=ROLE, default=1)

@receiver(signal=post_save, sender=CustomUser)
def user_created(sender, instance, created, **kwargs):
    reg, created = Group.objects.get_or_create(name="Regular")
    admin, created = Group.objects.get_or_create(name="Administrator")
    if created:
        if instance.role == 1:
            print("Regular")
            reg.user_set.add(instance)
        else:
            print("Admin")
            admin.user_set.add(instance)
    else:
        if instance.role == 1:
            admin.user_set.remove(instance)
            reg.user_set.add(instance)
        else:
            reg.user_set.remove(instance)
            admin.user_set.add(instance)


# Databricks notebook source
# MAGIC %md
# MAGIC The New York City Taxi and Limousine Commission (TLC) provides a [dataset](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page) of trips taken by taxis and for-hire vehicles in New York City.
# MAGIC
# MAGIC AWS hosts this dataset in an S3 bucket in the `us-east-1` region (more information [here]( https://registry.opendata.aws/nyc-tlc-trip-records-pds/)).
# MAGIC
# MAGIC Within this dataset, there are records for Yellow taxis, Green taxis, and for-hire vehicles (e.g. Uber, Lyft).
# MAGIC
# MAGIC For our purposes, we are interested in the records that have geospatial coordinates associated.  This includes:
# MAGIC - Yellow trip data from January 2009 through June 2016
# MAGIC - Green trip data from August 2013 through June 2016
# MAGIC - (The FHV trip data does not include coordinates)

# COMMAND ----------

# MAGIC %run ./setup

# COMMAND ----------

# DBTITLE 1,Configure Paths
BASE_PATH_DBFS = f"{path}/nyctaxi/green_tripdata"
SCHEMA_V1_RAW_PATH = BASE_PATH_DBFS + "/schema_v1"
SCHEMA_V2_RAW_PATH = BASE_PATH_DBFS + "/schema_v2"
BRONZE_PATH = "dbfs:" + BASE_PATH_DBFS + "/bronze"

# COMMAND ----------

dbutils.fs.mkdirs(SCHEMA_V1_RAW_PATH)
dbutils.fs.mkdirs(SCHEMA_V2_RAW_PATH)

# COMMAND ----------

with open("schema_v1_path.txt", "w") as file:
  file.write("/dbfs" + SCHEMA_V1_RAW_PATH)

with open("schema_v2_path.txt", "w") as file:
  file.write("/dbfs" + SCHEMA_V2_RAW_PATH)

# COMMAND ----------

# MAGIC %sh
# MAGIC
# MAGIC path=`cat schema_v1_path.txt`
# MAGIC
# MAGIC cat <<EOT >> green_tripdata_v1.txt
# MAGIC green_tripdata_2013-08.csv
# MAGIC green_tripdata_2013-09.csv
# MAGIC green_tripdata_2013-10.csv
# MAGIC green_tripdata_2013-11.csv
# MAGIC green_tripdata_2013-12.csv
# MAGIC green_tripdata_2014-01.csv
# MAGIC green_tripdata_2014-02.csv
# MAGIC green_tripdata_2014-03.csv
# MAGIC green_tripdata_2014-04.csv
# MAGIC green_tripdata_2014-05.csv
# MAGIC green_tripdata_2014-06.csv
# MAGIC green_tripdata_2014-07.csv
# MAGIC green_tripdata_2014-08.csv
# MAGIC green_tripdata_2014-09.csv
# MAGIC green_tripdata_2014-10.csv
# MAGIC green_tripdata_2014-11.csv
# MAGIC green_tripdata_2014-12.csv
# MAGIC EOT
# MAGIC
# MAGIC while read file; do
# MAGIC   wget -P $path http://s3.amazonaws.com/nyc-tlc/trip%20data/$file
# MAGIC done <green_tripdata_v1.txt

# COMMAND ----------

# MAGIC %sh
# MAGIC
# MAGIC path=`cat schema_v2_path.txt`
# MAGIC
# MAGIC cat <<EOT >> green_tripdata_v2.txt
# MAGIC green_tripdata_2015-01.csv
# MAGIC green_tripdata_2015-02.csv
# MAGIC green_tripdata_2015-03.csv
# MAGIC green_tripdata_2015-04.csv
# MAGIC green_tripdata_2015-05.csv
# MAGIC green_tripdata_2015-06.csv
# MAGIC green_tripdata_2015-07.csv
# MAGIC green_tripdata_2015-08.csv
# MAGIC green_tripdata_2015-09.csv
# MAGIC green_tripdata_2015-10.csv
# MAGIC green_tripdata_2015-11.csv
# MAGIC green_tripdata_2015-12.csv
# MAGIC green_tripdata_2016-01.csv
# MAGIC green_tripdata_2016-02.csv
# MAGIC green_tripdata_2016-03.csv
# MAGIC green_tripdata_2016-04.csv
# MAGIC green_tripdata_2016-05.csv
# MAGIC green_tripdata_2016-06.csv
# MAGIC EOT
# MAGIC
# MAGIC while read file; do
# MAGIC   wget -P $path http://s3.amazonaws.com/nyc-tlc/trip%20data/$file
# MAGIC done <green_tripdata_v2.txt

# COMMAND ----------

schema_v1 = StructType([
  StructField("VendorID",IntegerType(),True),
  StructField("lpep_pickup_datetime",TimestampType(),True),
  StructField("Lpep_dropoff_datetime",TimestampType(),True),
  StructField("Store_and_fwd_flag",StringType(),True),
  StructField("RateCodeID",IntegerType(),True),
  StructField("Pickup_longitude",DoubleType(),True),
  StructField("Pickup_latitude",DoubleType(),True),
  StructField("Dropoff_longitude",DoubleType(),True),
  StructField("Dropoff_latitude",DoubleType(),True),
  StructField("Passenger_count",IntegerType(),True),
  StructField("Trip_distance",DoubleType(),True),
  StructField("Fare_amount",DoubleType(),True),
  StructField("Extra",DoubleType(),True),
  StructField("MTA_tax",DoubleType(),True),
  StructField("Tip_amount",DoubleType(),True),
  StructField("Tolls_amount",DoubleType(),True),
  StructField("Ehail_fee",StringType(),True),
  StructField("Total_amount",DoubleType(),True),
  StructField("Payment_Type",IntegerType(),True),
  StructField("Trip_type",IntegerType(),True),
])

green_tripdata_v1 = (spark.read
                     .format("csv")
                     .option("header", True)
                     .schema(schema_v1)
                     .load(f"dbfs:{SCHEMA_V1_RAW_PATH}/*.csv")
                     .write
                     .format("delta")
                     .mode("overwrite")
                     .save(BRONZE_PATH)
                    )

spark.sql("CREATE TABLE green_tripdata_bronze USING DELTA LOCATION '" + BRONZE_PATH + "'")

# COMMAND ----------

schema_v2 = StructType([
  StructField("VendorID",IntegerType(),True),
  StructField("lpep_pickup_datetime",TimestampType(),True),
  StructField("Lpep_dropoff_datetime",TimestampType(),True),
  StructField("Store_and_fwd_flag",StringType(),True),
  StructField("RateCodeID",IntegerType(),True),
  StructField("Pickup_longitude",DoubleType(),True),
  StructField("Pickup_latitude",DoubleType(),True),
  StructField("Dropoff_longitude",DoubleType(),True),
  StructField("Dropoff_latitude",DoubleType(),True),
  StructField("Passenger_count",IntegerType(),True),
  StructField("Trip_distance",DoubleType(),True),
  StructField("Fare_amount",DoubleType(),True),
  StructField("Extra",DoubleType(),True),
  StructField("MTA_tax",DoubleType(),True),
  StructField("Tip_amount",DoubleType(),True),
  StructField("Tolls_amount",DoubleType(),True),
  StructField("Ehail_fee",StringType(),True),
  StructField("improvement_surcharge",DoubleType(),True), # This is a new column!
  StructField("Total_amount",DoubleType(),True),
  StructField("Payment_Type",IntegerType(),True),
  StructField("Trip_type",IntegerType(),True),
])

green_tripdata_v2 = (spark.read
                     .format("csv")
                     .option("header", True)
                     .schema(schema_v2)
                     .load(f"dbfs:{SCHEMA_V2_RAW_PATH}/*.csv")
                     .write
                     .format("delta")
                     .mode("append")
                     .option("mergeSchema", "true")
                     .save(BRONZE_PATH)
                    )

# COMMAND ----------

spark.conf.set("spark.databricks.io.skipping.mdc.curve", "hilbert")

# COMMAND ----------

spark.sql(f"OPTIMIZE {dbName}.green_tripdata_bronze ZORDER BY Pickup_latitude, Pickup_longitude")

# COMMAND ----------

display(spark.sql(f"SELECT count(*) FROM {dbName}.green_tripdata_bronze"))

# COMMAND ----------

display(spark.sql(f"SELECT count(*) FROM {dbName}.green_tripdata_bronze WHERE improvement_surcharge IS null"))

# COMMAND ----------

display(spark.sql(f"SELECT count(*) FROM {dbName}.green_tripdata_bronze WHERE improvement_surcharge IS NOT null"))

sessions/2021-12-14 | Geospatial Analytics and AI at Scale/notebooks/resources/load_data_greentaxi.py


import numpy as np, matplotlib.pyplot as plt
import pumpprobe as pp

# DATASETS WITH STIMULATION OF RID
ds_list = [
    "/projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_101248/",
    "/projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_152524/"
    ]

funa = pp.Funatlas.from_datasets(
                ds_list,
                merge_bilateral=True,merge_dorsoventral=False,merge_AWC=True)

occ1, occ2 = funa.get_occurrence_matrix()
occ3 = funa.get_observation_matrix()
ai_RID = funa.ids_to_i("RID")

fnames = ["external_data/GenesExpressing-npr-4-thrs2.csv",
          "external_data/GenesExpressing-npr-11-thrs2.csv",
          "external_data/GenesExpressing-pdfr-1-thrs2.csv",
          "external_data/GenesExpressing-daf-2-thrs2.csv"]

trans_rec_pairs = ["FLP-14,NPR-4",
                   "FLP-14,NPR-11",
                   "PDF-1,PDFR-1",
                   "INS-17,DAF-2"]

trans_exp_level = np.array([110634.0,110634.0,157972.0,1505.0])

'''excl_neu = [
    # Exclude neurons that are never observed
    "AIA_","RIB_","RIR_","RIF_","AUA_","RIR_","RMG_","RMF_","AIY_","ADF_","ALA","AVB_","RIS_","AVF_","RMH_","RIP_"
    # and that have not been observed in the recordings that I am using
    "ASK_","BAG_","RIC_","RIS_","ADE_","URB_","MC_",
    ]'''

x = np.arange(len(funa.head_ai))
exp_levels = np.zeros((len(fnames),len(funa.head_ai)))
for i_f in np.arange(len(fnames)):
    f = open(fnames[i_f],"r")
    lines = f.readlines()
    f.close()

    exp_levels_ = np.zeros(funa.n_neurons)

    for line in lines[1:]:
        s = line.split(",")
        cell_id = s[1][1:-1]
        exp_level = float(s[2])

        ai = funa.ids_to_i(cell_id)
        exp_levels_[ai] = exp_level*trans_exp_level[i_f]

    b = funa.reduce_to_head(exp_levels_)
    exp_levels[i_f] = b

# Sort them by the sum of the expression levels
tot_exp_levels = np.sum(exp_levels,axis=0)
bargsort = np.argsort(tot_exp_levels)[::-1]
lim = np.where(tot_exp_levels[bargsort]==0)[0][0]

fig = plt.figure(1,figsize=(16,9))
ax = fig.add_subplot(111)
x = np.arange(len(funa.head_ai))

prev_b = np.zeros(len(funa.head_ai))
for i_f in np.arange(len(fnames)):
    b = exp_levels[i_f][bargsort]

    # Set to zero the neurons that are never observed
    for i_hn in np.arange(len(funa.head_ids)):
        id_hn = str(funa.head_ids[bargsort][i_hn])
        ai_hn = funa.ids_to_i(id_hn)
        if occ3[ai_hn,ai_RID]==0:
            b[i_hn] = 0

    ax.bar(x,b,label=trans_rec_pairs[i_f],bottom=prev_b)
    prev_b += b

#ax.set_xticks(x[:lim])
ax.set_xticks(x)
#ax.set_xticklabels(funa.head_ids[bargsort][:lim],rotation=90,fontsize=9)
ax.set_xticklabels(funa.head_ids[bargsort],rotation=90,fontsize=9)
#ax.set_xlim(-1,lim)
ax.set_ylabel("CeNGEN transmitter count in RID * receptor counts in downstream neurons")
ax.set_title("\"Expected\" amplitude of reponse based on receptor and transmitter \n"+\
             "expression count from CeNGEN (transmitter,receptor)")
ax.legend()

# Manually compiled downstream neurons
'''downst_in_data = [
    # From the first 2D measurement (look it up, it's in the targeting tests)
    "AVD_",
    # From /projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_101248/
    # stim1
    "AVAL","AVEL","RMDVL","AVDL","AWBL","AVER","OLQD",
    # stim2
    "AVAL","SMDDL","RMDL","AVEL","RMDVL","SMBDL","AVDL","RMDDL","AWBL","RIG","RMDD_","CEPDL","AIMR","RIMR","OLQD","AVJR",
    # From /projects/LEIFER/francesco/pumpprobe/AKSxneuropal/20211102/pumpprobe_20211102_152524/
    # stim 1 (note that some are inhibitory here, but were excitatory in the other recording. and some pairs are one exc and one inh, like AVE)
    "RMDVL","AVDL","AVJL","M2L","AVEL","I2L","RIG","RIG","AIBL","AVER","RMDVR","AVDR",
    # stim 2 (here both AVER and AVEL are inh)
    "AVDL","M2L","RMEL","AVEL","RIG","IL2VL","AIBL","RIVL","M1","URYDR","AVER","AIBR","RMDR","RMDVR","AVDR",
    ]

downst_in_data = funa.approximate_ids(downst_in_data,merge_bilateral=True,merge_dorsoventral=False,merge_AWC=True)
downst_in_data, downst_in_data_counts = np.unique(downst_in_data,return_counts=True)'''

downst_in_data_ai = np.where(occ1[:,ai_RID]>0)[0]

downst_in_data_ai_head = funa.ai_to_head(downst_in_data_ai)
exp_bars = np.zeros_like(tot_exp_levels)
exp_bars[downst_in_data_ai_head] = occ1[downst_in_data_ai,ai_RID]/occ3[downst_in_data_ai,ai_RID]
exp_bars = exp_bars[bargsort]
new_lim = np.max(np.where(exp_bars!=0)[0])

axb = ax.twinx()
#axb.bar(x,exp_bars,color="#9467bd",alpha=0.6,label="response count")
h_ais_barg = funa.head_ai[bargsort]
for i in np.arange(exp_bars.shape[0]):
    if occ3[h_ais_barg[i],ai_RID]>0:
        axb.plot((i,i),(0,1),c="purple",marker="x",alpha=0.3)
    if exp_bars[i]>0:
        if i==0:
            axb.plot((i,i),(0,exp_bars[i]),c="k",marker="x",label="fractional response count")
        else:
            axb.plot((i,i),(0,exp_bars[i]),c="k",marker="x")

#if new_lim>lim:
    #ax.set_xlim(-1,new_lim+1)
    #ax.set_xticks(x[:new_lim+1])
    #new_ticklabels = funa.head_ids[bargsort][:new_lim+1].copy()
    #ax.set_xticklabels(new_ticklabels,rotation=90,fontsize=9)
axb.set_ylim(0,)
axb.set_ylabel("Fraction of times neurons responded in experiments")
axb.legend(loc=5)

fig.tight_layout()
plt.show()


try:
    with open("Table.txt","+a") as f:
        n = int(input("Enter no of tables:"))
        for i in range(1,n+1):
            f.write(f"\n{i}'s Table\n-----------------\n")
            for j in range(1,11):
                f.write(f"{i} * {j} = {i*j}\n")
            f.write("------end-------")
except ValueError:
    print("Enter number only")