#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: Jialiang Shi
try:
    from urllib.parse import quote
except ImportError:
    from urllib import quote

from gerrit.changes.revision.drafts import Drafts
from gerrit.changes.revision.comments import Comments
from gerrit.changes.revision.files import Files

class Revision(object):
    def __init__(self, project, change, revision, gerrit):
        self.project = project
        self.change = change
        self.revision = revision
        self.gerrit = gerrit

    def get_commit(self):
        """
        Retrieves a parsed commit of a revision.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/commit" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return self.gerrit.projects.get(self.project).get_commit(result.get("commit"))

    def get_description(self):
        """
        Retrieves the description of a patch set.
        If the patch set does not have a description an empty string is returned.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/description" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def set_description(self, input_):
        """
        Sets the description of a patch set.

        .. code-block:: python

            input_ = {
                "description": "Added Documentation"
            }

            change = gerrit.changes.get('myProject~stable~I10394472cbd17dd12454f229e4f6de00b143a444')
            revision = change.get_revision('3848807f587dbd3a7e61723bbfbf1ad13ad5a00a')
            result = revision.set_description(input_)

        :param input_: the DescriptionInput entity,
          https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#description-input
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/description" % (self.change, self.revision)
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.put(
            base_url, json=input_, headers=self.gerrit.default_headers
        )
        result = self.gerrit.decode_response(response)
        return result

    def get_merge_list(self):
        """
        Returns the list of commits that are being integrated into a target branch by a merge commit.
        By default the first parent is assumed to be uninteresting. By using the parent option another
        parent can be set as uninteresting (parents are 1-based).

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/mergelist" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return [
            self.gerrit.projects.get(self.project).get_commit(item.get("commit"))
            for item in result
        ]

    def get_revision_actions(self):
        """
        Retrieves revision actions of the revision of a change.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/actions" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def get_review(self):
        """
        Retrieves a review of a revision.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/review" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def get_related_changes(self):
        """
        Retrieves related changes of a revision. Related changes are changes that either depend on,
        or are dependencies of the revision.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/related" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def set_review(self, input_):
        """
        Sets a review on a revision, optionally also publishing draft comments, setting labels, adding reviewers or CCs,
        and modifying the work in progress property.
        A review cannot be set on a change edit. Trying to post a review for a change edit fails with 409 Conflict.

        .. code-block:: python

            input_ = {
                "tag": "jenkins",
                "message": "Some nits need to be fixed.",
                "labels": {
                    "Code-Review": -1
                },
                "comments": {
                      "sonarqube/cloud/project_badges.py": [
                            {
                                "line": 23,
                                "message": "[nit] trailing whitespace"
                            },
                            {
                                "line": 49,
                                "message": "[nit] s/conrtol/control"
                            },
                            {
                                "range": {
                                    "start_line": 50,
                                    "start_character": 0,
                                    "end_line": 55,
                                    "end_character": 20
                                },
                                "message": "Incorrect indentation"
                            }
                      ]
                }
            }

            change = gerrit.changes.get('myProject~stable~I10394472cbd17dd12454f229e4f6de00b143a444')
            revision = change.get_revision('3848807f587dbd3a7e61723bbfbf1ad13ad5a00a')
            result = revision.set_review(input_)

        :param input_: the ReviewInput entity,
          https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#review-input
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/review" % (self.change, self.revision)
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.post(
            base_url, json=input_, headers=self.gerrit.default_headers
        )
        result = self.gerrit.decode_response(response)
        return result

    def rebase(self, input_):
        """
        Rebases a revision.
        Optionally, the parent revision can be changed to another patch set through the RebaseInput entity.

        .. code-block:: python

            input_ = {
                "base" : "1234"
            }

            change = gerrit.changes.get('myProject~stable~I10394472cbd17dd12454f229e4f6de00b143a444')
            revision = change.get_revision('3848807f587dbd3a7e61723bbfbf1ad13ad5a00a')
            result = revision.rebase(input_)

        :param input_: the RebaseInput entity,
          https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#rebase-input
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/rebase" % (self.change, self.revision)
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.post(
            base_url, json=input_, headers=self.gerrit.default_headers
        )
        result = self.gerrit.decode_response(response)
        return result

    def submit(self):
        """
        Submits a revision.
        If the revision cannot be submitted, e.g. because the submit rule doesn’t allow submitting the revision or the
        revision is not the current revision, the response is 409 Conflict and the error message is contained in the
        response body.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/submit" % (self.change, self.revision)
        response = self.gerrit.requester.post(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def get_patch(self, zip_=False, download=False, path=None):
        """
        Gets the formatted patch for one revision.
        The formatted patch is returned as text encoded inside base64

        Adding query parameter zip (for example /changes/…/patch?zip) returns the patch as a single file inside of a
        ZIP archive. Clients can expand the ZIP to obtain the plain text patch, avoiding the need for a base64 decoding
        step. This option implies download.

        Query parameter download (e.g. /changes/…/patch?download) will suggest the browser save the patch as
        commitsha1.diff.base64, for later processing by command line tools.

        If the path parameter is set, the returned content is a diff of the single file that the path refers to.

        :param zip_:
        :param download:
        :param path:
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/patch" % (self.change, self.revision)

        if zip:
            endpoint += endpoint + "?zip"

        if download:
            endpoint += endpoint + "?download"

        if path:
            endpoint += endpoint + "?path=%s" % quote(path, safe="")

        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def submit_preview(self):
        """
        need fix bug
        Gets a file containing thin bundles of all modified projects if this change was submitted.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/preview_submit" % (
            self.change,
            self.revision,
        )
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def is_mergeable(self):
        """
        Gets the method the server will use to submit (merge) the change and an indicator if the change is currently mergeable.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/mergeable" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def get_submit_type(self):
        """
        Gets the method the server will use to submit (merge) the change.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/submit_type" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result

    def test_submit_type(self, input_):
        """
        Tests the submit_type Prolog rule in the project, or the one given.

        :param input_: the Prolog code
        :type: str
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/test.submit_type" % (
            self.change,
            self.revision,
        )
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.post(
            base_url, data=input_, headers={"Content-Type": "plain/text"}
        )
        result = self.gerrit.decode_response(response)
        return result

    def test_submit_rule(self, input_):
        """
        Tests the submit_rule Prolog rule in the project, or the one given.

        :param input_: the Prolog code
        :type: str
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/test.submit_rule" % (
            self.change,
            self.revision,
        )
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.post(
            base_url, data=input_, headers={"Content-Type": "plain/text"}
        )
        result = self.gerrit.decode_response(response)
        return result

    @property
    def drafts(self):
        return Drafts(change=self.change, revision=self.revision, gerrit=self.gerrit)

    @property
    def comments(self):
        return Comments(change=self.change, revision=self.revision, gerrit=self.gerrit)

    @property
    def files(self):
        return Files(change=self.change, revision=self.revision, gerrit=self.gerrit)

    def cherry_pick(self, input_):
        """
        Cherry picks a revision to a destination branch.

        .. code-block:: python

            input_ = {
                "message" : "Implementing Feature X",
                "destination" : "release-branch"
            }

            change = gerrit.changes.get('myProject~stable~I10394472cbd17dd12454f229e4f6de00b143a444')
            revision = change.get_revision('3848807f587dbd3a7e61723bbfbf1ad13ad5a00a')
            result = revision.cherry_pick(input_)

        :param input_: the CherryPickInput entity,
          https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#cherry-pick-commit
        :return:
        """
        endpoint = "/changes/%s/revisions/%s/cherrypick" % (self.change, self.revision)
        base_url = self.gerrit.get_endpoint_url(endpoint)
        response = self.gerrit.requester.post(
            base_url, json=input_, headers=self.gerrit.default_headers
        )
        result = self.gerrit.decode_response(response)
        return result

    def list_reviewers(self):
        """
        Lists the reviewers of a revision.

        :return:
        """
        endpoint = "/changes/%s/revisions/%s/reviewers" % (self.change, self.revision)
        response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
        result = self.gerrit.decode_response(response)
        return result


import json

from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect, resolve_url
from django.views.generic import ListView, UpdateView, CreateView, DetailView, DeleteView
from django.contrib import messages
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.renderers import TemplateHTMLRenderer

from .models import Post, Comment
from .forms import PostForm, CommentForm
from django.urls import reverse, reverse_lazy
from django.contrib.auth import get_user_model

# 함수기반
# def post_list(request):
#     post_list = Post.objects.filter(author=request.user)
#
#
#     return render(request, 'instagram/post_list.html', {
#         'post_list': post_list
#     })
#
# def post_create(request):
#     form = PostForm(request.POST, request.FILES)
#     pass
#
# def post_modify(request, pk):
#     pass
#
# def post_delete(request, pk):
#     pass

class PostListView(ListView):

    # def get_queryset(self):
    # get_queryset의 다음에 페이지네이터가 구현되어 있어서 paginate_by를 쓸 수 있다
    #         if not self.request.user.is_anonymous:
    #             self.queryset = Post.objects.filter(author=self.request.user)
    #             if not self.queryset:
    #                 self.queryset = None
    #             return self.queryset
    def get(self, request, *args, **kwargs):
        if not request.user.is_anonymous:
            qs = Post.objects.all() \
                .filter(
                Q(author__in=request.user.following_set.all()) |
                Q(author=request.user)
            )
            if qs:
                paginator, page, queryset, is_paginated = super().paginate_queryset(qs, 9)
                context = {
                    'paginator': paginator,
                    'page': page,
                    'is_paginated': is_paginated,
                    'post_list': queryset,
                }
                return render(request, 'instagram/post_list.html', context)
        return render(request, 'instagram/post_list.html', {
            'post_list': None
        })

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    form_class = PostForm

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.author = self.request.user
        messages.success(self.request, '포스팅 저장 완료')
        return super().form_valid(form)

class PostUpdateView(LoginRequiredMixin, UpdateView):
    model = Post
    form_class = PostForm

    def get(self, request, *args, **kwargs):
        self.object = get_object_or_404(Post, pk=kwargs['pk'])
        if self.object.author != request.user:
            messages.warning(self.request, '작성한 회원만 수정할 수 있습니다')
            return redirect(self.object)
        form = PostForm
        return super(PostUpdateView, self).get(form)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        if self.object.author == self.request.user:
            messages.success(self.request, '포스팅 수정 완료')
            form.save()
        else:
            messages.warning(self.request, '작성한 회원만 수정할 수 있습니다')
        return super().form_valid(form)

class PostDeleteView(LoginRequiredMixin, DeleteView):
    model = Post
    template_name_suffix = '_delete'
    success_url = 'instagram/post_list.html'

    def get(self, request, *args, **kwargs):
        self.object = get_object_or_404(Post, pk=kwargs['pk'])
        if self.object.author != request.user:
            messages.warning(self.request, '작성한 회원만 삭제할수 있습니다')
            return redirect(self.object)
        return super(PostDeleteView, self).get(Post)

    def post(self, request, *args, **kwargs):
        self.object = get_object_or_404(Post, pk=kwargs['pk'])
        if self.object.author != request.user:
            messages.warning(self.request, '작성한 회원만 삭제할수 있습니다')
            return redirect(self.object)
        self.object.delete()
        messages.success(request, '포스팅 삭제 완료')
        return redirect('instagram:post_list')

class PostDetailView(DetailView):
    model = Post

    def get_context_data(self, **kwargs):
        comment_list = Comment.objects.filter(post=kwargs.get('object'))
        comment_form = CommentForm()
        context = {
            'comment_list': comment_list,
            'form': comment_form
        }
        return super().get_context_data(**context)

    def post(self, request, **kwargs):
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.post = super().get_object()
            comment.save()
            return redirect('instagram:post_detail', pk=kwargs.get('pk'))

    def get(self, request, *args, **kwargs):
        pk = request.GET.get('pk', '')

        if pk:
            comment = Comment.objects.get(pk=pk)
            if comment.author != request.user:
                messages.warning(request, message="작성자가 아닙니다")
                return

            self.object = self.get_object()
            initial_dict = {
                "comment": comment.comment,
                "author": comment.author,
                "post": comment.post,
            }
            form = CommentForm(request.POST or None, initial=initial_dict)
            self.object = self.get_object()
            context = self.get_context_data(object=self.object)
            context["comment_edit_form"] = form
            context["comment_pk"] = comment.pk
            return self.render_to_response(context)
            # return render(request, "instagram/form.html", {
            #     "comment_edit_form": comment_edit_form,
            #     "comment_message": comment.comment
            # })

        else:
            self.object = self.get_object()
            context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.post = post
            comment.save()
            return redirect('instagram:post_detail', pk=pk)

    else:
        comment_list = Comment.objects.filter(post=pk)
        comment_form = CommentForm()

    return render(request, 'instagram/post_detail.html', {
        'post': post,
        'form': comment_form,
        'comment_list': comment_list,
    })

def post_like(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.user==post.author:
        messages.warning(request, "작성한 회원은 좋아요를 누를수 없습니다")
        return redirect('instagram:post_detail', pk=pk)
    post.like_user.add(request.user)
    messages.success(request, f"{post.author} 좋아요")
    redirect_url = request.META.get("HTTP_REFERER", "root")
    return redirect(redirect_url)

def post_unlike(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.like_user.remove(request.user)
    messages.success(request, f"{post.author} 좋아요 취소")
    redirect_url = request.META.get("HTTP_REFERER", "root")
    return redirect(redirect_url)

def comment_delete(request, pk, comment_pk):
    comment = get_object_or_404(Comment, pk=comment_pk)
    comment.delete()
    return redirect('instagram:post_detail', pk=pk)

def comment_edit(request, pk, comment_pk):
    comment = get_object_or_404(Comment, pk=comment_pk)
    form = CommentForm(request.POST or None,
                       instance=comment)
    if request.method == 'POST':
        if form.is_valid():
            comment_form = form.save(commit=False)
            comment_form.comment = request.POST.get("comment")
            comment_form.save()

    return redirect('instagram:post_detail', pk=pk)

# 함수기반
# @login_required
# def post_delete(request, pk):
#     model = Post
#     post = get_object_or_404(model, pk=pk)
#     if post.author == request.user:
#         messages.warning(request, '정말 삭제 하시겠습니까?')
#         if request.method == 'POST':
#             post.delete()
#             messages.success(request, '포스팅 삭제 완료')
#             return redirect('instagram:post_list')
#     else:
#         messages.warning(request, '작성한 회원만 삭제할 수 있습니다')
#         return redirect(post)
#     return render(request, 'instagram/post_delete.html', {
#     })

post_create = PostCreateView.as_view()
post_update = PostUpdateView.as_view()
post_list = PostListView.as_view()
post_delete = PostDeleteView.as_view()

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

from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from .serializers import PostSerializer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import generics
from rest_framework.generics import RetrieveAPIView
from rest_framework.decorators import api_view, action
from rest_framework.permissions import IsAuthenticated
from .permissions import IsAuthorOrReadonly

class PostViewSet(ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticated, IsAuthorOrReadonly]
    filter_backends = [SearchFilter, OrderingFilter]
    search_fields=['message']
    ordering=['id']
    @action(detail=False, methods=['GET'])
    def message(self, request):
        qs = self.get_queryset().filter(message__startswith='1')
        serializer = self.get_serializer(qs, many=True)
        return Response(serializer.data)

    @action(detail=True, methods=['PATCH'])
    def message_set(self, request, pk):
        instance = self.get_object()
        instance.message = '바뀜'
        instance.save(update_fields=['message'])
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

# class PostAPIView(generics.ListAPIView):
#     queryset = Post.objects.all()
#     serializer_class = PostSerializer
#
#
# class PostListAPIView(APIView):
#     def get(self, request):
#         qs = Post.objects.all()
#         serializer = PostSerializer(qs, many=True)
#         return Response(serializer.data)
#
# @api_view(['GET'])
# def post_api_view2(request):
#     qs = Post.objects.all()
#     serializer = PostSerializer(qs, many=True)
#     return Response(serializer.data)

class PostDetailAPIView(RetrieveAPIView):
    queryset = Post.objects.all()
    renderer_classes = [TemplateHTMLRenderer]
    template_name='instagram/mypost.html'

    def get(self, request, *args, **kwargs):
        post = self.get_object()
        return Response({
            'post': PostSerializer(post).data
        })


from lxml import html
from selenium import webdriver
import time
import pandas as pd
import itertools

options = webdriver.ChromeOptions()
#options.binary_location = browserpath
options.add_argument('headless')
options.add_argument('window-size=1200x900')

driver = webdriver.Chrome("chromedriver", chrome_options=options)
driver.get("https://www.dapp.com/search")

time.sleep(5)

for i in itertools.count():
	print("load page", i)
	vms = driver.find_elements_by_xpath('.//section[@class="view-more"]')
	if not len(vms):
		break
	vm = vms[0]
	vm.click()
	time.sleep(5)

tree = html.fromstring(driver.page_source)
links = tree.xpath(".//div[@class='item item']/a/@href")
names = tree.xpath(".//div[@class='item item']/a/div[@class='name']/text()")
cats = tree.xpath(".//div[@class='item item']/a/div[@class='extra-info']/div[@class='extra-name']/text()")[::2]

df = pd.DataFrame()
df["links"] = links
df["names"] = names
df["cats"] = cats

df.to_csv("dappcomall.csv", index=False)
print("done", len(df), "entries")


# Path: 3/solution.py

chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def part1(input: str) -> int:
    input_list = input.split("\n")

    total = 0

    for i in input_list:
        # split i in half
        first_half = i[0 : int(len(i) / 2)]
        second_half = i[int(len(i) / 2) :]
        for c in chars:
            if first_half.count(c) > 0 and second_half.count(c) > 0:
                total += chars.index(c) + 1

    return total

def part2(input: str) -> int:
    input_list = input.split("\n")

    total = 0

    for i in range(0, len(input_list), 3):
        first = input_list[i]
        second = input_list[i + 1]
        third = input_list[i + 2]
        for c in chars:
            if first.count(c) > 0 and second.count(c) > 0 and third.count(c) > 0:
                total += chars.index(c) + 1

    return total

if __name__ == "__main__":
    input = open("./3/input.txt", "r").read()
    print("Part 1: " + str(part1(input)))
    print("Part 2: " + str(part2(input)))


#! Python 3
# Regex search
# Write a program that opens all .txt files in a folder and searches for any line that matches a user-supplied regular expression

from pathlib import Path
import re, pyperclip

# write the regex of the pattern in the .txt to be searched
RegexSearch = re.compile(r'Freeman')  # the text can be anything, depending on the user

# write the path to the folder to be scanned for .txt files
p = Path('C://Users//opara//PycharmProjects')  # Path of the files to be opened and searched
files = list(p.glob('*.txt'))

# code to open all the files in the folder with the match
for file in files:
    print(f'OPENING {file}...')
    shit = open(file, 'r')
    result = RegexSearch.findall(shit.read())
    print(result)


# -*- coding: utf-8 -*-
"""Merges sliced renders in to one big plate
"""
from anima.dcc.fusion.toolbox import GenericTools

try:
    # for Fusion 6 and 7
    import PeyeonScript as bmf
except ImportError:
    # for Fusion 8+
    import BlackmagicFusion as bmf

from anima.dcc.fusion.utils import NodeUtils

class RenderMerger(object):
    """A tool to merge sliced renders"""

    def __init__(
        self, path="", slices_in_x=5, slices_in_y=5, plate_width=0, plate_height=0
    ):
        self.fusion = bmf.scriptapp("Fusion")
        self.comp = self.fusion.GetCurrentComp()

        self.fusion_version = float(
            self.fusion.GetAttrs("FUSIONS_Version").split(".")[0]
        )

        self.path = path
        self.slices_in_x = slices_in_x
        self.slices_in_y = slices_in_y
        self.plate_width = plate_height
        self.plate_height = plate_width

    def ui(self):
        """the UI for the script"""
        result = self.comp.AskUser(
            "Chose Slices",
            {
                1: {1: "Slice Sequence", 2: "FileBrowse", "Save": False},
                2: {
                    1: "Slice In Width",
                    2: "Slider",
                    "Min": 1,
                    "Max": 10,
                    "Default": 5,
                    "Integer": True,
                },
                3: {
                    1: "Slice In Height",
                    2: "Slider",
                    "Min": 1,
                    "Max": 10,
                    "Default": 5,
                    "Integer": True,
                },
            },
        )

        self.path = result["Slice Sequence"]
        self.slices_in_x = int(result["Slice In Width"])
        self.slices_in_y = int(result["Slice In Height"])

        self.do_merge()

    def calculate_total_width_height(self):
        """Calculates the total width and height of the resulting plate

        :return (int, int): Returns the width and height of the resulting plate
        """
        # calculate total width and height
        # instead of lock/unlock disable AutoClipBrowse temporarily
        auto_browse = GenericTools.disable_auto_clip_browse()
        loader = self.comp.Loader()
        GenericTools.set_auto_clip_browse(auto_browse)

        NodeUtils.set_node_attr(loader, "Clip", self.path)

        # set input
        loader.GetInputList()[10][0] = self.path
        # set clip time start
        loader.GetInputList()[15][0] = 0
        # set clip time end
        loader.GetInputList()[16][0] = 0

        attrs = loader.GetAttrs()
        plate_width = attrs["TOOLIT_Clip_Width"][1] * self.slices_in_x
        plate_height = attrs["TOOLIT_Clip_Height"][1] * self.slices_in_y

        # loader.Delete()

        return plate_width, plate_height

    def do_merge(self):
        """merges slices together"""
        width, height = self.calculate_total_width_height()

        bg = self.comp.Background()

        # set resolution
        NodeUtils.set_node_attr(bg, "Width", width)
        NodeUtils.set_node_attr(bg, "Height", height)

        # make it black with no alpha
        NodeUtils.set_node_attr(bg, "TopLeftRed", 0)
        NodeUtils.set_node_attr(bg, "TopLeftGreen", 0)
        NodeUtils.set_node_attr(bg, "TopLeftBlue", 0)
        NodeUtils.set_node_attr(bg, "TopLeftAlpha", 0)

        prev_merge = bg

        t = 0
        # instead of lock/unlock disable AutoClipBrowse temporarily
        auto_browse = GenericTools.disable_auto_clip_browse()
        for i in range(self.slices_in_y):
            # vertical stuff
            for j in range(self.slices_in_x):
                # horizontal stuff
                loader = self.comp.Loader()

                NodeUtils.set_node_attr(loader, "Clip", self.path)
                NodeUtils.set_node_attr(loader, "ClipTimeStart", t)
                NodeUtils.set_node_attr(loader, "ClipTimeEnd", t)

                merge = self.comp.Merge()

                # set center offset
                h_offset = 0.5 / self.slices_in_x + j * 1.0 / self.slices_in_x
                v_offset = 0.5 / self.slices_in_y + i * 1.0 / self.slices_in_y

                NodeUtils.set_node_attr(
                    merge, "Center", {1.0: h_offset, 2.0: v_offset, 3.0: 0.0}
                )

                # connect it to the previous merges output
                merge.Background = prev_merge
                merge.Foreground = loader

                prev_merge = merge
                t += 1
        GenericTools.set_auto_clip_browse(auto_browse)


class Animal(object):

    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
    def eat(self, amt):
        self.weight += amt
    def poop(self, amt):
        self.weight -= amt

class Cow(Animal):
    def moo(self, duration):
        return '{} says m{}!'.format(self.name, 'o' * duration)

class Chicken(Animal):
    def crow(self):
        return '{} likes to crow at the crack of dawn.'.format(self.name)

class Dog(Animal):
    def play(self):
        if self.age >= 10:
            duration = 5
        else:
            duration = 15
        return '{} is able to play for {} minutes.'.format(self.name, duration)

maximus = Dog('max', 10, 62)
print(maximus.play())

duke = Chicken('duke', 5, 7)
print(duke.crow())

bessie = Cow('bessie', 7, 1800)
print(bessie.moo(20))

bessie.eat(100)
bessie.poop(25)

print('{} weighs {} pounds.'.format(bessie.name, bessie.weight))


import os

Agregar = 1
Insertar = 2
Mostrar = 3
Buscar = 4
Eliminar = 5
Ordenar = 6
Limpiar = 7
Salir = 0
Frutas = []

def Imprimir_menu():
    os.system('cls')
    print(f'''              Frutas
        {Agregar}) Agregar
        {Insertar}) Insertar
        {Mostrar}) Mostrar
        {Buscar}) Buscar
        {Eliminar}) Eliminar
        {Ordenar}) Ordenar
        {Limpiar}) Limpiar Lista
        {Salir}) Salir''')

def Agregar_nueva():
    print('            Agregar')
    nueva = input('Nombre: ')
    Frutas.append(nueva)
    print('Agregada')

def Ingresar_nueva():
    print('            Insertar')
    nueva = input('Nombre: ')
    posicion = int(input('Posicion: '))
    Frutas.insert(posicion, nueva)
    print(f'Registro ingresado en la posicion {posicion}')

def Mostrar_lista():
    print('            Mostrar')
    if len(Frutas) > 0:
        for Nombre in Frutas:
            print(Nombre)
    else:
        print('No se a agregado nada')

def Buscar_registro():
    print('          Buscar')
    if len(Frutas) > 0:
        Nombre = input('Nombre a buscar: ')
        if Nombre in Frutas:
            cantidad = Frutas.count(Nombre)
            inicio = 0
            for i in range(cantidad):
                pos = Frutas.index(Nombre, inicio)
                print(f'{Nombre} se encuantra en la posicion {pos+1}')
                inicio = pos + 1
        else:
            print(f'No se encuantra {Nombre} en la lista')
    else:
        print('No hay regstros')

def Eliminar_registro():
    print('             Eliminar')
    if len(Frutas) > 0:
        for i in range(len(Frutas)):
            print(f'{i+1}. {Frutas[i]}')
        print('0. Cancelar')
        pos =  int(input(f'Posicion a eliminar desde 1 a {len(Frutas)}:'))
        if 0 < pos <= len(Frutas):
            Frutas.pop(pos-1)
            print('El registro se elimino')
        else:
            print('No se elimino ningun registro')
    else:
        print('No hay nada en la lista por borrar')

def Ordenar_registros():
    print('               Ordenar')
    if len(Frutas)  > 0:
        Frutas.sort()
        print('Se ordeno alfabeticamente')
    else:
        print('No se hay nada en la lista para poder ordenar')

def Limpiar_registros():
    print('             Limpiar')
    Frutas.clear()
    print('Se a borrado todo en la lista')

def main():
    Continuar = True
    while Continuar:
        Imprimir_menu()
        Opcion = int(input('Selecciona una opcion: '))
        os.system('cls')
        if Opcion == Agregar:
            Agregar_nueva()
        elif Opcion == Insertar:
            Ingresar_nueva()
        elif Opcion == Mostrar:
            Mostrar_lista()
        elif Opcion == Buscar:
            Buscar_registro()
        elif Opcion == Eliminar:
            Eliminar_registro()
        elif Opcion == Ordenar:
            Ordenar_registros()
        elif Opcion == Limpiar:
            Limpiar_registros()
        elif Opcion == Salir:
            print('Nos vemos prro')
            Continuar = False
        else:
             print('Opcion no valida')
        input('precione enter para continuar')
if __name__ == '__main__':
    main()


import pygame

#this will eventually extend sprite. Right now, it will just have a rectangle for collisions
class Flare():
    #pass position to
    def __init__(self, x, y, xdirection, ydirection):

        self.speed = 50
        self.direction = [0,0]

        magnitude = ((xdirection) ** 2 + (ydirection) ** 2) ** 0.5

        #normalize direction vector
        self.direction[0] = xdirection/magnitude
        self.direction[1] = ydirection/magnitude

        self.timerMax = 100
        self.timerCurrent = self.timerMax

        #for handling drawing
        self.max_radius = 500
        self.min_radius = 0

        self.max_light = 200
        self.min_light = 100

        self.frameWidth = 116
        self.frameHeight = 19

        self.sheet = pygame.image.load("Assets/Flare_Spritesheet.png").convert_alpha()
        self.sheet.set_clip(0, 0, self.frameWidth, self.frameHeight)
        self.image = self.sheet.subsurface(self.sheet.get_clip())

        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)

        self.frame = 0
        self.on_frames = { 0: (0, 0, self.frameWidth,  self.frameHeight), \
                           1: (126, 0, self.frameWidth,  self.frameHeight), \
                            }

        #control animation speed
        self.time_counter = 0
        self.frame_timer = 3

        self.light_surface = pygame.Surface((self.max_radius*2, self.max_radius*2), pygame.SRCALPHA)

        self.current_radius = int((1.0 - self.timerCurrent/self.timerMax) * self.min_radius + self.timerCurrent/self.timerMax * self.max_radius)
        self.current_light = int((1.0 - self.timerCurrent/self.timerMax) * self.min_light + self.timerCurrent/self.timerMax * self.max_light)

        #pygame.draw.rect(self.light_surface, self.color, self.rect)
        pygame.draw.circle(self.light_surface, (0,0,0, self.current_light), (self.max_radius, self.max_radius), self.current_radius)

    def draw_flare(self, world, camera_x, camera_y):
        world.blit(self.image, (self.rect.left + camera_x, self.rect.top + camera_y))

    def draw_light(self, darkness, camera_x, camera_y):
        draw_x = self.rect.centerx + camera_x
        draw_y = self.rect.centery + camera_y
        darkness.blit(self.light_surface, (draw_x - self.max_radius, draw_y - self.max_radius), special_flags = pygame.BLEND_RGBA_SUB)

    def moveX(self):
        self.rect.centerx += self.direction[0]*self.speed

    def collideHorizontal(self, wall):
        # handle flare X collision
        if self.direction[0] > 0:
            self.rect.right = wall.left
            #reverse direction but slow it down
            self.direction[0] = -self.direction[0]*0.5
        elif self.direction[0] < 0:
            self.rect.left = wall.right
            self.direction[0] = -self.direction[0]*0.5

        #if slow enough, set x direction to 0
        if abs(self.direction[0]) < 0.1:
            self.direction[0] = 0.0

    def moveY(self):
        #apply gravity
        self.direction[1] += 0.05
        #update y position
        self.rect.centery += self.direction[1]*self.speed

    def collideVertical(self, wall):
        # handle flare y collision
        if self.direction[1] > 0:
            self.rect.bottom = wall.top

            #let flare bounce, but slow it down
            self.direction[1] = -self.direction[1]*0.5
            #also, let flare x component slow down on a bounce
            self.direction[0] =  self.direction[0]*0.5

            #if slow enough, set x direction to 0
            if abs(self.direction[0]) < 0.1:
                self.direction[0] = 0.0
            #same with y direction:
            if abs(self.direction[1]) < 0.1:
                self.direction[1] = 0.0

        elif self.direction[1] < 0:
            self.rect.top = wall.bottom

            #let it bounce, no slow down
            self.direction[1] =  -self.direction[1]

    def get_frame(self, frame_set):

        if self.frame > (len(frame_set) - 1):
            self.frame = 0
        return frame_set[self.frame]

    def clip(self, clipped_rect):
        if type(clipped_rect) is dict:
            self.sheet.set_clip(pygame.Rect(self.get_frame(clipped_rect)))
        else:
            self.sheet.set_clip(pygame.Rect(clipped_rect))
        return clipped_rect

    def update(self):

        if self.timerCurrent > 0:
            self.timerCurrent -= 1

        self.clip(self.on_frames)

        self.time_counter += 1
        if self.time_counter > self.frame_timer:
            self.time_counter = 0
            self.frame += 1
        self.image = self.sheet.subsurface(self.sheet.get_clip())

        self.current_radius = int((1.0 - self.timerCurrent/self.timerMax) * self.min_radius + self.timerCurrent/self.timerMax * self.max_radius)
        self.current_light = int((1.0 - self.timerCurrent/self.timerMax) * self.min_light + self.timerCurrent/self.timerMax * self.max_light)

        self.light_surface.fill((0, 0, 0, 0))
        #pygame.draw.rect(self.light_surface, self.color, self.rect)
        pygame.draw.circle(self.light_surface, (0,0,0, self.current_light), (self.max_radius, self.max_radius), self.current_radius)


from unittest import TestCase
from test.test_prim import wg
from WeightedGraph.Dijkstra import Dijkstra

class TestDijkstra(TestCase):
    def test_dijkstra(self):
        dj = Dijkstra(wg)
        self.assertEqual([1, 3, 6, 7],
                         dj.dijkstra(1, 7),
                         "1-7 failed")
        self.assertEqual([4, 6, 7],
                         dj.dijkstra(4, 7),
                         "So it doesn't work")
        self.assertEqual([2, 5, 6],
                         dj.dijkstra(2, 6),
                         "2-6 failed")
        self.assertEqual([4, 3, 5],
                         dj.dijkstra(4, 5),
                         "4-5 failed")


# TODO: Add a progress bar to show the progress of the scraping process.
# TODO: Add a function to further clean the text, replacing special characters with their English equivalents.
# TODO: Make the script more robust by adding error handling.
# TODO: Add a function to extract the title of the webpage.
# TODO: Make the script more efficient by using multithreading.
# TODO: Make the whole script into a function that can be called from another script, which sets the parameters 'url', 'sleep_time', and 'text_container'.
# TODO: Add a function to automatically detect the content container.

import csv
import time
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
from nltk.tokenize import sent_tokenize

# root url of the website to be scraped
url = "https://python.langchain.com/en/latest/"
# css selector of the main content container to be scraped
text_container = "#main-content > div.bd-content > div.bd-article-container"
# time to sleep between each request
sleep_time = 2

# function to check if a link is valid
def is_valid_url(url):
    try:
        response = requests.head(url)
        return response.status_code == 200
    except requests.exceptions.RequestException:
        return False

# function to get all links from a webpage
def get_links(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")

    links = []
    for link in soup.find_all("a"):
        href = link.get("href")
        if href and not href.startswith("#"):
            href = urljoin(url, href)
            links.append(href)

    return links

# function to extract text and headers from a webpage
def extract_text_and_headers(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    main_content = soup.select_one(text_container)

    if main_content:
        headers = main_content.find_all(["h1", "h2", "h3", "h4", "h5", "h6"])
        paragraphs = main_content.find_all("p")
        text = " ".join([p.text for p in paragraphs])
    else:
        headers, text = [], ""

    return text, headers

# function to find the nearest header to a sentence
def nearest_header(headers, index):
    if not headers:
        return ""

    nearest = None
    distance = None

    for header in headers:
        header_index = header.parent.index(header)
        current_distance = abs(header_index - index)

        if distance is None or current_distance < distance:
            distance = current_distance
            nearest = header

    return nearest.text.strip()

# function to clean text
def clean_text(text):
    cleaned_text = " ".join(text.split())
    return cleaned_text

# function to extract sentences from a webpage
def extract_sentences(link, text, headers):
    sentences = sent_tokenize(text)
    header_sentences = [(link, nearest_header(headers, i), sentence) for i, sentence in enumerate(sentences)]
    return header_sentences

# get all links from the webpage
links = get_links(url)

# set up a list to store all sentences
all_sentences = []

# loop through all links and extract sentences
for link in links:
    if is_valid_url(link):
        text, headers = extract_text_and_headers(link)
        cleaned_text = clean_text(text)
        sentences = extract_sentences(link, cleaned_text, headers)
        all_sentences.extend(sentences)
        time.sleep(sleep_time)
        print(link)

# export all_sentences to a CSV file
with open("../output.csv", "w", newline="", encoding="utf-8") as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerow(["Link", "Nearest Header", "Sentence"])
    for row in all_sentences:
        csv_writer.writerow(row)

print("CSV file has been created.")


import pysal as ps
import pandas as pd
import numpy as np
import os
import csv

#function that averages all the values inside the uni id zones
#it gets the sum of rows, columns and zones that both rows and columns are in uni id list
#set all the values in targeted ids to 0
#set super zone rows, columns and intrazone to correspodning values
#get average value of rows, columns and superzone.
def avg_uni(matrix, uni_ids, univ_super):
    row_value = matrix.loc[uni_ids].sum()
    col_value = matrix[uni_ids].sum(axis = 1)
    in_zones =  matrix.ix[uni_ids, uni_ids].values.sum()

    matrix.loc[uni_ids] = 0
    matrix[uni_ids] = 0
    matrix.ix[uni_ids, uni_ids] = 0

    matrix.loc[univ_super] = row_value
    matrix[univ_super] = col_value
    matrix.ix[uni_ids, univ_super] = 0
    matrix.ix[univ_super, uni_ids] = 0

    matrix.set_value(univ_super, univ_super, in_zones)

    matrix.loc[univ_super] = matrix.loc[univ_super] / len(uni_ids)
    matrix[univ_super] = matrix[univ_super] / len(uni_ids)
    return matrix

#boolean to indentify if the model run is for the submodel unm dc_mc_property
#If true return True and ends function scope
#else retrun False
def is_uni(id):
    if id in [3,4]:
        return True
    return False

# Creates a df from a dbf
def dbf2df(dbfile, lower=True):  # Reads in DBF files and returns Pandas DF
    db = ps.open(dbfile)  # Pysal to open DBF
    d = {col: db.by_col(col) for col in db.header}  # Convert dbf to dictionary
    pandasDF = pd.DataFrame(d)  # Convert to Pandas DF
    if lower: #Make columns uppercase if wanted
        pandasDF.columns = pandasDF.columns.str.lower()
    db.close()
    return pandasDF

# functions that reads a txt file and return a dataframe matrix
# it creates a 919x919 dataframe
# reads the input file
# updates the 919 df with file values
def load_skim(skim, frame):
    with open(skim) as f:
        all_zones = pd.DataFrame(0.0, index=frame, columns=frame)
        df = pd.read_table(f, delim_whitespace=True,
                           dtype={'A': np.int64,
                                  'B': np.int64, 'C': np.float64},
                           names=('A', 'B', 'C'))
        df = df.pivot(index="A", columns="B", values="C")
        df = df.rename_axis(None)
        df.columns.name = None
        all_zones.update(df)
    return all_zones

# Retruns the name of the file, no extensions and path
def get_file_name(path):
    return os.path.basename(path).split(".")[0].strip().lower()

# Reads input files and returns the proper dataframe
# after reding the file it lower all column names
# set index to passed field
# updates to a dataframe with the  919 zones from 14 to 907
def read_file(file_input, frame, index_field=None):
    filename, file_extension = os.path.splitext(file_input)
    if file_extension == ".dbf":
        df = dbf2df(file_input)

    elif file_extension == ".csv":
        df = pd.read_csv(file_input)

    try:
        df.columns = map(str.lower, df.columns.values)
        df.index = df[index_field].astype(int)
        df.sort_index()
        all_zones = pd.DataFrame(0.0, index=frame, columns=df.columns.values)
        all_zones.update(df)
        return all_zones
    except ValueError:
        df = df[df[index_field] != 0]
        df.index = df[index_field].astype(int)
        df = df.rename_axis(None)
        return df

# creates a zeries of 1 and 0 depending if the zone is a model zone or not
# currenly zone models are 14 to 907
def get_model_zones(start=1, end=917, model_start=14, model_end=919):
    model_zones_list = []
    for i, x in enumerate(range(start, end + 1), 1):
        if model_start <= i <= model_end:
            z = 1
        else:
            z = 0
        model_zones_list.append(z)
    return pd.Series(data=model_zones_list, index=[i for i in range(start, end + 1)])

# set density variable
def set_denisty_variables(df, variable, area, numbin=4):
    # 1- it creates a new density column by dividing the vari_name by area.
    # 1.a vari_name comes from the dc_mc_property.xml Create_Density_Quartile_Variable tag.
    # 1.b area comes from the dc_mc_property.xml Area_Variable_Name tag.
    df.density = df['{}'.format(variable)].div(df['{}'.format(area)]).fillna(0)

    # 2 creates an ascending sorted list of the density field
    dsort = df.density.copy()
    dsort.sort_values(inplace=True)
    dsort = dsort.tolist()

    # Step3: Find density values at the location of 25%, 50%, and 75% in dsort
    # for bins it uses the number of rows of the socio table / num of bins
    bins = len(df.index) / numbin
    # treshold is  a list of 4 ranges value, each value is depending on evry
    # bins value.
    threshold = dsort[::bins]
    # by default is uses the last value of dsort +1 to ensure that we have a
    # max number for comparison
    threshold[-1] = dsort[-1] + 1
    return df.density, threshold

def emp_within(min, source, emp_vari, frame):
    empwithin = pd.Series(data=0.0, index=frame)
    for index, row in source.iterrows():
        temp = ((source.loc[index] > 0) & (source.loc[index] < min)) * 1
        indices = [key for key, value in temp.iteritems() if value == 1]
        try:
            numemp = emp_vari[indices].sum()
            empwithin[index] = np.float(numemp)
        except IndexError:
            empwithin[index] = np.float(0.0)
    return empwithin.divide(emp_vari.sum()).fillna(0)

# set density variable
# uses a threshold time such as 30 or 50 min
# It adds different skims to get total time stored in timedata
# temp is a pd series of 1 or 0 depending if the timedata.row[i] is less than the min threshold
# indices is a list that holds all the zones that are whitin the time treshold
# empvari is a series of the tot_emp field in the socio table. it sums all the emp inside indices
# for example if zone 17 and 18 are accesible within 30 min, it get totemp
# for zone 17 and 18 as empwithin
def set_accessibility_variables(min, matrices, skims_vari, emp_vari, frame):
    timedata = pd.DataFrame(0.0, index=frame, columns=frame)
    for i in skims_vari:
        timedata += matrices[i]
    empwithin = emp_within(min, timedata, emp_vari, frame)
    return empwithin

# return the model attributes from the xml file
#is uni and enrollment attributes needed for uni submodel
# enrollmnet values come from the dc_property xml via model enrollment value
def get_model_attributes(model, doubly=None, is_uni = False, enr_val = None):
    name = model.get('Name')
    model_id = model.get('Id')
    model_desc = model.get('Description')
    if is_uni:
        enroll_type =  model.get("Enrollment").lower()
        enrollment = enr_val[enroll_type]
        trip_rate = float(model.get("TripRate"))
        trip_count = trip_rate * enrollment
        if model.attrib.get("PandA") == 'Attraction':
            isPro = False
        else:
            isPro = True

        return {"name": model.get('Name'), "model_id": model.get('Id'), "model_desc": model_desc,
                "enrollment": enrollment, "isProduct": isPro, "trip_rate": trip_rate, "trip_count": trip_count}
    else:
        if model.get("Productions"):
            product = model.get("Productions").split(",")
            isPro = True
            if model.get("Attractions"):
                doubly = model.get("Attractions").split(",")
        else:
            product = model.get("Attractions").split(",")
            isPro = False

        return {"name": model.get('Name'), "model_id": model.get('Id'), "model_desc": model_desc,
                "product_columns": product, "isProduct": isPro, "doubly_columns": doubly}

# function that aggregates either productions or attracations based of product or doubly columns
# the columns come from the xml file through the get model attribute function
def get_panda(columns, panda_table, taz_start_id, taz_end_id):
    serie = pd.Series(
        data=0.0, index=[i for i in range(taz_start_id, taz_end_id + 1)])
    for col in columns:
        serie += panda_table[col.lower()]
    return serie

# Function to balance a matrix by doing an IPF
# it will balance rows for productions
# it will balance cols for attractions
def balancing_matrix(matrix, product, attract, log):
    tol = 0.005
    maxiter = 20
    relative = 1.0
    numzones = len(matrix.index)
    numiter = 0
    result = matrix

    while numiter < maxiter and relative > tol:

        normal_row = nomral_factor(result, attract)
        result = result.mul(normal_row, axis=1)

        log.msg("Iteration {}".format(numiter))
        normal_col = nomral_factor(result, product, index=1)
        result = result.mul(normal_col, axis=0)

        relative = computeErrors(result, attract,log)

        numiter += 1

    return result.fillna(0)

# Function used in the balancing matrix functions
# it gets the row or columns total depending on the sum index.
# index=0 means sumrows and index=1 means colsums
# it divides prod or attractions by sumtotal depending.
def nomral_factor(matrix, target, index=0):
    np.seterr(invalid='ignore')
    targetsum = matrix.sum(axis=index)
    return target.divide(targetsum).fillna(0)

# get the absolute and relative error for the ipf process
def computeErrors(matrix, attract,log):
    rowsum = matrix.sum(axis=0)
    adifference = pd.Series.abs(rowsum - attract)
    lowvalue = np.minimum(rowsum, attract)
    rdifference = adifference.div(lowvalue).fillna(0).max()
    log.msg("Relative error: {}".format(rdifference))
    log.msg("Absolute error: {}".format(adifference.max()))
    return rdifference

# function that aggregates any dataframe by area types.
# it groups by row then it groups by column
# manipulates the dataframe to exclude the 0 group which is usally non existent
def aggregated_result(matrix, group):
    max_group = int(group.max())
    if int(group.min()) <= 0:
        min_group = 1
    else:
        min_group = int(group.min())

    matrix = matrix.groupby(group).sum().groupby(group, axis=1).sum()
    matrix = matrix.rename_axis(None)
    matrix.columns = matrix.columns.astype(int)
    matrix.index = matrix.index.astype(int)
    return matrix.ix[:, min_group:max_group].loc[min_group:max_group]

def aggregated_result_validation(result, dis_mtx, socio, output_name, is_uni = False, superzone = None):
    #get index of the / to add aggregate to the name
    sindex = output_name.rfind("/", 1)
    output_file = output_name[:sindex + 1] + \
        '_aggregated_' + output_name[sindex + 1:]

    bins = {1: [0.0, 1.0], 2: [1.0, 2.5], 3: [2.5, 5.0], 4: [5.0, 7.5], 5: [7.5, 10.0], 6: [10.0, 15.0], 7: [15.0, 20.0],
            8: [20.0, 25.0], 9: [25.0, 30.0], 10: [30.0, 35.0], 11: [35.0, 40.0], 12: [40.0, 45.0], 13: [45.0, 30000.0]}

    labels = {1: '<=1.0', 2: '1.0>,<=2.5', 3: '2.5>,<=5.0', 4: '5.0>,<=7.5', 5: '7.5>,<=10', 6: '10>,<=15',
              7: '15>,<=20', 8: '20>,<=25', 9: '25>,<=30', 10: '30>,<=35', 11: '35>,<=40', 12: '40>,<=45', 13: '>45'}

    if is_uni:
        aggregated_result_dc_uni(result,output_file,dis_mtx,socio,bins,labels,superzone)
    else :
        aggregated_result_dc(result,output_file,dis_mtx,socio,bins,labels)

#Writes summary report for the aggregated pk,op files
def  aggregated_result_dc_uni(result, output_file, dis_mtx, socio, bins, labels, superzone):
    with open(output_file, "wb") as f:
        writer = csv.writer(f)
        ### finish this later. i dont think result series need to be transposed
        if result[superzone].sum() < result.loc[superzone].sum():
            result_series = result.loc[superzone]
            dis_mtx = dis_mtx.loc[superzone]
        else:
            result_series = result[superzone]
            dis_mtx = dis_mtx[superzone]

        # Writes distance summary
        for key in bins.keys():
            bin_ = bins[key]
            label = labels[key]
            temp = result_series.mul(((dis_mtx <= bin_[1]) & (dis_mtx > bin_[0])) * 1)
            trips = temp.values.sum()
            if key == 1:
                trips = result_series[superzone]
            if key == 2:
                trips = trips - result_series[superzone]
            writer.writerow([label,trips])
        intra_camp_trips = result_series.ix[superzone,superzone]
        writer.writerow(["Intra_Campus",intra_camp_trips])

        writer.writerow([])
        writer.writerow([])

        # Wrties aggregation by district summary
        agg_district = aggregated_result(result, socio.district)
        writer.writerow(["district"] + agg_district.columns.tolist())
        for row in agg_district.iterrows():
            index, data = row
            writer.writerow([index] + data.tolist())

        writer.writerow([])
        writer.writerow([])

        # Wrties aggregation by area type
        #since there a two county fields in the socio df, the country filed with int
        #is county_y. since it was read second. in order to change this, county from the socio dbf
        #needs have a different name
        agg_areatypes = aggregated_result(result, socio.county_y)
        writer.writerow(["county"] + agg_areatypes.columns.tolist())
        for row in agg_areatypes.iterrows():
            index, data = row
            writer.writerow([index] + data.tolist())

#Writes summary report for the aggregated pk,op files
def  aggregated_result_dc(result,output_file,dis_mtx,socio,bins,labels):
    with open(output_file, "wb") as f:
        writer = csv.writer(f)
        # Writes distance summary
        for key in bins.keys():
            bin_ = bins[key]
            label = labels[key]
            temp = result.mul(((dis_mtx <= bin_[1]) & (dis_mtx > bin_[0])) * 1)
            trips = temp.values.sum()
            writer.writerow([label, trips])
        total_miles = result * dis_mtx
        avg_miles = total_miles.values.sum() / result.values.sum()
        writer.writerow(['average distance', avg_miles])

        writer.writerow([])
        writer.writerow([])

        # Wrties aggregation by district summary
        agg_district = aggregated_result(result, socio.district)
        writer.writerow(["district"] + agg_district.columns.tolist())
        for row in agg_district.iterrows():
            index, data = row
            writer.writerow([index] + data.tolist())

        writer.writerow([])
        writer.writerow([])

        # Wrties aggregation by area type
        agg_areatypes = aggregated_result(result, socio["at"])
        writer.writerow(["areatypes"] + agg_areatypes.columns.tolist())
        for row in agg_areatypes.iterrows():
            index, data = row
            writer.writerow([index] + data.tolist())

#mode choice functions to determine walk times and shares from zone to zone
def intra_walk_share(zone, df, field):
    val = df[field].loc[df.index == zone].values[0]
    temp = df[field] * val
    return temp.values.tolist()

def external_walk_share(zone, df, field1, field2):
    val1 = df[field1].loc[df.index == zone].values[0]
    val2 = df[field2].loc[df.index == zone].values[0]
    temp = df[field2] * val1 +  df[field1] * val2
    return temp.values.tolist()

def intra_walk_time(zone, df, field):
    val = df[field].loc[df.index == zone].values[0]
    temp = df[field] + val
    return temp.values.tolist()

def external_walk_time(zone, df, field1, field2, field3, field4):
    val1 = df[field1].loc[df.index == zone].values[0]
    val2 = df[field2].loc[df.index == zone].values[0]
    val3 = df[field3].loc[df.index == zone].values[0]
    val4 = df[field4].loc[df.index == zone].values[0]
    temp = ((df[field3] + val1) * (df[field4] * val2) + (df[field1] + val3)  * (df[field2] * val4))
    return temp.values.tolist()

def drive_access_transit_shares(zone, df, field):
    val = 1 - df.sh_share.loc[df.index == zone].values[0] - df.md_share.loc[df.index == zone].values[0] - df.lg_share.loc[df.index == zone].values[0]
    temp = val * df[field]
    return temp.values.tolist()

#create walking time,share base dataframe
def create_walking_share_time_df(d):
    df = pd.DataFrame(index=pd.MultiIndex.from_product([d.index, d.index])).reset_index()
    df.columns = ['i', 'j']

    col = ["s_s_walk_share" ,"s_s_walk_time","s_m_walk_share","s_m_walk_time",
           "s_l_walk_share","s_l_walk_time","m_m_walk_share","m_m_walk_time",
           "m_l_walk_share","m_l_walk_time","l_l_walk_share","l_l_walk_time",
           "d_access_t_short_walk_share","d_access_t_short_walk_time",
           "d_access_t_med_walk_share","d_access_t_med_walk_time",
           "d_access_t_long_walk_share","d_access_t_long_walk_time","drive_only"]

    for c in col:
        df[c] = -99

    return df

#Function that deletes success file to run python script
def del_checker(file):
    filename = os.path.join(file, "01_Inputs/common/success.txt")
    if os.path.exists(filename):
        os.remove(filename)

def create_checker(file):
    filename = os.path.join(file, "01_Inputs/common/success.txt")
    with open(filename,"w") as f:
        f.write("1")


import getdata
import pickle
import logger

class prediction_():
    """
                       This class shall be used for predicting the user input with the model!!.
                       Written By: iNeuron Intelligence
                       Version: 1.0
                       Revisions: None
                       """

    def __init__(self,data):
        self.data = data
        self.file_object = open("Log.txt", 'a+')
        self.logwriter=logger.logger()

        self.filename = 'creditcardrandom.pickle'
        try:
            self.loaded_model = pickle.load(open('creditcardrandom.pickle', 'rb+'))
            self.logwriter.log(self.file_object,"Model loaded Successfully")
        except Exception as e:
            self.logwriter.log(self.file_object,f"Model doesn't load , Model loading failed due to {e}")
        try:
            self.predic = self.loaded_model.predict([[self.data.sex,self.data.marriage,self.data.age,self.data.bill_amt,
                                                      self.data.education,self.data.pay1]])
            self.logwriter.log(self.file_object, "Prediction Successfull")
        except Exception as e:
            self.logwriter.log(self.file_object,f"Prediction Failed due to {e}")


import numpy as np
import cv2 as cv
face_cascade = cv.CascadeClassifier('/home/lewis/gitRepos/face_tagger/haar.xml')
# eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
img = cv.imread('/home/lewis/test_imgs/DSC_9847.JPG')
# img.shape
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# # cv.namedWindow("main", cv.WINDOW_AUTOSIZE)

# faces = face_cascade.detectMultiScale(gray, 1.3, 3)
# for (x,y,w,h) in faces:
#     print 'hay'
#     print x, y, w, h
#     cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),8)
#     roi_gray = gray[y:y+h, x:x+w]
#     roi_color = img[y:y+h, x:x+w]
    # eyes = eye_cascade.detectMultiScale(roi_gray)
    # for (ex,ey,ew,eh) in eyes:
    #     cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# face_cascade = cv.CascadeClassifier('/home/lewis/haarside.xml')

# faces = face_cascade.detectMultiScale(gray, 1.3, 3)
# for (x,y,w,h) in faces:
#     print 'hay side'
#     print x, y, w, h
#     cv.rectangle(img,(x,y),(x+w,y+h),(255,160,12),8)
#     roi_gray = gray[y:y+h, x:x+w]
#     roi_color = img[y:y+h, x:x+w]

img = cv.resize(img, (img.shape[1] / 6, img.shape[0]/ 6))
cv.imshow('img',img)
a = chr(cv.waitKey(0) & 0xFF)
cv.destroyAllWindows()


# import module
from tkinter import *

# create object
root = Tk()

# Adjust size
root.geometry("600x600")

# setup window
root.title("Choose")

# Change the Label Text
def show():
    label.config( text = clicked.get() )

# Dropdoqn menu options
options = [
    "Explain",
    "Identify",
    "Me",
    "Quantum Moon",
    "You",
    "Eye of the Universe"
]

options2 = [
    "Explain",
    "Identify",
    "Me",
    "Quantum Moon",
    "You",
    "Eye of the Universe"
]

# datatype of menu test
clicked = StringVar()
clicked2 = StringVar()

#Initial menu text
clicked.set( "Explain" )
clicked2.set( "Identify" )

#Create Dropdown Menu
popupmenu = OptionMenu( root , clicked, *options )
popupmenu.grid(row=3, column=1)
popupmenu2 = OptionMenu( root , clicked2, *options2 )
popupmenu2.grid(row=3, column=2)

# create Label
label = Label( root , text = " " )

#Execute tkinter
root.mainloop()

# setup window
root.title("Choose")


#q5_upper_to_lower.py
#Changing upper case letters to lower case letters

#input upper case letter
ch = input("Enter Character: ")

#convert upper case to lower case
num = ord (ch) + 32

#print output
print (chr(num))


# %%
# load jwtd
import pandas as pd

input_file_name = './jwtd_v2.0/train.jsonl'
df = pd.read_json(input_file_name, orient='records', lines=True)

# %%
import CaboCha

def gen_chunks(tree):
    """
    構文木treeからチャンクの辞書を生成する
    """
    chunks = {}
    key = 0  # intにしているがこれはChunk.linkの値で辿れるようにしている

    for i in range(tree.size()):  # ツリーのサイズだけ回す
        tok = tree.token(i)  # トークンを得る
        if tok.chunk:  # トークンがチャンクを持っていたら
            chunks[key] = tok.chunk  # チャンクを辞書に追加する
            key += 1

    return chunks

def get_surface(tree, chunk):
    """
    chunkからtree内のトークンを得て、そのトークンが持つ表層形を取得する
    """
    surface = ''
    beg = chunk.token_pos  # このチャンクのツリー内のトークンの位置
    end = chunk.token_pos + chunk.token_size  # トークン列のサイズ

    for i in range(beg, end):
        token = tree.token(i)
        feature = token.feature.split(',')
        # print(feature[0])
        # if not feature[0] in ['名詞', '動詞']:
        #     return surface
        surface += token.surface  # 表層形の取得

    return surface

def make_pair(sentence, is_right):
    cp = CaboCha.Parser()  # パーサーを得る
    tree = cp.parse(sentence)  # 入力から構文木を生成
    # print(tree.toString(CaboCha.FORMAT_TREE))  # デバッグ用

    chunks = gen_chunks(tree)  # チャンクの辞書を生成する

    word_pairs = []

    for from_chunk in chunks.values():
        if from_chunk.link < 0:
            continue  # リンクのないチャンクは飛ばす

        # このチャンクの表層形を取得
        from_surface = get_surface(tree, from_chunk)

        # from_chunkがリンクしているチャンクを取得
        to_chunk = chunks[from_chunk.link]
        to_surface = get_surface(tree, to_chunk)
        word_pairs.append((from_surface, to_surface, is_right))

    return word_pairs

# %%
from tqdm import tqdm

def main():
    data = pd.DataFrame()
    with tqdm(total=len(df)) as pbar:
        for d in tqdm(df.itertuples()):
            pair = make_pair(d.post_text, 1)
            data = data.append(pair)
            pbar.update(1)
    data.columns = ['from_word', 'to_word', 'is_right']
    print(data.head())
    data.to_csv('./data/true_text.csv')

# %%
main()


import turtle
import time
import random

delay=0.1

# Set up screen
win = turtle.Screen()
win.title("Snake game")
win.bgcolor("green")
win.setup(width=600, height=600)
win.tracer(0)

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("blue")
head.penup()
head.goto(0, 100)
head.direction = "stop"

# Food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.shapesize(0.5, 0.5)
food.goto(0, 100)

segments = []

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0  High Score: 0", align="center", font=("Courier", 24, "bold"))

# Define coordinates
def move():
    if head.direction == "up":
        y = head.ycor()  # y Coordinate of the turtle
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()  # y Coordinate of the turtle
        head.sety(y - 20)

    if head.direction == "right":
        x = head.xcor()  # x Coordinate of the turtle
        head.setx(x + 20)

    if head.direction == "left":
        x = head.xcor()  # x Coordinate of the turtle
        head.setx(x - 20)

# Define movement rules
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_right():
    if head.direction != "left":
        head.direction = "right"

def go_left():
    if head.direction != "right":
        head.direction = "left"

# Listen to keyboard input
win.listen()
win.onkeypress(go_up, "w")
win.onkeypress(go_down, "s")
win.onkeypress(go_right, "d")
win.onkeypress(go_left, "a")

# main game loop
while True:
    win.update()

    # check for collision with wall
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"

        # hide  the segments
        for segment in segments:
            segment.goto(1000, 1000)

        # clear segment list
        segments.clear()

    # Check for collision with the food
    if head.distance(food) <20:
        # Move food to a random new position
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        food.goto(x, y)

        # Add a new segment to turtle body
        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

    # move the end segment (in this case: the newly added part of the snakes boy) in reverse order
    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x, y)

    # move segment 0 to where the head is
    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)

    move()

    #check for body collisions
    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"

            # Hide the segments
            for segment in segments:
                segment.goto(1000, 1000)

            segments.clear()

    time.sleep(delay)

wn.mainloop()


import tweepy
import json
from twitter_credentials import CONSUMER_KEY,ACCESS_TOKEN_SECRET,ACCESS_TOKEN,CONSUMER_SECRET

def authentication():
    try:
        auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        return auth
    except:
        print('Error Authentication')

def trending_topics():
    valid_token=authentication()
    api = tweepy.API(valid_token)
    trends = api.trends_place(1)

    data = trends[0]

    data_keys=data.keys()

    trends = data['trends']
    names = [i['name'] for i in trends]
    tweet_vol = [i['tweet_volume'] for i in trends]
    location = data['locations']
    started_at=data['created_at']

    return (names,tweet_vol,location,started_at)

#print(trending_topics())


import re, html, html2markdown, requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

readmeurl = 'https://raw.githubusercontent.com/mytechnotalent/Reverse-Engineering-Tutorial/master/README.md'

# emulating a browser here
fheaders = {
    'User-Agent'        : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    'Accept'            : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Language'   : 'en-US,en;q=0.9',
    'Accept-Encoding'   : 'gzip, deflate, br',
    'Sec-Fetch-Mode'    : 'navigate',
    'Sec-Fetch-Dest'    : 'document',
    'Sec-Fetch-Site'    : 'none',
    'Sec-Fetch-User'    : '?1',
    'DNT'               : '1',
    'Connection'        : 'close'
}

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)

def processfName(filen: str):
    '''
    Processes file names to convert them to files
    '''
    fm = filen.lower().replace(' ', '-').replace('-–-', '-').replace(
        '---', '-').replace(':', '').replace('.', '').replace(
        ',', '').replace('[', '').replace(']', '').replace('/', ''
        ).replace('"', '').replace('?', '').replace('&',
        '').replace('=', '').strip()
    fm += '.md'
    return fm

def downloadImage(link: str):
    '''
    Downloads an image from a given URL
    '''
    filename = 'imgs/{}.jpg'.format(link.split('?')[0].split('/')[-1])
    with open(filename, 'wb+') as imgf:
        imgf.write(requests.get(link, headers=fheaders).content)
    return '/{}'.format(filename)

def processContent(source: str):
    '''
    Processes content for getting images and code properly
    '''
    # processing images
    soup = BeautifulSoup(source, 'html.parser')
    for img in soup.find_all('img'):
        if not img.get('data-delayed-url'):
            lnk = img.get('src')
            if lnk == "//:0":
                lnk = img.get('data-li-src')
        else:
            lnk = img.get('data-delayed-url')
        ntag = soup.new_tag('img', src=downloadImage(html.unescape(lnk)))
        img.replaceWith(ntag)
    texts = ['''For a complete table of contents of all the lessons please click below as it will give you a brief of each lesson in addition to the topics it will cover. https://github.com/mytechnotalent/Reverse-Engineering-Tutorial''', '''UNDER NO CONDITIONS ARE YOU TO EVER USE THIS EDUCATION TO CAUSE HARM TO ANY SYSTEM OF ANY KIND AS I AM NOT RESPONSIBLE! THIS IS FOR LEARNING PURPOSES ONLY!''', '''
''']
    toput = soup.__str__()
    for i in texts:
        if i in toput:
            toput = toput.replace(i, '')
    return toput

def grabContent(pgsrc: str, category: str, fname: str):
    '''
    Grabs blog content off the linkedin page
    '''
    rex = r"
(
.*)

<h3 class=\"ugc-post-bar__published_by|\"author-info author-info__container)"
    patt = re.compile(rex, re.DOTALL | re.MULTILINE)
    content = processContent(re.search(patt, pgsrc).group(1))
    content = '
{}
{}'.format(fname, content)
    print('\n\n'+content+'\n\n')
    dirname = 'pages/{}'.format(processfName(fname))
    if category not in open('SUMMARY.md', 'r').read():
        with open('SUMMARY.md', 'a') as wf:
            wf.write('* [{}]({})\n'.format(category, 'pages/{}'.format(processfName(category))))
    toadd = '\t* [{}]({})'.format(fname, dirname)
    with open(dirname, 'w+') as wf:
        wf.write(html2markdown.convert(content))
    with open('SUMMARY.md', 'a') as wf:
        wf.write(toadd+'\n')

def grabLinks(source: str):
    '''
    Grabs links to the linkedin site
    '''
    patt = re.compile(r'(?i)click\s\[[a-z]{4}\]\((.*?)\)', re.MULTILINE)
    fname = re.compile(r'(?i)^##\s*Lesson\s*\d{1,5}:(.+?)\((.*?)\)', re.MULTILINE)
    rexmatch = [i for i in patt.finditer(source)]
    rexname = [i for i in fname.finditer(source)]
    for match, name in zip(rexmatch, rexname):
        if match.group(1).startswith('https://www.linkedin.com/pulse/'):
            print("Visiting link:", match.group(1))
            driver.get(match.group(1))
            grabContent(driver.page_source, name.group(1).strip(), name.group(2))

if __name__ == '__main__':
    # init summary.md
    with open("SUMMARY.md", 'w') as wf:
        wf.write('# Summary\n\n')
    # visit raw URL
    print("Visiting homepage...")
    driver.get(readmeurl)
    grabLinks(driver.page_source)
    driver.quit()


import matplotlib.pyplot as plt
import time
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from sklearn.metrics import classification_report
import numpy as np
from tensorflow.keras.datasets import cifar10
from sklearn.preprocessing import LabelBinarizer
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from model import miniGoogleNet

def lr_schedule(epoch):
    lr = 0.01
    if epoch > 90:
        lr = 0.0001
    elif epoch > 50:
        lr = 0.001
    elif epoch > 40:
        lr = 0.01
    print("lr = ", lr)
    return lr

# init
# initialize the initial learning rate, batch size, and number of epochs to train for
INIT_LR = lr_schedule(0)
BATCH_SIZE = 128
NUM_EPOCHS = 100

# initialize the label names for the CIFAR-10 dataset
labelNames = ["airplane", "automobile", "bird", "cat", "deer", "dog",
              "frog", "horse", "ship", "truck"]

# load the CIFAR-10 dataset
print("[INFO] loading CIFAR-10 dataset...")
((trainX, trainY), (testX, testY)) = cifar10.load_data()

# scale the data to the range [0, 1]
trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0

# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=18, zoom_range=0.15,
                         width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
                         horizontal_flip=True, fill_mode="nearest")

print('Loading model...')
model = miniGoogleNet(32, 32, 3, len(labelNames))

# initialize the optimizer and compile the model
opt = SGD(lr=INIT_LR, momentum=0.9, decay=INIT_LR / NUM_EPOCHS)
print("[INFO] training network...")
model.compile(loss="categorical_crossentropy", optimizer=opt,
              metrics=["accuracy"])

# define the checkpoint
filepath = "/content/drive/My Drive/Colab/checkpoint_new.h5/"
lr_scheduler = LearningRateScheduler(lr_schedule)
# define the checkpoint
checkpoint = ModelCheckpoint(filepath, monitor='loss', mode='min', save_best_only=True, verbose=1)
callbacks_list = [checkpoint, lr_scheduler]

# start
start = time.time()
# train the network
H = model.fit_generator(
    aug.flow(trainX, trainY, batch_size=BATCH_SIZE),
    validation_data=(testX, testY),
    steps_per_epoch=trainX.shape[0] // BATCH_SIZE,
    epochs=NUM_EPOCHS,
    verbose=1,
    callbacks=callbacks_list)

duration = time.time() - start
print("{} s to get output".format(duration))

import pickle

with open('/content/drive/My Drive/Colab/new2tt', 'wb') as file_pi:
    pickle.dump(H.history, file_pi)

# evaluate the network
import time

print("[INFO] evaluating prediction...")
start = time.time()
predictions = model.predict(testX, batch_size=BATCH_SIZE)
duration = time.time() - start
print("{} s to get output".format(duration))
print(classification_report(testY.argmax(axis=1),
                            predictions.argmax(axis=1), target_names=labelNames, digits=5))

# determine the number of epochs and then construct the plot title
N = np.arange(0, NUM_EPOCHS)
title = "Training Loss and Accuracy on CIFAR-10)"

# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="val_loss")
plt.plot(N, H.history["accuracy"], label="train_acc")
plt.plot(N, H.history["val_accuracy"], label="val_acc")
plt.title(title)
plt.xlabel("Epoch №")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.savefig('/content/drive/My Drive/Colab/checkpoint_new.png')

model.summary()


class Solution:
    def count_set_bit(self, n):
        m = n
        if n == 0:
            return 0
        counter = 0
        while n:
            counter += (n&1)
            n >>= 1
        print(m, counter)
        return counter
    def sortBySetBitCount(self, arr, n):
        # Your code goes here
        # x = list(range(len(arr)))
        # x.sort(key=lambda i: (-self.count_set_bit(arr[i]), i))
        y = [i[1] for i in sorted(enumerate(arr), key=lambda item: (-self.count_set_bit(item[1]), item[0]))]

        return y

arr = [5, 2, 3, 9, 4, 6, 7, 15, 32]
print(Solution().sortBySetBitCount(arr, len(arr)))

DSASheet/3_SearchingAndSorting/2/Sort by Set Bit Count/Solution.py


class Cliente:

    def __init__(self, nome, cpf, telefone, dtnasc, endereco, sexo, status=True, id=None):
        self.id = id
        self.nome = nome
        self.cpf = cpf
        self.telefone = telefone
        self.dtnasc = dtnasc
        self.endereco = endereco
        self.sexo = sexo
        self.status = status

    def __str__(self):
        return f'Nome: {self.nome}\n\t Telefone: {self.telefone}\n\t' \
               f'Endereço: {self.endereco}'

    def get_data_dict(self):
        return {'id': self.id,
                'nome': self.nome,
                'cpf': self.cpf,
                'telefone': self.telefone,
                'dtnasc': str(self.dtnasc),
                'endereco': self.endereco,
                'sexo': self.sexo,
                'status':self.status}


class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        def find(root, node, path):
            if not root:
                return
            if root == node:
                return path + [node]
            return find(root.left, node, path + [root]) or find(root.right, node, path + [root])

        first =  find(root, p, [])
        second = find(root,q, [])

        for i in range(min(len(first), len(second))):
            if i > 0 and first[i] != second[i]:
                return first[i-1]
            if i == min(len(first), len(second)) - 1:
                return first[i]

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if not root:
            return
        if root == p or root == q:
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if not left or not right:
            return left or right

        return root

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if root == p or root == q:
            return root

        left = right = None
        if root.left:
            left = self.lowestCommonAncestor(root.left, p, q)
        if root.right:
            right = self.lowestCommonAncestor(root.right, p, q)

        if left and right:
            return root
        else:
            return left or right

236. Lowest Common Ancestor of a Binary Tree.py


"""
문제 : 황선자씨는 우주신과 교감을 할수 있는 채널러 이다.
하지만 우주신은 하나만 있는 것이 아니기때문에 황선자 씨는 매번 여럿의 우주신과 교감하느라 힘이 든다.
이러던 와중에 새로운 우주신들이 황선자씨를 이용하게 되었다.
하지만 위대한 우주신들은 바로 황선자씨와 연결될 필요가 없다.
이미 황선자씨와 혹은 이미 우주신끼리 교감할 수 있는 우주신들이 있기 때문에 새로운 우주신들은
그 우주신들을 거쳐서 황선자 씨와 교감을 할 수 있다.
우주신들과의 교감은 우주신들과 황선자씨 혹은 우주신들 끼리 이어진 정신적인 통로를 통해 이루어 진다.
하지만 우주신들과 교감하는 것은 힘든 일이기 때문에 황선자씨는 이런 통로들이 긴 것을  좋아하지 않는다.
왜냐하면 통로들이 길 수록 더 힘이 들기 때문이다.
또한 우리들은 3차원 좌표계로 나타낼 수 있는 세상에 살고 있지만 우주신들과
황선자씨는 2차원 좌표계로 나타낼 수 있는 세상에 살고 있다. 통로들의 길이는 2차원 좌표계상의 거리와 같다.
이미 황선자씨와 연결된, 혹은 우주신들과 연결된 통로들이 존재한다.
우리는 황선자 씨를 도와 아직 연결이 되지 않은 우주신들을 연결해 드려야 한다.
새로 만들어야 할 정신적인 통로의 길이들이 합이 최소가 되게 통로를 만들어 “빵상”을 외칠수 있게 도와주자.
입력 : 첫째 줄에 우주신들의 개수(N<=1,000) 이미 연결된 신들과의 통로의 개수(M<=1,000)가 주어진다.
두 번째 줄부터 N개의 줄에는 황선자를 포함하여 우주신들의 좌표가 (0<= X<=1,000,000), (0<=Y<=1,000,000)가 주어진다.
그 밑으로 M개의 줄에는 이미 연결된 통로가 주어진다. 번호는 위의 입력받은 좌표들의 순서라고 생각하면 된다. 좌표는 정수이다.
출력 : 첫째 줄에 만들어야 할 최소의 통로 길이를 출력하라. 출력은 소수점 둘째짜리까지 출력하여라.
"""
import math
import sys
input=sys.stdin.readline

def get_distance(p1,p2):
    a=p1[0]-p2[0]
    b=p1[1]-p2[1]
    return math.sqrt((a*a)+(b*b))

# n의 꼭대기에 있는 parent를 찾는과정이다.
def get_parent(parent,n):
    if parent[n]==n:
        return n
    return get_parent(parent,parent[n])

# a와 b 를 각각 꼭대기 까지 간다음에 그 부모노드의 값이 작은쪽이 그것의 부모노드가 된다.
def union_parent(parent,a,b):
    a=get_parent(parent,a)
    b=get_parent(parent,b)
    if a<b:
        parent[b]=a
    else:
        parent[a]=b

# a와 b 의 부모노드가 같은지 판별하는것 그두게의 부모노드가 같다면 True 반환
def find_parent(parent,a,b):
    a=get_parent(parent,a)
    b=get_parent(parent,b)
    if a==b:
        return True
    else:
        return False

edges=[]
parent={}
locations=[]
n,m=map(int,input().split())
# n: 우주신 개수 , m :이미 연결된 통로의 개수
for _ in range(n):
    # 좌표를 넣어준다.
    x,y=map(int,input().split())
    locations.append((x,y))

# 좌표의 개수 =n
length=len(locations)

# i가 0부터 n-2까지이다.
for i in range(length-1):
    # j 는 1부터 n-1 까지이다.
    for j in range(i+1,length):
        # 간선에 모든 간선간의 거리와 그 좌표를 각각 넣어준다. 0: 1 2 3 4 ..n-1 , 1:2,3,4,... n-1 ,,, n-2:n-1
        edges.append((i+1,j+1,get_distance(locations[i],locations[j])))

# 1부터 n 까지 자기자신을 부모라고 지정해둔다.
for i in range(1,n+1):
    parent[i]=i

for i in range(m):
    a,b=map(int,input().split())
    # a와 b 중에서 더 작은 값이 부모노드가 된다.
    union_parent(parent,a,b)

# 거리가 짧은 순으로 정렬하여서 연결안되어있는 노드들중에서 짧은거부터 연결되도록 한다.
edges.sort(key=lambda data:data[2])

result=0
for a,b,cost in edges:
    # find_parent 가 True 면 a,b가 같은 부모를 가지고 있다는 것이다.
    # 두개의 부모가 다르다면 그 두개를 연결시켜준다.
    if not find_parent(parent,a,b):
        union_parent(parent,a,b)
        result+=cost

print(f"{result:.2f}")


# import required files and modules

import requests
from bs4 import BeautifulSoup
import pandas as pd

# set the headers and user string
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
}

def product(url):
    # send a request to fetch HTML of the page

    response = requests.get(url, headers=headers)
    if response.status_code != 200:
      print('Sorry cannot fetch data for this product right now!!')
      exit()
    #print(response)
    # create the soup object
    soup = BeautifulSoup(response.content, 'html.parser')

    # change the encoding to utf-8
    soup.encode('utf-8')

    title = soup.find('span',attrs={'class': 'B_NuCI'}).get_text()
    price = soup.find('div',attrs={'class': '_30jeq3 _16Jk6d'}).get_text()

    th=[]
    th.append('Title')
    th.append('Price')
    for td in soup.find_all('td',attrs={'class':'_1hKmbr col col-3-12'},text=True):
        th.append(td.get_text())

    td=[]
    td.append(title)
    td.append(price)
    for t in soup.find_all('li',attrs={'class':'_21lJbe'},text=True):
        td.append(t.get_text())

    if len(th)==2:
      for i in soup.find_all('div',attrs={'class':'col col-3-12 _2H87wv'},text=True):
        th.append(i.get_text())

      for t in soup.find_all('div',attrs={'class':'col col-9-12 _2vZqPX'},text=True):
        td.append(t.get_text())

    if len(th)>len(td):
      th.pop(2)
    else:
      if len(th)<len(td):
        td.pop(2)

    data={"Features": th,
      "Details": td
       }

    df=pd.DataFrame(data=data)

    return df
n=int(input())

list_of_df=[]

for i in range(0,n):
  url=input()
  list_of_df.append(product(url))

feature={ }
final={}

list_of_feature=[]

for df in list_of_df :
  for index,rows in df.iterrows():
    if rows["Features"] in feature:
       h=1
    else :
      list_of_feature.append(rows["Features"])
      feature[rows["Features"]]=1

final["Features"]=list_of_feature
heading='Details of Product '
for i in range(0,n):
  final[heading+str(i+1)]=[]

for f in list_of_feature:
  i=0
  for df in list_of_df :
    flag=0
    for index,rows in df.iterrows():
      if rows["Features"]==f:
        flag=1
        final[heading+str(i+1)].append(rows["Details"])
    if flag==0:
       final[heading+str(i+1)].append('NA')
    i+=1

final_df=pd.DataFrame(data=final)
final_df.to_excel("data.xlsx",index=False)


# Twitter credentials
ACADEMIC_TOKEN = "YOUR_TOKEN"
PRIVATE_TOKEN = "YOUR_TOKEN"

# MongoDB Database and collection names
MONGO_DB_NAME = "twitter"
STREAM_COLLECTION = "stream"
CONVERSATIONS_COLLECTION = "conversations"

# Database address
DB_ADDRESS = "mongodb://localhost:27017"

# Requested twitter-data
REQUESTED_FIELDS = {"tweet.fields": 'author_id,conversation_id,created_at,id,public_metrics,text',
                    "expansions": 'author_id,referenced_tweets.id,in_reply_to_user_id',
                    "media.fields": "type,preview_image_url,url",
                    "user.fields": 'created_at,name,public_metrics,url,username'}

# Stream filter rules in a list of dicts
FILTER_RULES = [{"value": '"enter your filter rules" -is:retweet', "tag": "filtertag"}]


"""
A tool that connects with the available blendshapes.
|-----------------------------------|
|  control_name:       [          ] |
|  connection_name:    [translateX] |
|  -------------------------------- |
|  blend_shape_node    [          ] |
|  blend_shape_attrib  [          ] |
| --------------------------------- |
|   [     button: connect       ]   |
|---------------------------------- |
"""

# import standard modules
from PySide2 import QtCore, QtGui, QtWidgets

# import local modules
from deformers import blendshape_utils
from maya_utils import ui_utils
from maya_utils import object_utils
from maya_utils import animation_utils

# reloads
reload(ui_utils)
reload(object_utils)

# define global variables
window_title = 'agBlendShapeConnector'
open_windows = {}

class Form(QtWidgets.QDialog):
    WIDGETS = {}

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setWindowTitle(window_title)

        # main layour
        layout = QtWidgets.QVBoxLayout()

        # controller object
        h_layout = QtWidgets.QHBoxLayout()
        self.WIDGETS['controllerLineEdit'] = QtWidgets.QLineEdit("Please enter the controller name.")
        self.WIDGETS['controllerLineEdit'].setReadOnly(True)
        self.WIDGETS['lineEditCtrlAddButton'] = QtWidgets.QPushButton(" <<< Control")
        h_layout.addWidget(self.WIDGETS['controllerLineEdit'], 0)
        h_layout.addWidget(self.WIDGETS['lineEditCtrlAddButton'], 1)

        layout.addLayout(h_layout) # 0
        layout.addWidget(QtWidgets.QLabel("--------------------------------------------------------------------------------")) # 2
        layout.insertSpacerItem(2, QtWidgets.QSpacerItem(5, 10)) # 2

        # attributes object
        h_layout2 = QtWidgets.QHBoxLayout()
        self.WIDGETS["blendShapeLabel"] = QtWidgets.QLabel("Please choose the available blend-shapes.")
        self.WIDGETS['blendShapeComboBox'] = QtWidgets.QComboBox()
        self.WIDGETS['blendShapeComboBox'].setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.add_menu_actions(self.WIDGETS['blendShapeComboBox'])
        self.populate_blendshapes()
        h_layout2.addWidget(self.WIDGETS["blendShapeLabel"], 0)
        h_layout2.addWidget(self.WIDGETS['blendShapeComboBox'], 1)

        # attributes object
        h_layout3 = QtWidgets.QHBoxLayout()
        self.WIDGETS["blendShaoeAttributesLabel"] = QtWidgets.QLabel("Please select available blend-shape attributes")
        self.WIDGETS['blendShaoeAttributesComboBox'] = QtWidgets.QComboBox()
        h_layout3.addWidget(self.WIDGETS["blendShaoeAttributesLabel"], 0)
        h_layout3.addWidget(self.WIDGETS['blendShaoeAttributesComboBox'], 1)

        layout.addLayout(h_layout2) # 3
        layout.addLayout(h_layout3) # 4
        layout.addWidget(QtWidgets.QLabel("Connect the blendShape with one of these attributes: "))  # 6
        layout.insertSpacerItem(5, QtWidgets.QSpacerItem(4, 10)) # 5

        self.WIDGETS['performConnectionButton'] = QtWidgets.QPushButton("Connect the attributes.")
        h_layout4 = QtWidgets.QHBoxLayout()
        self.WIbutton_group = QtWidgets.QButtonGroup()
        self.WIDGETS["attributesButtonGroup"] = QtWidgets.QButtonGroup()
        translateX = QtWidgets.QCheckBox("translateX")
        translateX.setChecked(True)
        translateY = QtWidgets.QCheckBox("translateY")
        translateZ = QtWidgets.QCheckBox("translateZ")

        rotateX = QtWidgets.QCheckBox("rotateX")
        rotateY = QtWidgets.QCheckBox("rotateY")
        rotateZ = QtWidgets.QCheckBox("rotateZ")
        self.WIDGETS["attributesButtonGroup"].addButton(translateX)
        self.WIDGETS["attributesButtonGroup"].addButton(translateY)
        self.WIDGETS["attributesButtonGroup"].addButton(translateZ)
        self.WIDGETS["attributesButtonGroup"].addButton(rotateX)
        self.WIDGETS["attributesButtonGroup"].addButton(rotateY)
        self.WIDGETS["attributesButtonGroup"].addButton(rotateZ)
        h_layout4.addWidget(translateX)
        h_layout4.addWidget(translateY)
        h_layout4.addWidget(translateZ)
        h_layout4.addWidget(rotateX)
        h_layout4.addWidget(rotateY)
        h_layout4.addWidget(rotateZ)
        layout.addLayout(h_layout4)
        layout.addWidget(self.WIDGETS['performConnectionButton'])

        self.setLayout(layout)
        self.connect_buttons()
        self.populate_shapes()

    def add_menu_actions(self, widget):
        """
        adds menu actions.
        """
        select = QtWidgets.QAction(self)
        select.setText("Select")
        select.triggered.connect(self.select_blendshape)
        widget.addAction(select)
        return True

    def select_blendshape(self):
        """
        selects the blendshape.
        """
        data = self.get_text_data()
        object_utils.select_object(data['blendShapeNode'])
        return True

    def populate_menu_bar(self):
        """
        populates the menu bar with options
        """
        file_menu = self.menuBar().addMenu('&BlendShape')
        file_menu.addAction("&Select BlendShape Node")
        return True

    def connect_buttons(self):
        """
        connects the abailable buttons
        """
        self.WIDGETS['lineEditCtrlAddButton'].clicked.connect(self.populate_controller)
        self.WIDGETS['performConnectionButton'].clicked.connect(self.connect_blendshape_attribute)

    def get_text_data(self):
        """
        get text data from the UI
        :return: <dict> return the line edit, comboBox text data as dictionary
        """
        text_data = {}
        text_data['controllerLineEdit'] = self.WIDGETS['controllerLineEdit'].text()
        item_idx = self.WIDGETS['blendShapeComboBox'].currentIndex()
        text_data['blendShapeNode'] = self.WIDGETS['blendShapeComboBox'].itemText(item_idx)
        item_idx = self.WIDGETS['blendShaoeAttributesComboBox'].currentIndex()
        text_data['blendShapeNodeAttributes'] = self.WIDGETS['blendShaoeAttributesComboBox'].itemText(item_idx)
        text_data['attributeName'] = self.WIDGETS['attributesButtonGroup'].checkedButton().text()
        return text_data

    def connect_blendshape_attribute(self):
        """
        make a dynamic fk chain through selection.
        :return: <bool> True for success.
        """
        data = self.get_text_data()
        controller_text = data['controllerLineEdit']
        driver_attrib_text = data['attributeName']
        blend_node_text = data['blendShapeNode']
        blend_shape_attribute = data['blendShapeNodeAttributes']
        print(controller_text, driver_attrib_text, ' >>> ', blend_node_text, blend_shape_attribute)
        animation_utils.connect_anim(controller_text, driver_attrib_text, blend_node_text, blend_shape_attribute)
        return True

    def populate_blendshapes(self):
        """
        populates the controller text edit box.
        :return:
        """
        blend_shape_nodes = blendshape_utils.get_scene_blendshapes(as_strings=True)
        self.WIDGETS['blendShapeComboBox'].addItems(blend_shape_nodes.values())

    def populate_shapes(self):
        """
        populates the controller text edit box.
        :return:
        """
        data = self.get_text_data()
        blend_node_text = data['blendShapeNode']
        shape_names = blendshape_utils.get_shapes(blend_node_text)
        self.WIDGETS['blendShaoeAttributesComboBox'].addItems(shape_names)

    def populate_controller(self):
        """
        populates the controller text edit box.
        :return:
        """
        selected_object = object_utils.get_selected_node(single=True)
        self.WIDGETS['controllerLineEdit'].setText(selected_object)
        self.setWindowTitle("{} :: {}".format(window_title, selected_object))

    def close(self):
        self.deleteLater()

def open_it():
    """
    opens the tool in whatever application we are using.
    :return: <bool> True for success.
    """
    global open_windows
    if window_title in open_windows:
        open_windows[window_title].close()
    open_windows[window_title] = Form(parent=ui_utils.get_maya_parent_window())
    open_windows[window_title].show()
    return open_windows


import tiktoken
# If tiktoken not installed. Install with pip install --upgrade tiktoken

# Source: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb

code = "cl100k_base"

encoding = tiktoken.get_encoding(code)

def num_tokens_from_string(string: str, encoding_name: str) -> int:
    """Returns the number of tokens in a text string."""
    encoding = tiktoken.get_encoding(encoding_name)
    num_tokens = len(encoding.encode(string))
    return num_tokens

def counter(texto: str, model: str):
    #here we define the model
    result = num_tokens_from_string(texto, code)
    return result


import sys
sys.setrecursionlimit(2000)
N, S = map(int, input().split())
L = []
for i in range(N):
    l = list(map(int, input().split()))
    L.append([l[0], l[1:]])
ans = 0
X = 1
index = 0

def recursion(lis, n, ind):
    global ans
    for i in range(lis[0]):
        seki = lis[1][i]
        if ind == N - 1:
            if n * seki == S:
                ans += 1
        else:
            recursion(L[ind + 1], n * seki, ind + 1)

recursion(L[index], X, index)
print(ans)