"""Module to count device connection statistics (port numbers, port speeds, virtual cahnnel usage)
and add notes if some criteria are not fullfilled"""

import numpy as np
import pandas as pd

from .device_connection_statistics_notes import add_notes

unique_vc_str ='Unique_VC_quantity_'
bandwidth_str = 'Bandwidth_'
device_port_quantity_str = 'Device_port_quantity_'
unique_slot_str = 'Unique_switch_slot_quantity_'
unique_port_speed_str = 'Unique_port_speed_quantity_'
director_str = 'director_quantity'
switch_str = 'switch_quantity'

columns_str = [unique_vc_str, unique_slot_str, unique_port_speed_str, bandwidth_str, director_str]

def device_connection_statistics(portshow_aggregated_df):
    """Main function to count device connection statistics"""

    portshow_aggregated_modified_df, fabric_labels_lst = prior_preparation(portshow_aggregated_df)
    portshow_vc_unique_df, portshow_switch_unique_df, portshow_slot_unique_df, portshow_port_speed_unique_df = unique_values(portshow_aggregated_modified_df)
    # count statistcics for ports quantity, ports speed, ports vc and device unique vc statistics for each device
    device_connection_statistics_df = count_device_connection_statistics(portshow_aggregated_modified_df, portshow_vc_unique_df, portshow_switch_unique_df,
                                                                            portshow_slot_unique_df, portshow_port_speed_unique_df)
    # add device bandwidth in each Fabric_label
    device_connection_statistics_df = count_device_bandwidth(device_connection_statistics_df, portshow_aggregated_modified_df)
    # add notes
    device_connection_statistics_df = add_notes(device_connection_statistics_df, fabric_labels_lst, columns_str)

    # drop fabric_labels columns and remove 'Unknown' tag from 'Device_Location'
    device_connection_statistics_df.drop(columns=fabric_labels_lst, inplace=True)
    device_connection_statistics_df['Device_Location'] = device_connection_statistics_df['Device_Location'].replace(['Unknown'], np.nan)

    # change columns order
    stat_columns = device_connection_statistics_df.columns.tolist()
    device_columns = ['Fabric_name', 'Device_Host_Name', 'Device_Location', 'deviceType', 'deviceSubtype']
    reorder_stat_columns = [*device_columns, *stat_columns[5:]]
    device_connection_statistics_df = device_connection_statistics_df[reorder_stat_columns]
    device_connection_statistics_df.fillna(np.nan, inplace=True)
    return device_connection_statistics_df

def prior_preparation(portshow_aggregated_df):
    """Function to filter required data from portshow_aggregated_df DataFrame
    to count statistics"""

    # AG mode switches dropped to avoid duplicate  connection information
    mask_switch_native = portshow_aggregated_df['switchMode'] == 'Native'
    # drop Switches and Virtual connect modules
    mask_not_switch_vc = ~portshow_aggregated_df['deviceType'].isin(['SWITCH', 'VC'])
    # device class has to be defined
    mask_devicetype = portshow_aggregated_df['deviceType'].notna()
    # drop fabrics which are not part of assessment
    mask_fabric_name_label = portshow_aggregated_df[['Fabric_name', 'Fabric_label']].notna().all(axis=1)
    # drop fabrics which are out of assessment scope
    mask_valid_fabric = ~portshow_aggregated_df['Fabric_name'].isin(['x', '-'])

    mask_complete = mask_fabric_name_label & mask_switch_native & mask_not_switch_vc & mask_devicetype & mask_valid_fabric
    columns_lst = ['Fabric_name', 'deviceType',	'Device_Location', 'deviceSubtype', 'Device_Host_Name',
                   'Fabric_label', 'speed', 'Virtual_Channel', 'switchWwn', 'switchType' , 'switchClass', 'slot']
    portshow_aggregated_modified_df = portshow_aggregated_df.loc[mask_complete, columns_lst].copy()

    # assign 'unknown' tag for na Device_Location for grouping and crosstab operations
    portshow_aggregated_modified_df['Device_Location'].fillna('Unknown', inplace=True)
    # set 'Virtual_Channel', 'speed' with Fabric_label tag
    portshow_aggregated_modified_df['Virtual_Channel'] = \
        portshow_aggregated_modified_df['Fabric_label'] + '_' + portshow_aggregated_modified_df['Virtual_Channel']
    portshow_aggregated_modified_df['speed'] = \
        portshow_aggregated_modified_df['Fabric_label'] + '_' + portshow_aggregated_modified_df['speed']
    # set column name tag for Fabric_labels for later port number count
    portshow_aggregated_modified_df['Fabric_connection'] = \
        device_port_quantity_str + portshow_aggregated_modified_df['Fabric_label']

    # director or switch connection tag
    mask_director = portshow_aggregated_modified_df['switchClass'] == 'DIR'
    portshow_aggregated_modified_df['switchType'] = np.where(mask_director, director_str , switch_str)
    portshow_aggregated_modified_df['switchType'] = portshow_aggregated_modified_df['Fabric_label'] + '_' + portshow_aggregated_modified_df['switchType']

    # extract speed values
    portshow_aggregated_modified_df['speed_extract'] = portshow_aggregated_modified_df['speed'].str.extract(r'(\d+)')

    # fabric_labels used in Fabric
    fabric_labels_lst = portshow_aggregated_modified_df['Fabric_label'].unique()
    fabric_labels_lst.sort()
    return portshow_aggregated_modified_df, fabric_labels_lst

def unique_values(portshow_modified_df):
    """Function to create DataFrames to count for each device unique VC quantity,
    unique switch type connected quantity (combination of switchWwn and swicthType),
    unique switch slot connection quantity (combination of switchWwn and slot number),
    unique portspeed quantity"""

    # create dataFrames count to count quantity of unique VC, switcName, slot number
    columns = ['Fabric_name', 'deviceType', 'Device_Location', 'deviceSubtype','Device_Host_Name', 'Fabric_label']

    # create DataFrame with unique VC for each device only to verify if Virtual channel divercitu present for each device
    unique_vc_columns = columns + ['Virtual_Channel']
    portshow_vc_unique_df = portshow_modified_df[unique_vc_columns].copy()
    portshow_vc_unique_df.drop_duplicates(inplace=True)
    # portshow_vc_unique_df = portshow_modified_df.drop_duplicates().copy()
    portshow_vc_unique_df['Unique_Virtual_Channel'] = unique_vc_str +  portshow_vc_unique_df['Fabric_label']

    # create DataFrame with unique switchWwn and director, swicth tags
    # to verify how many switches divice connected to and its type (direcotr, switch)
    unique_switch_columns = columns + ['switchWwn', 'switchType']
    portshow_switch_unique_df = portshow_modified_df[unique_switch_columns].copy()
    portshow_switch_unique_df.drop_duplicates(inplace=True)

    # create DataFrame with unique slot number to verify if slot diversity present for each device
    unique_slot_columns = columns + ['switchWwn', 'slot']
    portshow_slot_unique_df = portshow_modified_df[unique_slot_columns].copy()
    portshow_slot_unique_df.drop_duplicates(inplace=True)
    portshow_slot_unique_df['Unique_switch_slot'] = unique_slot_str +  portshow_slot_unique_df['Fabric_label']

    # create DataFrame with unique port speed to verify if speed diversity present for each device
    unique_slot_columns = columns + ['speed_extract']
    portshow_port_speed_unique_df = portshow_modified_df[unique_slot_columns].copy()
    portshow_port_speed_unique_df.drop_duplicates(inplace=True)
    portshow_port_speed_unique_df['Unique_port_speed'] = unique_port_speed_str +  portshow_port_speed_unique_df['Fabric_label']
    return portshow_vc_unique_df, portshow_switch_unique_df, portshow_slot_unique_df, portshow_port_speed_unique_df

def count_device_connection_statistics(portshow_aggregated_modified_df, portshow_vc_unique_df,
                                        portshow_switch_unique_df, portshow_slot_unique_df, portshow_port_speed_unique_df):
    """Function to count ports quantity, ports speed, ports vc and device unique vc statistics for each device"""

    device_connection_statistics_df = pd.DataFrame()
    statistics_lst =  [('Fabric_connection', portshow_aggregated_modified_df),
                       ('Fabric_label', portshow_aggregated_modified_df),
                       ('speed', portshow_aggregated_modified_df),
                       ('switchType', portshow_switch_unique_df),
                       ('Virtual_Channel', portshow_aggregated_modified_df),
                       ('Unique_Virtual_Channel', portshow_vc_unique_df),
                       ('Unique_switch_slot', portshow_slot_unique_df),
                       ('Unique_port_speed', portshow_port_speed_unique_df)]

    # count statistcics for each column, DataFRame pair in statistics_lst
    for column, df in statistics_lst:
        margins_flag = True if column == 'Fabric_connection' else False
        # groupby columns
        index_lst = [df.Fabric_name,
                     df.deviceType,
                     df.Device_Location,
                     df.deviceSubtype,
                     df.Device_Host_Name]
        # count statistics for current column
        current_statistics_df = pd.crosstab(index = index_lst,
                                columns = df[column],
                                margins=margins_flag)
        # add current_statistics_df DataFrame to aggregated device_connection_statistics_df DataFRame
        if device_connection_statistics_df.empty:
            device_connection_statistics_df = current_statistics_df.copy()
        else:
            device_connection_statistics_df = device_connection_statistics_df.merge(current_statistics_df, how='left',
                                                                                left_index=True, right_index=True)
    # rename 'All' column
    device_connection_statistics_df.rename(columns={'All': device_port_quantity_str + 'Total'}, inplace=True)
    return device_connection_statistics_df

def count_device_bandwidth(device_connection_statistics_df, portshow_aggregated_modified_df):
    """Function to add total bandwidth for each device in device_connection_statistics_df DataFrame"""

    portshow_speed_df = portshow_aggregated_modified_df.copy()
    # extract speed value from 'speed' column
    portshow_speed_df['speed_extract'] = portshow_speed_df['speed'].str.extract(r'(\d+)')
    portshow_speed_df['speed_extract'] = portshow_speed_df['speed_extract'].astype('int64', errors='ignore')
    # add 'Bandwidth' tag to Fabric_label to create columns later
    portshow_speed_df['Bandwidth_label'] = bandwidth_str + portshow_speed_df['Fabric_label']

    # groupby ports for each device in Fabric_label and summarize port speeds to get device bandwidth
    grp_lst = ['Fabric_name', 'deviceType',	'Device_Location', 'deviceSubtype', 'Device_Host_Name', 'Bandwidth_label']
    bandwidth_df = portshow_speed_df.groupby(grp_lst, as_index = False)['speed_extract'].sum()
    # move 'Bandwidth_label' column values as independent column names with values from 'speed_extract' column
    bandwidth_pivot_df = bandwidth_df.pivot_table(values='speed_extract', index=grp_lst[:-1], columns='Bandwidth_label', aggfunc='first')
    # if device has no connection to Fabric_label than bandwidth is zero
    bandwidth_pivot_df.fillna(0, inplace=True)

    # add bandwidth info to device_connection_statistics_df DataFrame
    device_connection_statistics_df = device_connection_statistics_df.merge(bandwidth_pivot_df, how='left', left_index=True, right_index=True)
    device_connection_statistics_df.reset_index(inplace=True)
    return device_connection_statistics_df


#!/usr/bin/python3
import datetime
import math
import sys

import epd7in5
from PIL import Image, ImageDraw, ImageFont

import ical_worker
from config import *

#timezone = pytz.timezone(TIMEZONE)
#ical_worker.basetime = datetime.datetime.now(timezone)
#ical_worker.basetime.astimezone(timezone)

fheadline = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Light.ttf', headline_size)
ftext = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Light.ttf', text_size)
fbold = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Bold.ttf', text_size)
#fawesome = ImageFont.truetype('fa-regular.otf', text_size)

print("DAYS: {}".format(DAYS))
print("hours_day: {}".format(hours_day))
def get_drawable_events():
    print("per_hour: {}\t lost: {}".format(per_hour, height - bar_top - offset_top - offset_allday - hours_day * per_hour))
print("per_day: {}\t lost: {}".format(per_day, width - bar_left - offset_left - per_day * DAYS))
epd = epd7in5.EPD()

def prepare_grid(d):
    """ Prepares the Days X Hours grid for drawing events into it """

    # separate top bar from rest
    d.line([(offset_left, offset_top + bar_top - 1), (width, offset_top + bar_top - 1)], width=2)
    # separate all-day events from grid
    d.line([(offset_left, offset_top + bar_top + offset_allday), (width, offset_top + bar_top + offset_allday)], width=2)
    # separate the left bar from the rest
    d.line([(offset_left + bar_left -1, offset_top), (offset_left + bar_left - 1, height)], width=2)

    # draw the vertical day separators and day headlines
    for i in range(0, DAYS):
        x = offset_left + bar_left + per_day * i
        # for every but the first, draw separator to the left
        if i > 0:
            d.line([(x, offset_top), (x, height)])
        # draw date headline
        day = ical_worker.basetime + datetime.timedelta(days=i)
        headline = day.strftime('%a, %d')
        textsize_x = d.textsize(headline, fheadline)[0]
        textoffs_x = math.floor((per_day - textsize_x) / 2)
        d.text((x + textoffs_x, offset_top), headline, font=fheadline)

    # draw horizontal hour separators and hour numbers
    for i in range(0, hours_day):
        y = offset_top + bar_top + offset_allday + per_hour * i
        # for every but the first, draw separator before
        if i > 0:
            # separator = dotted line with every fourth pixel
            for j in range(offset_left, width, 4):
                d.point([(j, y)])
        # draw the hour number
        textoffs_y = math.floor((per_hour - text_size) / 2)
        d.text((offset_left, y + textoffs_y - 1), "%02d" % (BEGIN_DAY + i), font=fheadline)

    # clear the all-day events space
    d.rectangle((offset_left + bar_left + 1, offset_top + bar_left + 1, width, offset_top + bar_left + offset_allday - 1), fill=200, width=0)

def draw_short_event(d, e):
    """
    Internal function for drawing events into the grid.

    Not to be used for drawing events manually, please use draw_event for that.

    This function cannot draw events lasting across midnight. Instead, such events are split up
    into several calls of draw_short_event and draw_allday_event.

    """
    x_start = offset_left + bar_left + e["day"] * per_day + e["column"] * per_day / e["max_collision"]
    y_start = offset_top + bar_top + offset_allday + math.floor((e["start"] - (BEGIN_DAY * 60)) * per_hour / 60)
    width = per_day / e["max_collision"]
    y_end = offset_top + bar_top + offset_allday + math.floor((e["end"] - (BEGIN_DAY * 60)) * per_hour / 60)
    # clear the event's area and make the outline
    d.rectangle((x_start, y_start, x_start + width, y_end), outline=0, width=2, fill=200)

    textoffs_x = 5
    textoffs_y = (per_hour - text_size) // 2 - 1

    fulltext = e["title"]
    while d.textsize(fulltext, font=ftext)[0] > width - 2 * textoffs_x and len(fulltext) > 0:
        fulltext = fulltext[:-1]
    if e["end"] - e["start"] >= 90:
        begintext = "%02d:%02d" % (e["start"] // 60, e["start"] % 60)
        endtext = "%02d:%02d" % (e["end"] // 60, e["end"] % 60)
        datetext = "\n%s-%s" % (begintext, endtext)
        if d.textsize(datetext, font=ftext)[0] > width - 2 * textoffs_x:
           datetext = "\n%s" % begintext
        if d.textsize(datetext, font=ftext)[0] <= width - 2 * textoffs_x:
            fulltext += datetext
    d.text((x_start + textoffs_x, y_start + textoffs_y), fulltext, font=ftext)
    print(fulltext)
    #d.text((x_start + 5, y_start + text_size + textoffs_y), begintext + "-" + endtext, font=ftext)

    print(e)

def draw_allday_event(d, ev):
    """
    Internal function for drawing events that shouldn't appear in the grid.

    Not to be used for drawing events manually, please use draw_event for that.
    """
    if e["column"] >= ALLDAY_MAX:
        return
    x_start = offset_left + bar_left + e["start"] * per_day - 1
    x_end = offset_left + bar_left + e["end"] * per_day
    y_start = offset_top + bar_top + e["column"] * allday_size - 1
    width = x_end - x_start

    d.rectangle((x_start, y_start, x_end, y_start + allday_size + 2), outline=0, fill=200, width=2)

    textoffs_x = 5
    textoffs_y = (allday_size - text_size) // 2
    fulltext = e["title"]
    while d.textsize(fulltext, font=ftext)[0] > width - 2 * textoffs_x and len(fulltext) > 0:
        fulltext = fulltext[:-1]
    d.text((x_start + textoffs_x, y_start + textoffs_y), fulltext, font=ftext)

def draw_event(d, ev):
    """
    High-level function for drawing an event as it is generated by the iCal Library

    d -- the Pillow.ImageDraw Object to draw onto
    ev -- the icalendar event Object to draw
    """
    pass

if __name__ == "__main__":
    (drawables, all_days) = ical_worker.get_drawable_events()
    im = Image.new('1', (width, height), 255)
    d = ImageDraw.Draw(im)

    prepare_grid(d)
    #draw_event(d, evs[1])
    for l in drawables:
        for e in l:
            draw_short_event(d, e)
    for e in all_days:
        draw_allday_event(d, e)
    im.save(open("out.jpg", "w+"))

    epd.init()
    epd.display(epd.getbuffer(im))
    epd.sleep()


import os
# import copy

import tornado.web
from tornado import gen

from tornado_reloaded.web import RequestHandler
from tornado_reloaded.contrib.admin.modules import HeaderModule
from tornado_reloaded.contrib.admin.modules import PartialModule

class BaseHandler(RequestHandler):
    def __init__(self, application, request, spec_match, url_lang=None, **kwargs):
        application.ui_modules['HeaderModule'] = HeaderModule
        application.ui_modules['PartialModule'] = PartialModule
        super(RequestHandler, self).__init__(application, request, spec_match, url_lang, **kwargs)
        self.resource = None

    @property
    def settings(self):
        """An alias for `self.application.settings`."""
        if not hasattr(self, '_settings'):
            self._settings = {}
            for name, value in self.application.settings.items():
                self._settings[name] = value
            # self._settings = copy.deepcopy(self.application.settings)
            self._settings['template_path']         = os.path.join(os.path.dirname(__file__), '../templates/')
            self._settings['static_path']         = os.path.join(os.path.dirname(__file__), '../static/')
            self._settings['static_url_prefix']   = '/admin_static/'
        return self._settings


from django.core.management.base import BaseCommand, CommandError
from water_app.models import Getdata, Block
from django.db.models import Model

class Command(BaseCommand):
    help = 'get Data and save block'

    def add_arguments(self, parser):
        pass

    def handle(self, *args, **options):
        area_name = {'area_name'}
        area_id = {'area_id'}
        street_id = {'street_id'}
        street_name = {'street_name'}
        village_id = {'village_id'}
        village_name = {'village.name'}

        for data in Block.objects.all():
            area_id.add(data.area_id)
            area_name.add(data.area_name)
            street_id.add(data.street_id)
            street_name.add(data.street_name)
            village_id.add(data.village_id)
            village_name.add(data.village_name)

        for data in Getdata.objects.all():
            block = Block()
            block.village_id = data.committee_id
            block.village_name = data.committee_name

            block.street_id = data.region_id
            if not block.street_id:
                block.street_id = '无'

            block.street_name = data.region_name
            if not block.street_name:
                block.street_name = '无'

            block.area_id = data.piece_id
            if not block.area_id:
                # 没有片区id的话
                block.area_id = '无'

            block.area_name = data.piece_name
            if not block.area_name:
                # 没有片区名字的话
                block.area_name = '普陀区'
            if block.area_name not in area_name or block.street_name not in street_name or block.village_name not in village_name or block.area_id not in area_id or block.street_id not in street_id or block.village_id not in village_id:
                block.save()
                village_name.add(block.village_name)
                street_name.add(block.street_name)
                area_name.add(block.area_name)
                street_id.add(block.street_id)
                area_id.add(block.area_id)
                village_id.add(block.village_id)
                print('done')
        print(area_name)
        print(street_name)
        print(village_name)
        print(street_id)
        print(area_id)
        print(village_id)


import logging
import webapp2

from google.appengine.api import taskqueue

from current_stages import get_current_stages

TEMPLATE = '''\
<meta http-equiv="refresh" content="30">
<pre>
Things started:
%s
</pre>
'''

class RunStagesHandler(webapp2.RequestHandler):
  def get(self):
    things_started = []
    for stage in get_current_stages():
      if stage.needs_cleanup():
        taskqueue.add(
            url='/cleanup/%s/previous-%s/current-%s' % (stage.name, stage.previous_commit, stage.current_commit),
            method='GET',
            queue_name='run',
        )
        things_started.append("Cleanup of %s" % stage.stage_id)
      elif not stage.is_finished():
        for tasklet in stage.tasklets:
          if tasklet.can_run():
            stage_url = '/stage/%s/previous-%s/current-%s' % (stage.name, stage.previous_commit, stage.current_commit)
            taskqueue.add(
                url='%s?go=%s' % (stage_url, tasklet.tid),
                method='GET',
                queue_name='run',
            )
            things_started.append("For %s starting %s" % (stage.stage_id, tasklet.tid))
    if things_started:
      logging.debug(things_started)
    self.response.out.write(TEMPLATE % ''.join(' - %s\n' % thing for thing in things_started))


import numpy as np
from PIL import Image
import os
from categories import categories, class_dirs

class_labels = categories
class_dirs = class_dirs

class_means = []
class_stds = []
for i, class_dir in enumerate(class_dirs):
    mean = np.zeros(3)
    std = np.zeros(3)
    num_images = 0
    for filename in os.listdir(class_dir):
        img = Image.open(os.path.join(class_dir, filename)).convert('RGB')
        # pixels = np.asarray(img)
        # print("{} - {}".format(pixels.min(), pixels.max()))
        img_np = np.array(img)
        img_np = img_np / 255.0
        mean += np.mean(img_np, axis=(0,1))
        std += np.std(img_np, axis=(0,1))
        num_images += 1
    mean /= num_images
    std /= num_images
    class_means.append(mean)
    class_stds.append(std)
    print(f"Class {i+1} - Mean: {mean}, Std: {std}")

print(np.mean(class_means, axis=0))
print(np.mean(class_stds, axis=0))


"""
https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
"""
# !/bin/python3

def climbingLeaderboard_naive(ranked, player):
    res = list()
    for p in player:
        curr_rank = 1
        prev_ranked = None
        for r_item in ranked:
            if p >= r_item:
                res.append(curr_rank)
                break
            elif prev_ranked != r_item:
                curr_rank += 1
                prev_ranked = r_item
        if p < ranked[-1]:
            res.append(curr_rank)
        ranked.append(p)
        ranked.sort(reverse=True)
    return res

def climbingLeaderboard(ranked, player):
    res = list()
    leaderboard = sorted(set(ranked), reverse=True)
    l = len(leaderboard)

    for a in player:
        while (l > 0) and (a >= leaderboard[l-1]):
            l -= 1
        res.append(l + 1)
    return res

def climbingLeaderboard__(ranked, player):
    res = list()
    set_ranked = list(set(ranked))
    set_ranked.sort(reverse=True)
    rank_dict = dict(zip(set_ranked, range(1, len(set_ranked) + 1)))
    for p in player:
        nums = list(rank_dict.keys())
        nums.sort()
        for k in range(len(nums)):
            if p > nums[k]:
                more_k = [i for i in nums if i > nums[k]]
                for i in more_k:
                    rank_dict[i] += 1
                rank_dict[p] = rank_dict[k]
    return res

if __name__ == '__main__':
    # testcase
    # f_int = open("test/in.txt")
    # f_out = open("test/out.txt")
    # inp = f_int.readlines()
    # ranked_count = int(inp[0].strip())
    # ranked = list(map(int, inp[1].rstrip().split()))
    # player_count = int(inp[2].strip())
    # player = list(map(int, inp[3].rstrip().split()))
    # result = climbingLeaderboard(ranked, player)
    # # print(' '.join(map(str, result)))
    # correct = f_out.read().split('\n')
    # # print(' '.join(correct))
    # assert correct == result

    ranked_count = int(input().strip())
    ranked = list(map(int, input().rstrip().split()))
    player_count = int(input().strip())
    player = list(map(int, input().rstrip().split()))
    result = climbingLeaderboard(ranked, player)
    print('\n'.join(map(str, result)))


from django import forms
from .models import *
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User

class AddPostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'slug', 'text', 'photo', 'is_published', 'category']

        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-input'}),
            'slug': forms.TextInput(attrs={'class': 'form-input-url'}),
            'text': forms.Textarea(attrs={'class': 'form-input-text'}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['category'].empty_label = "Категорія не вибрана"

    def clean_title(self):
        title = self.cleaned_data['title']
        if len(title) > 100:
            raise ValidationError('Довжина перевищує 100 символів')
        return title


from django.conf import settings
from django.conf.urls import include, url, patterns
from django.contrib import admin
from django.views.generic.base import TemplateView
from common.decorators import openid_exempt

urlpatterns = [
    # Examples:
    # url(r'^$', 'trustwho.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^bigvs/', include('bigvs.urls')),
    url(r'^articles/', include('articles.urls')),
    url(r'^feedback/', include('feedback.urls')),
    url(r'^subscribe/', include('subscribe.urls')),
    url(r'^prediction/', include('prediction.urls')),
    url(r'^user/', include('wechat.urls')),
    url(r'^$', openid_exempt(TemplateView.as_view(template_name='index.html'))),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += patterns('',
        url(r'^__debug__/', include(debug_toolbar.urls)),
    )
    from django.views.static import serve
    urlpatterns += patterns(
        '',
        url(r'^files/(?P<path>.*)$', openid_exempt(serve), {'document_root': settings.MEDIA_ROOT})
    )


import fractions  # gcd is here for Python versions < 3
import math  # Get gcd here for Python versions >= 3
import sys

from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import while_loop

def gcd(a, b, name=None):
  """Returns the greatest common divisor via Euclid's algorithm.

  Args:
    a: The dividend. A scalar integer `Tensor`.
    b: The divisor. A scalar integer `Tensor`.
    name: An optional name for the operation.

  Returns:
    A scalar `Tensor` representing the greatest common divisor between `a` and
    `b`.

  Raises:
    ValueError: If `a` or `b` are not scalar integers.
  """
  with ops.name_scope(name, 'gcd', [a, b]):
    a = ops.convert_to_tensor(a)
    b = ops.convert_to_tensor(b)

    a.shape.assert_has_rank(0)
    b.shape.assert_has_rank(0)

    if not a.dtype.is_integer:
      raise ValueError('a must be an integer type. Got: %s' % a.dtype)
    if not b.dtype.is_integer:
      raise ValueError('b must be an integer type. Got: %s' % b.dtype)

    # TPU requires static shape inference. GCD is used for subframe size
    # computation, so we should prefer static computation where possible.
    const_a = tensor_util.constant_value(a)
    const_b = tensor_util.constant_value(b)
    if const_a is not None and const_b is not None:
      if sys.version_info.major < 3:
        math_gcd = fractions.gcd
      else:
        math_gcd = math.gcd
      return ops.convert_to_tensor(math_gcd(const_a, const_b))

    cond = lambda _, b: math_ops.greater(b, array_ops.zeros_like(b))
    body = lambda a, b: [b, math_ops.mod(a, b)]
    a, b = while_loop.while_loop(cond, body, [a, b], back_prop=False)
    return a


import itertools
from glob import glob
import os
from .gmx.rtp import read_rtp
from .ffinput import read_ff
from . import DATA_PATH

FORCE_FIELD_PARSERS = {'.rtp': read_rtp, '.ff': read_ff}

class ForceField(object):
    """
    Description of a force field.

    Attributes
    ----------
    blocks: dict
    links: list
    modifications: list
    renamed_residues: dict
    name: str
    variables: dict
    reference_graphs: dict
    """
    def __init__(self, directory=None, name=None):
        self.blocks = {}
        self.links = []
        self.modifications = []
        self.renamed_residues = {}
        self.variables = {}
        self.name = None
        if directory is not None:
            self.read_from(directory)
            self.name = os.path.basename(directory)
        if name is not None:
            self.name = name
        if self.name is None:
            msg = 'At least one of `directory` or `name` must be provided.'
            raise TypeError(msg)

    def read_from(self, directory):
        """
        Populate or update the force field from a directory.

        The provided directory must contain a subdirectory with the same name
        as the force field.
        """
        source_files = iter_force_field_files(directory)
        for source in source_files:
            extension = os.path.splitext(source)[-1]
            with open(source) as infile:
                FORCE_FIELD_PARSERS[extension](infile, self)

    @property
    def reference_graphs(self):
        """
        Returns all known blocks.

        Returns
        -------
        dict
        """
        return self.blocks

    @property
    def features(self):
        """
        List the features declared by the links.

        Returns
        -------
        set
        """
        return set(feature for link in self.links for feature in link.features)

    def has_feature(self, feature):
        """
        Test if a feature is declared by the links.

        Parameters
        ----------
        feature: str
            The name of the feature of interest.

        Returns
        -------
        bool
        """
        return feature in self.features

def find_force_fields(directory, force_fields=None):
    """
    Read all the force fields in the given directory.

    A force field is defined as a directory that contains at least one RTP
    file. The name of the force field is the base name of the directory.

    If the force field argument is not ``None``, then it must be a dictionary
    with force field names as keys and instances of :class:`ForceField` as
    values. The force fields in the dictionary will be updated if force fields
    with the same names are found in the directory.

    Parameters
    ----------
    directory: pathlib.Path or str
        The path to the directory containing the force fields.
    force_fields: dict
        A dictionary of force fields to update.

    Returns
    -------
    dict
        A dictionary of force fields read or updated. Keys are force field
        names as strings, and values are instances of :class:`ForceField`. If a
        dictionary was provided as the "force_fields" argument, then the
        returned dictionary is the same instance as the one provided but with
        updated content.
    """
    if force_fields is None:
        force_fields = {}
    directory = str(directory)  # Py<3.6 compliance
    for name in os.listdir(directory):
        path = os.path.join(directory, name)
        try:
            next(iter_force_field_files(path))
        except StopIteration:
            pass
        else:
            try:
                if name not in force_fields:
                    force_fields[name] = ForceField(path)
                else:
                    force_fields[name].read_from(path)
            except IOError:
                msg = 'An error occured while reading the force field in  "{}".'
                raise IOError(msg.format(path))
    return force_fields

def iter_force_field_files(directory, extensions=FORCE_FIELD_PARSERS.keys()):
    """
    Returns a generator over the path of all the force field files in the directory.
    """
    return itertools.chain(*(
        glob(os.path.join(directory, '*' + extension))
        for extension in extensions
    ))

FORCE_FIELDS = find_force_fields(os.path.join(DATA_PATH, 'force_fields'))


#-*-coding:utf8 -*-

import os
import time

import cmd_tools
from config import ConfigParse

import log_utils
import manifest_utils
import my_utils
import zip_utils

from pack_exception import PackException
import pack_exception
import file_utils
import env
from time import sleep

class PackManager(object):
    """
    	打包基类
    """
    __debug = False
    _packageName = None
    _projectShorName = None

    def run(self):
        self.decompile()
        self.injectFilesForAutomation()
        self.pack()

    def __init__(self):
        self.sdksRootDir = os.path.join(env.SUPPORT_DIR, self._projectShorName)

        self.outputDir = ConfigParse.shareInstance().getOutputDir()
        if self.outputDir is None :
            raise PackException(pack_exception.PHP_PARAMS_ERROR, 'outputDir is None')
        self.outputName = ConfigParse.shareInstance().getOutputName()
        self.apkFile = ConfigParse.shareInstance().getApkPath()

        timestamp_str = str(time.time())
        self.taskWorkDir = os.path.join(my_utils.getWorkDir(), self._projectShorName, timestamp_str, "temp")

        if not os.path.exists(self.taskWorkDir):
            os.makedirs(self.taskWorkDir)
        log_utils.getLogger().info('taskWorkDir == ' + self.taskWorkDir)
        self.decompileDir = self.taskWorkDir + '/decompile'
        self.tmpApkToolPath = os.path.join(self.taskWorkDir, 'apktooltmp')

    def __del__(self):
        if not self.__debug:
            my_utils.delete_file_folder(self.taskWorkDir)

    def decompile(self):
        log_utils.getLogger().info('start decompiling...')

        taskLogDir = os.path.join(self.taskWorkDir, 'log')
        if not os.path.exists(taskLogDir):
            os.makedirs(taskLogDir)

        logFileName = os.path.join(taskLogDir, 'repack.log')
        log_utils.changeLoggerFileHandler(logFileName)

        if not os.path.exists(self.apkFile):
            raise PackException(pack_exception.SOURCE_APK_NOT_EXIST, 'source apk file %s does not exist' % self.apkFile)

        tmpApkSource = self.taskWorkDir + '/temp.apk'
        my_utils.copyFile(self.apkFile,tmpApkSource)

        log_utils.getLogger().info('decompiling apk ... ')
        #apk反编译
        ret = cmd_tools.decompileApk(tmpApkSource, self.decompileDir, self.tmpApkToolPath)
        if ret:
            raise PackException(pack_exception.DECOMPLE_APK_FAILED, 'decompile apk %s failed' % tmpApkSource)

    def injectFilesForAutomation(self):
        raise NotImplementedError("请在PackManger子类里实现该方法！")

    def pack(self):
        try:
            log_utils.getLogger().info('generating new r file ... ')
            #重新生成R文件

            ret = cmd_tools.produceNewRFile(
                self._packageName,
                self.decompileDir)
            if ret:
                raise PackException(pack_exception.PRODUCE_NEW_R_FILE_FAILED, 'produce new r file failed')

            #重新编译成apk
            tempRecompileApkDir = self.taskWorkDir + '/tempRecompileApk'
            tempRecompileApk = '%s/nosig.apk' % (tempRecompileApkDir)
            tempSignedApkName = '%s/_sig.apk' % (tempRecompileApkDir)

            log_utils.getLogger().info('recompiling apk ... tempRecompileApk = ' + tempRecompileApk)
            ret = cmd_tools.recompileApk(self.decompileDir, tempRecompileApk, self.tmpApkToolPath)
            if ret:
                raise PackException(pack_exception.RECOMPILE_APK_FAILED, 'recompile apk failed')

            # self.outputName是 autotest_xxx.apk
            self.doPackWithoutChannelSymbol(tempRecompileApk, tempSignedApkName, self.outputDir, self.outputName)

            #测试桩重打包
            import env
            testbundle_apkFile = os.path.join(env.SUPPORT_DIR, self._projectShorName, 'testbundle', 'TestBundle.apk')
            #testbundle_apkFile = os.path.join(env.GOD_TOOL_DIR, 'TestBundle_base.apk')
            if not os.path.exists(testbundle_apkFile):
                raise PackException(pack_exception.SOURCE_APK_NOT_EXIST, 'testbundle_base apk file %s does not exist' % testbundle_apkFile)

            # 拷贝TestBundle.apk 到  工作目录下/testbundle_temp.apk
            tmpTestBundleApk = self.taskWorkDir + '/testbundle_temp.apk'
            my_utils.copyFile(testbundle_apkFile,tmpTestBundleApk)

            testbundle_decompileDir = self.taskWorkDir + '/testbundle_decompile'
            testbundle_tmpApkToolPath = os.path.join(self.taskWorkDir, 'testbundle_apktooltmp')
            log_utils.getLogger().info('decompiling testbundle apk ... ')

            #测试桩反编译 到   工作目录下/testbundle_decompile
            ret = cmd_tools.decompileApk(tmpTestBundleApk, testbundle_decompileDir, testbundle_tmpApkToolPath)
            if ret:
                raise PackException(pack_exception.DECOMPLE_APK_FAILED, 'decompile testbundle apk %s failed' % tmpTestBundleApk)

            testbundle_packageName = "com.boyaa.application.testbundle"
            # 修改manifest.xml
            self.repackTestBundleForMatch(self.sdksRootDir, testbundle_decompileDir, self._packageName)

            log_utils.getLogger().info('generating new r file for testbundle... ')
            #重新生成测试桩的R文件
            ret = cmd_tools.produceNewRFile(
                testbundle_packageName,
                testbundle_decompileDir)
            if ret:
                raise PackException(pack_exception.PRODUCE_NEW_R_FILE_FAILED, 'produce new r file for testbundle failed')

            #重新编译测试桩apk
            testbundle_tempRecompileApkDir = self.taskWorkDir + '/testbundle_tempRecompileApk'
            testbundle_tempRecompileApk = '%s/testbundle_nosig.apk' % (testbundle_tempRecompileApkDir)
            testbundle_tempSignedApkName = '%s/testbundle_sig.apk' % (testbundle_tempRecompileApkDir)

            log_utils.getLogger().info('recompiling apk ... tempRecompileApk = ' + testbundle_tempRecompileApk)
            ret = cmd_tools.recompileApk(testbundle_decompileDir, testbundle_tempRecompileApk, testbundle_tmpApkToolPath)
            if ret:
                raise PackException(pack_exception.RECOMPILE_APK_FAILED, 'recompile test bundle apk failed')

            self.doPackWithoutChannelSymbol(testbundle_tempRecompileApk, testbundle_tempSignedApkName, os.path.join(self.outputDir, 'testbundle'), "testbundle.apk")

            #log_utils.getLogger().info('doPack: =============================================success=============================================' + StreamHandler.terminator)

        except PackException as pe:
            log_utils.getLogger().error(pe)
            #log_utils.getLogger().info('doPack: =============================================Failed=============================================' + StreamHandler.terminator)
            return

    def doPackWithoutChannelSymbol(self, tempRecompileApk, tempSignedApkName, outputDir, outputName):
        '''
        @param channelId 渠道id，用于获取keystore
        @param tempRecompileApk 重打包后的未签名apk path
        @param tempSignedApkName  签名对齐后的包名称
        @param outputDir  输出路径（非完整path）
        @param outputName  输出名称（非完整path）
        '''
        log_utils.getLogger().info('doPackWithoutChannelSymbol ... ')
        #apk签名
        log_utils.getLogger().info('signing apk')
        keystoreInfo = ConfigParse.shareInstance().getKeystoreInfo()

        # 签名，没细看
        ret = cmd_tools.signApk(tempRecompileApk, keystoreInfo['file'], keystoreInfo['storePassword'],
                      keystoreInfo['keyAlias'], keystoreInfo['aliasPassword'])
        if ret:
            raise PackException(pack_exception.SIGN_APK_FAILED, 'sign apk failed')

        #apk优化
        apkName = '%s/%s' % (outputDir, outputName)
        outputDir = os.path.dirname(apkName)

        if not os.path.exists(outputDir):
            os.makedirs(outputDir)

        log_utils.getLogger().debug(outputDir)
        log_utils.getLogger().info('zipaligning and renaming apk apkName == ' + apkName);

        # 调用工具里的 zipalign.exe 对temRecompileApk 进行优化，生成tempSignedApkName
        ret = cmd_tools.alignAPK(tempRecompileApk, tempSignedApkName)

        if ret:
            raise PackException(pack_exception.ALIGN_APK_FAILED, 'align apk failed')
        import shutil
        shutil.move(tempSignedApkName, apkName)

    def doPackWithOneChannelSymbol(self, channelId, tempRecompileApk, tempSignedApkName, outputDir, outputName, symbol, taskWorkDir):
        '''
        @param channelId 渠道id，用于获取keystore
        @param tempRecompileApk 重打包后的未签名apk path
        @param tempSignedApkName  签名对齐后的包名称
        @param outputDir  输出路径（非完整path）
        @param outputName  输出名称（非完整path）
        @param symbol  自定义渠道标记
        @param taskWorkDir  工作目录
        '''
        log_utils.getLogger().info('doPackWithOneChannelSymbol ... ')
        #添加渠道标识
        log_utils.getLogger().info('adding channel symbol to /assets/ ... ')
        channelSymbolFileName = ConfigParse.shareInstance().getChannelSymbolFileName()
        zip_utils.addChannelSymbolFile(tempRecompileApk, symbol, channelSymbolFileName, taskWorkDir)

        #apk签名
        log_utils.getLogger().info('signing apk')
        keystoreInfo = ConfigParse.shareInstance().getKeystoreInfoByChannelId(channelId)
        ret = cmd_tools.signApk(tempRecompileApk, keystoreInfo['file'], keystoreInfo['storePassword'],
                      keystoreInfo['keyAlias'], keystoreInfo['aliasPassword'])
        if ret:
            raise PackException(pack_exception.SIGN_APK_FAILED, 'sign apk failed')

        #apk优化
        apkName = '%s/%s' % (outputDir, outputName)
        outputDir = os.path.dirname(apkName)

        if not os.path.exists(outputDir):
            os.makedirs(outputDir)

        log_utils.getLogger().debug(outputDir)
        log_utils.getLogger().info('zipaligning and renaming apk apkName == ' + apkName);
        ret = cmd_tools.alignAPK(tempRecompileApk, tempSignedApkName)
        if ret:
            raise PackException(pack_exception.ALIGN_APK_FAILED, 'align apk failed')
        os.rename(tempSignedApkName, apkName)

    def doBatchPackWithChannelSymols(self, channelId, unsignedApk, tempSignedApkName, outputDir, outputName, symbolList, taskWorkDir):
        '''
        @param channelId 渠道id，用于获取keystore
        @param unsignedApk 重打包后的未签名apk path
        @param tempSignedApkName  签名对齐后的包名称
        @param outputDir  输出路径（非完整path）
        @param outputName  输出名称（非完整path）
        @param symbolList  自定义渠道标记列表
        @param taskWorkDir  工作目录
        '''
        log_utils.getLogger().info('doBatchPackWithChannelSymols ... ')
        batchPackDir = os.path.join(taskWorkDir, 'batch')
        if not os.path.exists(batchPackDir):
            os.makedirs(batchPackDir)

        copiedUnsignedApk = os.path.join(os.path.dirname(unsignedApk), 'batchTmpApk.apk')

        log_utils.getLogger().info('start batch packing ... ')
        for symbol in symbolList:
            if not file_utils.copyFile(unsignedApk, copiedUnsignedApk):
                raise PackException(pack_exception.COPY_FILE_EXCEPTION,
                                    'cannot copy apk during doBatchPackWithChannelSymols')

            #添加渠道标识
            log_utils.getLogger().info('adding channel symbol to /assets/ ... ')
            channelSymbolFileName = ConfigParse.shareInstance().getChannelSymbolFileName()
            zip_utils.addChannelSymbolFile(copiedUnsignedApk, symbol, channelSymbolFileName, taskWorkDir)

            #apk签名
            log_utils.getLogger().info('signing apk')
            keystoreInfo = ConfigParse.shareInstance().getKeystoreInfoByChannelId(channelId)
            ret = cmd_tools.signApk(copiedUnsignedApk, keystoreInfo['file'], keystoreInfo['storePassword'],
                          keystoreInfo['keyAlias'], keystoreInfo['aliasPassword'])
            if ret:
                raise PackException(pack_exception.SIGN_APK_FAILED, 'sign apk failed')

            #apk优化
            apkName = '%s/%s' % (batchPackDir, outputName)
            basename = os.path.splitext(os.path.basename(apkName))[0]
            apkOutputDir = os.path.dirname(apkName)
            if not os.path.exists(apkOutputDir):
                os.makedirs(apkOutputDir)

            apkName = os.path.join(apkOutputDir, basename + '(' + symbol + ').apk')

            log_utils.getLogger().debug(apkOutputDir)
            log_utils.getLogger().info('zipaligning and renaming apk apkName == ' + apkName);
            ret = cmd_tools.alignAPK(copiedUnsignedApk, tempSignedApkName)
            if ret:
                raise PackException(pack_exception.ALIGN_APK_FAILED, 'align apk failed')
            os.rename(tempSignedApkName, apkName)

        log_utils.getLogger().info('batch packing success... ')
        log_utils.getLogger().info('zip apk files ... ')
        zip_utils.zipApks(batchPackDir, '%s/%s' % (outputDir, outputName))

    def repackTestBundleForMatch(self, sdksRootDir, decompileDir, targetPackageName):
        cfgResRootDir = sdksRootDir

        #apk资源修改
        decompileManifest = os.path.join(decompileDir, 'AndroidManifest.xml')

        log_utils.getLogger().info('modifying AndroidManifest.xml ... ')
        manifest_utils.modifyManifestForTestBundle(decompileManifest, decompileManifest, targetPackageName)


import argparse
from zquest.extract import ZeldaClassicReader

def uncompress_qst(input_path, output_path):
    reader = ZeldaClassicReader(input_path, {'only_sections': []})
    reader.read_qst()
    reader.save_qst_no_serialize_no_compression(output_path)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('input', type=str)
    parser.add_argument('--output')
    args = parser.parse_args()

    uncompress_qst(args.input, args.output)


""" Declear class-based view for exposing Chef model """
from django.db.models import Prefetch

from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import DjangoModelPermissionsOrAnonReadOnly

from community.models import Chef
from community.serializers import ChefSerializer, BaseChefSerializer

class ChefViewSet(ReadOnlyModelViewSet):
    """ Chef viewset """
    filterset_fields = ['brands', ]

    queryset = Chef.objects.all()
    serializer_class = ChefSerializer
    permission_classes = (DjangoModelPermissionsOrAnonReadOnly,)

    def get_queryset(self):
        queryset = super().get_queryset()
        queryset = queryset.prefetch_related('brands', 'profile_media',)
        queryset = queryset.prefetch_related(
            Prefetch('brands__tags'),
            Prefetch('brands__chefs'),
        )
        return queryset

    def get_serializer_class(self):
        if self.action == 'list':
            return BaseChefSerializer
        return super().get_serializer_class()


inputs = ['test1', 'input']
movement = {
    'N': 1,
    'E': 1j,
    'S': -1,
    'W': -1j,
    'R': 1j,
    'L': -1j
}

for input in inputs:

    # Part 1
    with open(f'12/{input}', 'r') as f:
        instructions = [(r[0], int(r[1:])) for r in f.read().split("\n")]

    heading = 0 + 1j
    location = 0 + 0j

    for action, value in instructions:
        if action == 'F':
            location += heading * value
        if action in 'NEWS':
            location += movement[action] * value
        elif action in 'LR':
            for _ in range((value // 90)):
                heading *= movement[action]
        # print(f'Position {location} heading {heading}')

    print(f'Part 1: {int(abs(location.real) + abs(location.imag))}')

    # Part 2
    location = 0 + 0j
    waypoint = 1 + 10j

    for action, value in instructions:
        if action == 'F':
            location += waypoint * value
        if action in 'NEWS':
            waypoint += movement[action] * value
        elif action in 'LR':
            for _ in range((value // 90)):
                waypoint *= movement[action]

        # print(f'Position {location} waypoint {waypoint}')

    print(f'Part 2: {int(abs(location.real) + abs(location.imag))}')


import crypt

print("Hashcrack~v1")
print("OBS: Ferramenta que possivelmente irie atualizar futuramente")
print("=="*30)
print("=="*10, "Hashcrack~v1", "=="*13)
print("=="*30)

i = input("Deseja pesquisar por quantidade de caracteres?(s/n): ").upper()

if i == "S":
    qtd_i = float(input("Caracteres: "))
    hash = input("hash:")
    list = str(input("Wordlist:"))
    file = open(list, 'r')

    for senhas in file:
        if len(senhas) == qtd_i+1:
            c = crypt.crypt(senhas[:-1], hash)
            if c == hash:
                print("\nSenha: {}".format(senhas))
else:
    hash = input("hash:")
    list = str(input("Wordlist:"))
    file = open(list, 'r')
    for senhas in file:
        c = crypt.crypt(senhas[:-1], hash)
        if c == hash:
            print("\nSenha: {}".format(senhas))


# coding=utf-8
from collections import defaultdict

input_file1 = 'D:/Projects/Pycharm/SIR/Data/info_stat/RepostCountReal.txt'
input_file2 = 'D:/Projects/Pycharm/SIR/Data/info_stat/SpreadTree.txt'
output_file = 'D:/Projects/Pycharm/SIR/Data/info/big_info/'

mid = []
ratio = 0.9
repost_dict = defaultdict(list)

def extract_big_info(path1, path2, path3):
    with open(path1, 'r', encoding='utf-8') as read_f:
        for row in read_f:
            row = row.split('\t')
            if int(row[1]) != 0:
                res = int(row[2])/int(row[1])
                if res >= ratio:
                    mid.append(row[0])
    with open(path2, 'r', encoding='utf-8') as read_f:
        cnt = 0
        for row in read_f:
            cnt += 1
            if cnt % 10000 == 0:
                print(cnt)
            row = row.split("'")
            if row[1] in mid:
                record = row[3]+'\t'+row[5]+'\t'+row[7]+'\t'+row[9]+'\t'+row[11]
                repost_dict[row[1]].append(record)
    for key in repost_dict:
        s = path3 + str(key)
        with open(s, 'w') as write_f:
            write_f.write('%s\t%s\n' % (key, '\t'.join(repost_dict[key])))

if __name__ == '__main__':
    extract_big_info(input_file1, input_file2, output_file)


"""
https://projecteuler.net/problem=4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.
"""

MAX_THREE_DIGITS = 999
result = 0

for digit_1 in range(MAX_THREE_DIGITS, 100, -1):
  for digit_2 in range(MAX_THREE_DIGITS, 100, -1):
      product = digit_1 * digit_2
      reverse_product_str = str(product)[::-1]
      if str(product) == reverse_product_str and product > result:
        result = product

print(result)


from . import user_admin
from django.contrib import admin

from api import models
from . import inlines

@admin.register(models.Subject)
class SubjectAdmin(admin.ModelAdmin):
    inlines = [inlines.CurriculumInline]
    list_display = ["name", "curriculums"]

    @admin.display()
    def curriculums(self, obj):
        return [x.grade for x in obj.curriculums.all()]

@admin.register(models.Strand)
class StrandAdmin(admin.ModelAdmin):
    list_display = ["name", "curriculum"]
    search_fields = ["name"]
    inlines = [inlines.SubStrandInline]

@admin.register(models.District)
class DistrictAdmin(admin.ModelAdmin):
    search_fields = ["name"]

@admin.register(models.School)
class SchoolAdmin(admin.ModelAdmin):
    search_fields = ["code", "name"]
    list_display = ["name", "district", "code"]
    autocomplete_fields = ["district"]
    fieldsets = (
        (
            "Basic Information",
            {"fields": ("name", "code", "owner", "email", "gender", "telephone")},
        ),
        (
            "Location Information",
            {"fields": ("district", "town", "digital_address", "location")},
        ),
        (
            "Other Information",
            {
                "fields": ("description", "date_established", "logo", "visible"),
            },
        ),
    )

@admin.register(models.Lesson)
class LessonAdmin(admin.ModelAdmin):
    list_display = ["topic", "curriculum", "slug"]
    list_display_links = ["slug"]

    @admin.display()
    def curriculum(self, instance):
        return "%s %s" % (instance.grade, instance.subject)


import os
print(os.path.abspath(__file__))

from docx2python import docx2python
from Resolution import Resolution

with open('resolution.docx', "w") as f:
    doc = docx2python(f, html = True)
    print(doc.text)
    print("---------------------------------")

reso = Resolution(doc.text)

print(reso)

#import json

#with open("jsondump.json", "w") as f:
#    json.dump(reso.resolution, f, indent=4)

##import docx
##
##doc = docx.Document("Mock Resolution.docx")
##paras = doc.paragraphs
##
##for p in paras:
##    print("style: " + p.style.name + " | text: " + p.text[:20])
##    print("-----")
##
##print(paras[12].runs[0].text)
##
##def test():
##    print(paras[12].runs[0].text)
#print(paras[12].style)

##import zipfile
##import xml.dom.minidom

##d = zipfile.ZipFile('./Mock Resolution.docx')
##uglyXml = xml.dom.minidom.parseString(d.read('word/numbering.xml')).toprettyxml(indent='  ')
##
##with open('numbering.xml', 'w') as f:
##    f.write(uglyXml)
##
##import xml.etree.ElementTree as ET
##
##tree = ET.parse('document.xml')
##root = tree.getroot()
##w = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
##
##for p in root[0].findall(w + 'p'):
##    for ppr in p.findall(w + 'pPr'):
##        for numpr in ppr.findall(w + 'numPr'):
##            print(numpr.find(w + 'ilvl').attrib)
##


import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk, ImageFilter
import tkinter.messagebox
import pytesseract
import cv2
import numpy as np

def extract_text(Image_path):
    # converts the image to text by taking the image path as an input
    output_text = pytesseract.image_to_string(Image_path)
    return output_text

class Parent(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)
        self.frames = {}

        # allows the different screens to be raised to the front

        for F in (First_page,Image_view):
            frame = F(container, self)
            self.frames[F]= frame
            frame.grid(row=0,column=0,sticky = 'nsew')
        self.show_screen(First_page)

    def show_screen(self, screenpage):
        # selects the appropriate screen to be pushed to the front of the application
        frame = self.frames[screenpage]
        frame.tkraise()

class First_page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        # Front page label
        Front_page_label = tk.Label(self,text = 'Please select the image file.')
        Front_page_label.pack(side='left',fill = 'x',expand =1)
        # Button used to select an image from the file system
        BrowseButton1 = tk.Button(self,text='Broswe for image.', command= self.browse_images)
        BrowseButton1.pack(side= 'left',fill = 'x',expand =1,padx = 20)

        # Button used to view the selected image and to proceed to the next page in the app
        ViewButton1 = tk.Button(self,text='View uploaded image.', command= self.view)
        ViewButton1.pack(side= 'left',fill = 'y',expand =1,padx = 20)

    def view(self):
        try:
            image = Image.open(filename)
            photo = ImageTk.PhotoImage(image)
            global lx
            lx = tk.Label(image=photo)
            lx.image = photo
            lx.pack()
            self.controller.show_screen(Image_view)
        except:
            tkinter.messagebox.showinfo('Error', 'Select a Jpeg or Png image first')

    def browse_images(self):
        global filename
        filename =  filedialog.askopenfilename(initialdir = "/",title = "Select image file",filetypes = (("jpeg files","*.jpg"),("png files","*.png"),("all files","*.*")))
        if len(filename) == 0:
            tkinter.messagebox.showinfo('Error', 'Please select an actual file')
        else:
            tkinter.messagebox.showinfo('Success!', 'The image has been uploaded, click on View uploaded image.')

    def preprocess():
        image_file = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) # read the image into opencv format
        kernel = np.ones((1,1),np.uint8) # kernel used for erosion and dilation
        image_file = cv2.dilate(image_file, kernel, iterations = 1)
        image_file = cv2.erode(image_file, kernel, iterations = 1)
        retval, threshold = cv2.threshold(image_file, 115,255, cv2.THRESH_BINARY) # different thresholding function may be used later
        cv2.imshow('Result of Image preprocessing.', threshold) # displays the preprocessed image
        text_released = extract_text(image_file)
        result_print = tk.StringVar()
        result_print.set(text_released)

        # A new window is created to display the extracted text
        top = tk.Toplevel()
        top.title('Extracted text.')
        top.geometry('800x450')
        t= tk.Label(top,text = '', textvariable =result_print)
        t.pack()
        if len(text_released) == 0:
            tkinter.messagebox.showinfo('', 'No text extracted from image.')

class Image_view(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        preprocessButton = tk.Button(self,text = 'Preprocess Image and Extract text.',command = First_page.preprocess)
        preprocessButton.pack(side = 'bottom')

App = Parent()
App.title('Optical Character Recognition App')
App.geometry('1600x900')
App.mainloop()


import os
import sys
from netpyne import sim
from sciunit.models.backends import Backend

class NetpyneBackend(Backend):
  # From sim.load()
  def cache_to_results(self, cache):
    sim.initialize()
    sim.loadSimCfg(None, data=cache)
    sim.cfg.createNEURONObj = True
    sim.loadNetParams(None, data=cache)
    sim.loadNet(None, data=cache, compactConnFormat=sim.cfg.compactConnFormat)
    sim.loadSimData(None, data=cache)

  # From sim.saveData()
  def results_to_cache(self, results):
    cache = {
      'netpyne_version': sim.version(show=False),
      'netpyne_changeset': sim.gitChangeset(show=False),
      'net': {
        'params': sim.replaceFuncObj(sim.net.params.__dict__),
        'cells': sim.net.allCells,
        'pops': sim.net.allPops,
      },
      'simConfig': sim.cfg.__dict__,
      'simData': sim.allSimData
    }

    if 'LFP' in cache['simData']['allSimData']:
      cache['simData']['allSimData']['LFP'] = cache['simData']['allSimData']['LFP'].tolist()

    cache = sim.replaceDictODict(cache)

    return cache

  def get_sim_hash(self):
    """
      Used to determine whether the sim we want to run is already cached.

      Does NOT react to sim.net.modifyConns, modifyStims, and other similar methods.
      If we want to differentiate between the sims on which `sim.net.modifyConns` was or wasn't called, we want to cache:
        net['cells'] = sim.net.allCells
        net['pops'] = sim.net.allPops
      additionally.
      However, these can be huge objects, and we might want to consider other options:
      1. Do we even need to account for `sim.net.modifyConns`? Is this frequently used in papers?
      2. Instead of looking at sim.net.allCells, every time we change something, we might change the attribute of the NetpyneModel.
    """
    net = {}
    net['params'] = sim.replaceFuncObj(sim.net.params.__dict__)
    net['simConfig'] = sim.cfg.__dict__
    net['netpyne_version'] = sim.version(show=False)
    return net

  def backend_run(self):
    self.model.initSim()
    sim_hash = self.get_sim_hash()
    self.model.set_run_params(sim_hash=sim_hash)
    # Checks whether we have cache, and runs it (with _backend_run) if we don't.
    super().backend_run()

  def _backend_run(self):
    # Identical to sim.create():
    #
    # 1. We leave these up to the user (they implement it in initSim()):
    #    ATTENTION: it's prettier to only leave sim.initialize() to the user.
    #    The reason I also left .createPops() and .createCells() to the user
    #    is because the PdcmModel randomizes the input AFTER it creates pops & cells.
    #    When we implement more models it will be more clear what we want
    #    the user to have in initSim(), and what we want to run for them.
    #
    #    sim.initialize(netParams, simConfig)
    #    sim.net.createPops()
    #    sim.net.createCells()
    #
    # 2. And we run these ourselves.
    #    The user can override _backend_run if they want to!
    sim.net.connectCells()
    sim.net.addStims()
    sim.net.addRxD()
    sim.setupRecording()

    # Identical to sim.simulate():
    sim.runSim()
    sim.gatherData()

    # Identical to sim.analyze():
    sim.saveData()
    sim.analysis.plotData()
    return sim


#pip install gitpython
import os
import git

projectsFolder = '/home/gary/Projects/'

repoUrl = input("Repo url:")
repoName = repoUrl.split("/")[-1]
repoDir = os.path.join(projectsFolder,repoName)

print(repoDir)
try:
    repo = git.Repo.init(repoDir)
    repo.clone_from(repoUrl,repoDir)
except:
    print("Repository is already existing!")


from dataclasses import dataclass
from datetime import datetime
from functools import singledispatchmethod
from uuid import UUID

import cbaxter1988_utils.log_utils
from boe.applications.transcodings import (
    TaskEntityTranscoding,
    TaskStatusEnumTranscoding,
    BytesTranscoding
)
from boe.lib.common_models import AppEvent
from boe.lib.domains.task_domain import (
    TaskAggregate,
    TaskDomainFactory,
    TaskDomainWriteModel,
    TaskStatusEnum
)
from eventsourcing.application import Application
from eventsourcing.persistence import Transcoder

logger = cbaxter1988_utils.log_utils.get_logger("TaskDomainApps")

class TaskManagerAppEventFactory:
    @dataclass(frozen=True)
    class NewTaskEvent(AppEvent):
        task_id: UUID
        owner_id: UUID
        name: str
        description: str
        status: TaskStatusEnum
        value: float
        evidence_required: bool
        due_date: datetime

    @dataclass(frozen=True)
    class UpdateTaskValueEvent(AppEvent):
        task_id: UUID
        value: float

    @dataclass(frozen=True)
    class AddEvidenceEvent(AppEvent):
        task_id: UUID
        data: bytes

    @dataclass(frozen=True)
    class MarkTaskCompleteEvent(AppEvent):
        task_id: UUID

    @classmethod
    def build_new_task_event(
            cls,
            owner_id: str,
            name: str,
            description: str,
            due_date: datetime,
            evidence_required: bool,
            value: float,
            task_id: str,
            **kwargs

    ) -> NewTaskEvent:
        return cls.NewTaskEvent(
            owner_id=UUID(owner_id),
            name=name,
            description=description,
            status=TaskStatusEnum.incomplete,
            due_date=due_date,
            evidence_required=evidence_required,
            value=value,
            task_id=UUID(task_id)
        )

    @classmethod
    def build_mark_task_complete_event(
            cls,
            task_id: str
    ) -> "TaskManagerAppEventFactory.MarkTaskCompleteEvent":
        return cls.MarkTaskCompleteEvent(
            task_id=UUID(task_id)
        )

    @classmethod
    def build_update_task_value_event(
            cls,
            task_id: str,
            value: float
    ) -> "TaskManagerAppEventFactory.UpdateTaskValueEvent":
        return cls.UpdateTaskValueEvent(
            task_id=UUID(task_id),
            value=value
        )

    @classmethod
    def build_add_evidence_event(
            cls,
            task_id: str,
            data: bytes
    ) -> "TaskManagerAppEventFactory.AddEvidenceEvent":
        return cls.AddEvidenceEvent(
            task_id=UUID(task_id),
            data=data
        )

class TaskManagerApp(Application):
    def __init__(self):
        super().__init__()
        self.factory = TaskDomainFactory()
        self.write_model = TaskDomainWriteModel()

    def register_transcodings(self, transcoder: Transcoder):
        super().register_transcodings(transcoder)
        transcoder.register(TaskEntityTranscoding())
        transcoder.register(TaskStatusEnumTranscoding())
        transcoder.register(TaskStatusEnumTranscoding())
        transcoder.register(BytesTranscoding())

    def get_task_aggregate(self, task_id: UUID) -> TaskAggregate:
        return self.repository.get(task_id)

    def _save_aggregate(self, aggregate: TaskAggregate):
        self.write_model.save_aggregate(aggregate)
        self.save(aggregate)

    @singledispatchmethod
    def handle_event(self, event):
        raise NotImplementedError(f'Invalid Event {event}')

    @handle_event.register(TaskManagerAppEventFactory.NewTaskEvent)
    def _(self, event: TaskManagerAppEventFactory.NewTaskEvent):
        task_aggregate = self.factory.build_task_aggregate(
            value=event.value,
            due_date=event.due_date,
            description=event.description,
            owner_id=str(event.owner_id),
            evidence_required=event.evidence_required,
            name=event.name,
            _id=str(event.task_id)
        )
        self.write_model.save_aggregate(task_aggregate)

        self.save(task_aggregate)
        return task_aggregate.id

    @handle_event.register(TaskManagerAppEventFactory.MarkTaskCompleteEvent)
    def _(self, event: TaskManagerAppEventFactory.MarkTaskCompleteEvent):
        task_aggregate = self.get_task_aggregate(task_id=event.task_id)
        task_aggregate.mark_task_complete()

        self.write_model.save_aggregate(task_aggregate)
        self.save(task_aggregate)

    @handle_event.register(TaskManagerAppEventFactory.UpdateTaskValueEvent)
    def _(self, event: TaskManagerAppEventFactory.UpdateTaskValueEvent):
        aggregate = self.get_task_aggregate(task_id=event.task_id)

        aggregate.change_value(value=event.value)

        self._save_aggregate(aggregate)

        return aggregate.id

    @handle_event.register(TaskManagerAppEventFactory.AddEvidenceEvent)
    def _(self, event: TaskManagerAppEventFactory.AddEvidenceEvent):
        aggregate = self.get_task_aggregate(task_id=event.task_id)

        if aggregate.task.evidence_required:
            aggregate.add_task_evidence(data=event.data)

        self._save_aggregate(aggregate=aggregate)

        return aggregate.id


from flask import request, session
from auth.auth import is_authenticated, is_admin
from flask.json import jsonify
from models.patients import Patients, db, ma
from models.assessments import Assessments
from models.patient_screening_details import PatientsScreeningDetails
import pymysql

def create_patient():
    data = request.get_json()

    fname = data["firstname"]
    lname = data["lastname"]
    email = data["email"]
    phone = data["contact"]
    age = data["age"]
    bday = data["bday"]
    gender = data["gender"]
    street = data["address"]
    city = data["city"]
    country = data["country"]
    zip = data["zipcode"]
    user_id = session.get("user_id")

    # Check if session exist
    if not is_authenticated(user_id):
        return jsonify({"error": "Unauthorized"}), 401

    try:
        new_user = Patients(fname=fname, lname=lname, fullname="{} {}".format(
            fname, lname), email=email, phone=phone, age=age, bday=bday, gender=gender, street=street, city=city, country=country, zip=zip, created_by=user_id)

        db.session.add(new_user)
        db.session.commit()

    except pymysql.Error as e:
        print("could not close connection error pymysql %d: %s" %
              (e.args[0], e.args[1]))

    patients_query = db.session.query(
        *Patients.__table__.columns).select_from(Patients)
    patients_query = patients_query.outerjoin(Assessments, Patients.id == Assessments.patient_id).filter(
        Patients.created_by == user_id).order_by(Patients.id)

    patients_query_response = patients_query.all()
    patient_response_object = patient_record_schema.jsonify(
        patients_query_response)

    assessments_query = db.session.query(*Assessments.__table__.columns).select_from(
        Assessments).filter(Assessments.patient_id == Patients.id)
    assessment_response_object = patient_assessment_schema.jsonify(
        assessments_query)

    records = {'patients': patient_response_object.get_json(
    ), 'assessment': assessment_response_object.get_json()}

    return records

class PatientAssessmentSchema(ma.Schema):
    class Meta:
        fields = ('id', 'date_taken', 'date_finished',
                  'assessor_id', 'patient_id')

patient_assessment_schema = PatientAssessmentSchema(many=True)

class PatientRecordSchema(ma.Schema):
    class Meta:
        fields = ('id', 'fname', 'lname', 'fullname', 'age', 'bday', 'phone', 'email',
                  'gender', 'city', 'country', 'street', 'zip', 'registered_date', 'created_by')

patient_record_schema = PatientRecordSchema(many=True)

class PatientScreeningDetailsSchema(ma.Schema):
    class Meta:
        fields = ('id', 'assessment_id', 'patient_notes', 'last_edited_on')

patient_screening_details_schema = PatientScreeningDetailsSchema(many=True)


from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post, Groups
from django.contrib.auth import get_user_model, get_user
from .forms import NewPost
from django.contrib.auth.decorators import login_required

# Create your views here.

def first(request):
    post_list = Post.objects.order_by('-pub_date').all()
    paginator = Paginator(post_list, 10)  # показывать по 10 записей на странице.

    page_number = request.GET.get('page')  # переменная в URL с номером запрошенной страницы
    page = paginator.get_page(page_number)  # получить записи с нужным смещением
    return render(request, 'index.html', {
        'page': page,
        'paginator': paginator
    }
                  )

def group_posts(request, slug):
    # функция get_object_or_404 получает по заданным критериям объект из базы данных
    # или возвращает сообщение об ошибке, если объект не найден
    group = get_object_or_404(Groups, slug=slug)
    post_list = Post.objects.filter(group=group).order_by('-pub_date').all()
    paginator = Paginator(post_list, 10)  # показывать по 10 записей на странице.

    page_number = request.GET.get('page')  # переменная в URL с номером запрошенной страницы
    page = paginator.get_page(page_number)  # получить записи с нужным смещением
    # Метод .filter позволяет ограничить поиск по критериям. Это аналог добавления
    # условия WHERE group_id = {group_id}
    return render(request, "group.html", {
        "group": group,
        'page': page,
        'paginator': paginator
    }
                  )

@login_required
def new_post(request):
    form = NewPost()
    return render(request, 'new_post.html', {
        'form': form
    })

def new_post_confirm(request):
    text = request.POST.get('text')
    group = request.POST.get('group')
    author = get_user(request)
    post = Post(text=text, group_id=group, author=author)
    post.save()
    return render(request, 'new_post_confirm.html')

def profile(request, username):
    users = get_object_or_404(get_user_model(), username=username)
    post_list = Post.objects.filter(author=users).order_by('-pub_date').all()
    posts = Post.objects.filter(author=users).latest('-pub_date')
    num = post_list.count()
    paginator = Paginator(post_list, 10)
    page_number = request.GET.get('page')
    page = paginator.get_page(page_number)

    return render(request, 'profile.html', {
        'users': users,
        'page': page,
        'paginator': paginator,
        'num': num,
        'posts': posts

    })

def post_view(request, username, post_id):
    get_object_or_404(get_user_model(), username=username)
    posts = get_object_or_404(Post, id=post_id)
    users = posts.author
    num = Post.objects.filter(author=users).count()

    return render(request, 'post.html', {
        'posts': posts,
        'users': users,
        'num': num
    })

@login_required
def post_edit(request, username, post_id):
    posts = Post.objects.get(id=post_id)
    if posts.author.username == username:
        form = NewPost()
        return render(request, 'post_edit.html', {
            'form': form,
            'post_id': post_id,
            'username': username
        })
    else:
        return redirect(to=request.path)
    # тут тело функции. Не забудьте проверить,
    # что текущий пользователь — это автор записи.
    # В качестве шаблона страницы редактирования укажите шаблон создания новой записи
    # который вы создали раньше (вы могли назвать шаблон иначе)

def edit_confirm(request, username, post_id):
    posts = Post.objects.get(id=post_id)
    posts.text = request.POST.get('text')
    posts.group_id = request.POST.get('group')
    posts.save()
    return redirect(to=f'../../profile/{username}')


import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.encoders import encode_base64
from email.utils import COMMASPACE, formatdate

def mail(sender, pwd, to, subject, text, files=[]):
    msg = MIMEMultipart()
    msg["From"] = sender
    msg["To"] = COMMASPACE.join(to)
    msg["Date"] = formatdate(localtime=True)
    msg["Subject"] = subject
    msg.attach(MIMEText(text))
    for file in files:
        part = MIMEBase("application", "octet-stream")
        part.set_payload( open(file,"rb").read() )
        #Encoders.encode_base64(part)
        encode_base64(part)
        part.add_header("Content-Disposition", "attachment; filename='%s'"
                       % os.path.basename(file))
        msg.attach(part)
    # Note that we need to designate the remote SMTP server we want to use.
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.starttls()
    mailServer.login(sender, pwd)
    mailServer.sendmail(sender, to, msg.as_string())
    mailServer.close()


from math import *
from vpython import *
from heapq import nsmallest

brown = vector(0.4,0.2,0.6)

minRad = 1e6
d = 1e10

sunEarthDist = 1.49598e11
eSpeed = 29839.7170
mSpeed = 1022
earthMoonDist = 3.844e8

earthMass = 5.972e24
sunMass = 1.989e30
moonMass = 7.347673e22

ved = sphere(pos=vector(0, -d, 0),radius=minRad,color=color.rgb_to_hsv(brown))
tilden = sphere(pos=vector(0.8660254038 * d,0.5 * d,0),radius=minRad,color=color.white)
chamy = sphere(pos=vector(-0.8660254038 * d,0.5 * d,0),radius=minRad, color=color.yellow)

G = 6.67e-11

ved.mass = 1e23
tilden.mass = 1e23
chamy.mass = 1e23

b = 258.25
v8x = 0.93240737 * b
v8y = 0.86473146 * b

s = 1
#196.23746, maxdt = 2000
v = 196.23773
#100 - three rings
#250 - three rings but bodies start on inside

ved.velocity = vector(v, 0, 0)
ved.accel = vector(0, 0, 0)

tilden.velocity = vector(-0.5 * v, 0.8660254038 * v, 0)
tilden.accel = vector(0, 0, 0)

chamy.velocity = vector(-0.5 * v, -0.8660254038 * v, 0)
chamy.accel = vector(0, 0, 0)

ved.trail = curve(color=color.blue) #,retain=250)
tilden.trail = curve(color=color.red) #,retain=250)
chamy.trail = curve(color=color.green) #,retain=250)

ved.trail.append(pos = ved.pos)
tilden.trail.append(pos=tilden.pos)
chamy.trail.append(pos=chamy.pos)

#setup other variables
rTV = vector(0, 0, 0)
rTC = vector(0, 0, 0)
rCV = vector(0, 0, 0)
fTV = vector(0, 0, 0)
fTC = vector(0, 0, 0)
fCV = vector(0, 0, 0)
ved.force = vector(0, 0, 0)
tilden.force = vector(0, 0, 0)
chamy.force = vector(0, 0, 0)

def update():
    #update distances
    global rTV, rTC, rCV, fTV, fTC, fCV
    rTV = ved.pos - tilden.pos
    rTC = chamy.pos - tilden.pos
    rCV = ved.pos - chamy.pos
    fTV = ((-(G) * (tilden.mass * ved.mass)) * (1 / (mag2(rTV)))) * norm(rTV)
    fTC = ((-(G) * (tilden.mass * chamy.mass)) * (1 / (mag2(rTC)))) * norm(rTC)
    fCV = ((-(G) * (chamy.mass * ved.mass)) * (1 / (mag2(rCV)))) * norm(rCV)

    #update forces
    ved.force = fTV + fCV
    tilden.force = -(fTV + fTC)
    chamy.force = fTC - fCV

    #update accels
    ved.accel = ved.force / ved.mass
    tilden.accel = tilden.force / tilden.mass
    chamy.accel = chamy.force / chamy.mass

def energy():
    # checking for energy conservation: GPE + KE
    gpeTV = ((-(G) * (tilden.mass * ved.mass)) * (1 / (mag(rTV))))
    gpeTC = ((-(G) * (tilden.mass * chamy.mass)) * (1 / (mag(rTC))))
    gpeCV = ((-(G) * (chamy.mass * ved.mass)) * (1 / (mag(rCV))))
    totalGPE = gpeTV + gpeTC + gpeCV

    keVed = 0.5 * ved.mass * mag2(ved.velocity)
    keTilden = 0.5 * tilden.mass * mag2(tilden.velocity)
    keChamy = 0.5 * chamy.mass * mag2(chamy.velocity)
    totalKE = keVed + keTilden + keChamy

    return totalKE + totalGPE

#center of mass initialization
CM = sphere(pos=vector(0,0,0),radius=minRad * 0.1, color=color.magenta)
CM.trail = curve(color=color.magenta) #,retain=250)

#update before while loop starts
update()

maxDT = 500
minDT = 1
stableRate = 300
maxRate = 600

TVInitDist = mag(ved.pos - tilden.pos)
TCInitDist = mag(chamy.pos - tilden.pos)
CVInitDist = mag(ved.pos - chamy.pos)

initEnergy = energy()
#print(initEnergy)

time = 0
dt = maxDT

#lines between
a1 = curve(color = color.gray(0.5), retain = 2)
b1 = curve(color = color.gray(0.5), retain = 2)
c1 = curve(color = color.gray(0.5), retain = 2)
a2 = curve(color = color.gray(0.5), retain = 2)
b2 = curve(color = color.gray(0.5), retain = 2)
c2 = curve(color = color.gray(0.5), retain = 2)

run = True
showLines = True

while run:

    #determining rate -> keep program running same "speed"
    #newRate = stableRate * maxDT / dt
    #if(newRate > maxRate):
    #    newRate = maxRate
    rate(200)

    #update lines
    if(showLines):
        a1.append(tilden.pos, chamy.pos)
        b1.append(ved.pos, tilden.pos)
        c1.append(ved.pos, chamy.pos)

        a2.append(tilden.pos, CM.pos)
        b2.append(ved.pos, CM.pos)
        c2.append(CM.pos, chamy.pos)

    #center of mass
    cOfM = ((ved.pos * ved.mass) + (chamy.pos * chamy.mass) + (tilden.pos * tilden.mass)) / (ved.mass + chamy.mass + tilden.mass)
    CM.pos = cOfM

    #stopping state -> end sim if any masses collide
    if ((mag(rTV) < minRad) or (mag(rTC) < minRad) or (mag(rCV) < minRad)):
        run = False
        print("collision!")

    #mark last position
    prev = chamy.pos.x

    #update pos via verlet velocity
    ved.pos += (ved.velocity * dt) + ((0.5 * ved.accel) * (dt**2))
    chamy.pos += (chamy.velocity * dt) + ((0.5 * chamy.accel) * (dt**2))
    tilden.pos += (tilden.velocity * dt) + ((0.5 * tilden.accel) * (dt ** 2))

    #update accel and velocity
    vedAccelCurr = ved.accel
    chamyAccelCurr = chamy.accel
    tildenAccelCurr = tilden.accel
    update()
    ved.velocity += (vedAccelCurr + ved.accel) * (0.5 * dt)
    chamy.velocity += (chamyAccelCurr + chamy.accel) * (0.5 * dt)
    tilden.velocity += (tildenAccelCurr + tilden.accel) * (0.5 * dt)

    # #update via euler's integration (to test error)
    # update()
    #
    # ved.velocity += ved.accel * dt
    # tilden.velocity += tilden.accel * dt
    # chamy.velocity += chamy.accel * dt
    #
    # ved.pos += ved.velocity * dt
    # tilden.pos += tilden.velocity * dt
    # chamy.pos += chamy.velocity * dt

    #trails
    ved.trail.append(pos = ved.pos)
    tilden.trail.append(pos=tilden.pos)
    chamy.trail.append(pos=chamy.pos)

    CM.trail.append(pos=CM.pos)

    #mark current position
    curr = chamy.pos.x

    #determine period
    if((curr != 0) and (prev != 0)):
        if((curr / abs(curr)) > (prev / abs(prev))):
            print((chamy.pos.y - (-d)))

    #checking for energy conservation: GPE + KE
    pct = abs((energy() / initEnergy) - 1) * 100
    #print(pct, time / 86400, pct > 5)

    #stop program if error in energy > 5%
    #print(time / 86400)
    if(pct > 5):
        run = False
        print(pct, time / 86400, pct > 5)

    #determining dt based on distance
    avgDist = (mag(rTV) + mag(rTC) + mag(rCV)) * (1/3)
    TVCurrDist = mag(rTV)
    TCCurrDist = mag(rTC)
    CVCurrDist = mag(rCV)

    list = [TVCurrDist, TCCurrDist, CVCurrDist]
    minDist = nsmallest(1, list)
    dt = ceil(((minDist[0] / avgDist) * (maxDT - minDT)) + minDT)
    #failsafe
    if(dt > maxDT):
        dt = maxDT

     # update for next iteration
    time += dt

    #c.clear()

    #print(dt, newRate, minDist[0] / avgDist)


import hail as hl

#Import mt file
print("Reading in mts...")
pop_strings = ['AFR', 'AJ', 'AMR', 'EAS', 'FIN', 'NFE', 'SAS']
cd_tables = []
ibd_tables = []
uc_tables = []

for pop_string in pop_strings:
	cd_tables.append(hl.read_matrix_table('gs://ibd-exomes/' + pop_string + '_cd+control.mt'))
	ibd_tables.append(hl.read_matrix_table('gs://ibd-exomes/' + pop_string + '_ibd+control.mt'))
	uc_tables.append(hl.read_matrix_table('gs://ibd-exomes/' + pop_string + '_uc+control.mt'))

for test in ['wald', 'lrt', 'firth']:
	for i, pop_string in enumerate(pop_strings):
		print("Conducting " + test + " tests for " + pop_string + " population...")

		print("Crohn's disease...")
		cd_result = hl.logistic_regression_rows(test=test, y=cd_tables[i].has_cd, x=cd_tables[i].GT.n_alt_alleles(), covariates=[1, cd_tables[i].PC1, cd_tables[i].PC2, cd_tables[i].PC3, cd_tables[i].PC4])
		cd_result = cd_result.order_by(cd_result.p_value)
		cd_result.export('gs://ibd-exomes/' + pop_string + '_CD_' + test + '.tsv.bgz')

		print("Inflammatory bowel disease...")
		ibd_result = hl.logistic_regression_rows(test=test, y=ibd_tables[i].has_ibd, x=ibd_tables[i].GT.n_alt_alleles(), covariates=[1, ibd_tables[i].PC1, ibd_tables[i].PC2, ibd_tables[i].PC3, ibd_tables[i].PC4])
		ibd_result = ibd_result.order_by(ibd_result.p_value)
		ibd_result.export('gs://ibd-exomes/' + pop_string + '_IBD_' + test + '.tsv.bgz')

		print("Ulcerative colitis...")
		uc_result = hl.logistic_regression_rows(test=test, y=uc_tables[i].has_uc, x=uc_tables[i].GT.n_alt_alleles(), covariates=[1, uc_tables[i].PC1, uc_tables[i].PC2, uc_tables[i].PC3, uc_tables[i].PC4])
		uc_result = uc_result.order_by(uc_result.p_value)
		uc_result.export('gs://ibd-exomes/' + pop_string + '_UC_' + test + '.tsv.bgz')


#! /usr/bin/python

import pandas as pd

parameter_filename = "export_parameters_20200501_190002.csv"
elements_filename = "export_elements_20200501_190001.csv"

# derive date of file from filename
file_date = parameter_filename[18:len(parameter_filename)-11]

# create export filename
export_csv_file = "EcoData_{}.csv".format(file_date)

parameter_dataframe = pd.read_csv(parameter_filename)
elements_dataframe = pd.read_csv(elements_filename)

# drop rows from paramater dataframe where NaN appears in the DSL related columns
parameter_dataframe.dropna(subset = ["downstreamcurrrate"], inplace=True)

# Calculate the average of the different DSL related parameters and store in the parameters dataframe
parameter_dataframe['avgCurrDN'] = parameter_dataframe.groupby('serialnumber')['downstreamcurrrate'].transform('mean')
parameter_dataframe['avgMaxDn'] = parameter_dataframe.groupby('serialnumber')['downstreammaxrate'].transform('mean')
parameter_dataframe['avgCurrUp'] = parameter_dataframe.groupby('serialnumber')['upstreamcurrrate'].transform('mean')
parameter_dataframe['avgMaxUp'] = parameter_dataframe.groupby('serialnumber')['upstreammaxrate'].transform('mean')
print("averaging end: {}".format(datetime.now()))

# Remove duplicates from parameter dataframe to reduce size
parameter_dataframe.drop_duplicates(subset ="serialnumber", keep = 'first', inplace = True)

# map the downstream averages from the parameter file over into the elements file
elements_dataframe['avgCurrDN']=elements_dataframe['serialnumber'].map(parameter_dataframe.set_index('serialnumber')['avgCurrDN'])
elements_dataframe['avgMaxDn']=elements_dataframe['serialnumber'].map(parameter_dataframe.set_index('serialnumber')['avgMaxDn'])
elements_dataframe['avgCurUp']=elements_dataframe['serialnumber'].map(parameter_dataframe.set_index('serialnumber')['avgCurrUp'])
elements_dataframe['avgMaxUp']=elements_dataframe['serialnumber'].map(parameter_dataframe.set_index('serialnumber')['avgMaxUp'])

# export elements dataframe to a .csv file
elements_dataframe.to_csv(export_csv_file, index = False)


# -*- coding: utf8 -*-

"""
Usage:
  hookit [--scripts=<dir>] [--listen=<address>] [--port=<port>]

args:
  --scripts=<dir>     Where to look for hook scripts [default: .]
  --listen=<address>  Server address to listen on [default: 0.0.0.0]
  --port=<port>       Server port to listen on [default: 8000]
"""

import sys
import json
import socket
import logging
import os.path
from subprocess import Popen

from docopt import docopt
from github3 import GitHub
from ipaddress import ip_address, ip_network

try:
    from BaseHTTPServer import HTTPServer
    from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError:
    from http.server import HTTPServer
    from http.server import SimpleHTTPRequestHandler

logger = logging.getLogger('hookit')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())

WHITELIST = GitHub().meta().get('hooks', [])

def in_whitelist(client_ip):
    for ip in WHITELIST:
        if ip_address(u'%s' % client_ip) in ip_network(ip):
            return True
    return False

class HookHandler(SimpleHTTPRequestHandler):
    timeout = 10

    def do_GET(self):
        self.send_forbidden()

    def do_POST(self):
        # Reject all requests from non-Github IPs
        if not in_whitelist(self.client_address[0]):
            self.send_forbidden()
            return

        # Read POST data
        length = int(self.headers.get('Content-Length'))
        data = self.rfile.read(length).decode("utf-8")

        # Parse POST data and get payload
        try:
            payload = json.loads(data)
        except ValueError as e:
            logger.error('%s: %s' % (e, data))
            self.send_forbidden()
            return

        if 'zen' in payload and 'ref' not in payload:
            self.send_ok()
            return

        hook_trigger(payload)
        self.send_ok()

    def send_ok(self):
        self.send_response(200)
        self.end_headers()

    def send_forbidden(self):
        self.send_response(403)
        self.end_headers()

def hook_trigger(payload):
    owner = payload['repository']['owner']['name']
    repository = payload['repository']['name']
    branch = payload['ref'].split('/')[-1]
    commit = payload['after']

    jail = os.path.abspath(docopt(__doc__)['--scripts'])

    triggers = [
        os.path.abspath('%s/%s' % (jail, owner)),
        os.path.abspath('%s/%s/%s' % (jail, owner, repository)),
        os.path.abspath('%s/%s/%s/%s' % (jail, owner, repository, branch)),
    ]

    for trigger in triggers:
        # Check if absolute trigger path resides in jail directory
        if not os.path.commonprefix([trigger, jail]).startswith(jail):
            logger.warning('Tried to execute script outside jail: %s' % trigger)
            break

        # No action
        if not os.path.isfile(trigger):
            continue

        logger.debug('Executing %s' % trigger)
        Popen([trigger] + [
            '--owner=%s' % owner,
            '--branch=%s' % branch,
            '--repository=%s' % repository,
            '--commit=%s' % commit,
        ])
        break

def run():
    args = docopt(__doc__)
    host = args['--listen']

    try:
        port = int(args['--port'])
    except ValueError:
        logger.error('Binding port must be integer')
        sys.exit(1)

    logger.info('Now listening for webhooks on http://%s:%s...' % (host, port))

    try:
        http = HTTPServer((host, port), HookHandler)
        http.serve_forever()
    except (socket.gaierror, socket.error) as e:
        startup_error(e.strerror.capitalize())
    except OverflowError as e:
        startup_error('Port must be in interval 0-65535')
    except KeyboardInterrupt:
        print('')
        sys.exit(0)

def startup_error(message):
    logger.error('Could not start server: %s' % message)
    sys.exit(1)