from matplotlib import pyplot as plt
from matplotlib.dates import date2num
import numpy as np
import argparse
import csv
from datetime import datetime
import math
import os

# USAGE:
#   python plot.py -folder 2014-04-01_2017-11-13
# AVAILABLE OPTIONS:
#   -folder (required)
# EXAMPLE:
#   python plot.py -folder 2014-04-01_2017-11-13
#   python plot.py -folder 2014-04-01_2017-12-19

cwd = os.getcwd()
parser = argparse.ArgumentParser(description='Analyze sales team data')
parser.add_argument('-folder', '--folder', help='Folder name', required=True)
args = vars(parser.parse_args())

# CONSTANTS
showPlt = False

def get_csv_data(filename):
    with open(cwd + '/' + args['folder'] + '/' + filename) as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        raw_data = np.array([tuple(row) for (i, row) in enumerate(readCSV)])
        heading = raw_data[0]
        data = raw_data[1:]
        data[data=='']='0'
        data_dict = [dict(zip(heading, row)) for row in data]
    return (heading, data_dict)

def get_col(colName, rows):
    return np.array([cols[colName] for cols in rows])

def convert_to_datetime(dates):
    return np.array([datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ').date() for date in dates]).astype('str')

def setupPlt():
    plt.figure(figsize=(20, 9), dpi=80)
    plt.xticks(fontsize=7)
    plt.yticks(fontsize=7)
    plt.xticks(rotation='vertical')

def savePlt(filename):
    if showPlt:
        plt.show()
    plt.savefig(cwd + '/' + args['folder'] + '/charts/' + filename + '.svg')

# annual-retention-monthly-trend.csv
#
filename = 'annual-retention-monthly-trend.csv'
(heading, data) = get_csv_data(filename)
retention_rate = get_col('retention_rate', data)
date = get_col('date', data)

setupPlt()
plt.bar(date, retention_rate)
plt.title(filename)
plt.ylabel('retention_rate')
plt.xlabel('monthly_cohort')
savePlt(filename)

# initial-purchase-cycle-by-monthly-cohort.csv
#
filename = 'initial-purchase-cycle-by-monthly-cohort.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_cycle = get_col('initial_purchase_cycle_in_days', data).astype(float)
# purchase_value = get_col('initial_purchase_value', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.bar(x + 0.00, purchase_cycle, color='#EE3224', width=0.25, label='purchase_cycle')
# plt.bar(x + 0.25, purchase_value, color='#FFC222', width=0.25, label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('initial_purchase_cycle_in_days')
plt.xlabel('purchase_date')
savePlt(filename)

# repeat-purchase-cycle-by-monthly-cohort.csv
#
filename = 'repeat-purchase-cycle-by-monthly-cohort.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_cycle = get_col('avg_purchase_cycle', data).astype(float)
# purchase_value = get_col('avg_purchase_size', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.bar(x + 0.00, purchase_cycle, color='#EE3224', width=0.25, label='purchase_cycle')
# plt.bar(x + 0.25, purchase_value, color='#FFC222', width=0.25, label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('initial_purchase_cycle_in_days')
plt.xlabel('purchase_date')
savePlt(filename)

# new-deals-weekly-cohort-trend.csv
#
filename = 'new-deals-weekly-cohort-trend.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_value = get_col('decimal_value', data).astype(float)
num_new_clients = get_col('contact_count', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.plot(x, purchase_value, color='#EE3224', label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('purchase_value')
plt.xlabel('purchase_date')
savePlt(filename)

# number of new orders
setupPlt()
plt.xticks(x, date)
plt.plot(x, num_new_clients, color='#EE3224', label='num_new_clients')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('num_new_clients')
plt.xlabel('purchase_date')
savePlt(filename)

# repeat-deals-weekly-cohort-trend.csv
#
filename = 'repeat-deals-weekly-cohort-trend.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_value = get_col('decimal_value', data).astype(float)
num_repeat_clients = get_col('contact_count', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.plot(x, purchase_value, color='#EE3224', label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('purchase_value')
plt.xlabel('purchase_date')
savePlt(filename)

# number of repeat orders
setupPlt()
plt.xticks(x, date)
plt.plot(x, num_repeat_clients, color='#EE3224', label='num_repeat_clients')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('num_repeat_clients')
plt.xlabel('purchase_date')
savePlt(filename)

# initial-purchase-cycle-by-deal-size-distribution.csv
#
filename = 'initial-purchase-cycle-by-deal-size-distribution.csv'
(heading, data) = get_csv_data(filename)
bucket_labels = get_col('bucket', data)
avg_initial_purchase_cycle_in_days = get_col('avg_initial_purchase_cycle_in_days', data).astype(float)
x = np.arange(len(bucket_labels))

setupPlt()
plt.xticks(x, bucket_labels)
plt.bar(x, avg_initial_purchase_cycle_in_days, color='#EE3224', label='avg_initial_purchase_cycle_in_days')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('avg_initial_purchase_cycle_in_days')
plt.xlabel('bucket')
savePlt(filename)

# avg-deal-size-by-contact-distribution.csv
#
filename = 'avg-deal-size-by-contact-distribution.csv'
(heading, data) = get_csv_data(filename)
bucket_labels = get_col('avg deal size bucket', data)
frequency = get_col('frequency', data).astype(int)
x = np.arange(len(bucket_labels))

setupPlt()
plt.xticks(x, bucket_labels)
plt.bar(x, frequency, color='#EE3224', label='frequency')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('frequency')
plt.xlabel('avg deal size bucket')
savePlt(filename)


import requests
from bs4 import BeautifulSoup
import json
import re
from urllib.parse import urlparse, quote, quote_plus
import io
import time
import os
import threading
import argparse

headers = {
    "sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "Windows",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Mobile Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-GB,en;q=0.9",
}

all_images = {}

threads_running = 0 # don't change this

def get_extension(url):
    path = urlparse(url).path.strip('/')
    path,filename = os.path.split(path)
    filename,ext = os.path.splitext(filename)
    if ext:
        return ext
    else:
        return '.jpg'

def threaded_download(savepath,url):
    global threads_running

    try:
        r = requests.get(url,headers=headers)
    except Exception as e:
        print("Request Error : "+url)
        threads_running-=1
        return

    if r.status_code==200:
        if 'image' in r.headers.get('content-type','').lower():
            with open(savepath,'wb',) as f:
                f.write(r.content)
            print('Downloaded '+os.path.basename(savepath))
        else:
            print("Not an Image : "+url)

    else:
        print(f"{r.status_code} Error : "+url+" ")

    threads_running-=1

def collect_images_from_google(query):
    chunk = 0
    step = 0
    prev_len = 0
    no_more = 0

    while True:
        r = requests.get(f'https://www.google.com/search?q={query}&tbm=isch&biw=1271&bih=697&async=_id:islrg_c,_fmt:json&asearch=ichunklite&ved=0ahUKEwjw_KTXkM_3AhVJxDgGHR-pDeoQtDIIPSgA&start={chunk}&ijn={step}',headers=headers)

        if r.status_code==200:
            json_text = r.content.decode('utf8').removeprefix(")]}'")
            json_data = json.loads(json_text)
            try:
                results = json_data['ichunklite']['results']
                for result in results:
                    original_image = result['viewer_metadata']['original_image']['url']
                    all_images[original_image] = result['image_docid']
            except:
                pass

            chunk +=100
            step +=1

        if no_more>5:
            break
        if prev_len==len(all_images):
            no_more+=1
        else:
            print(len(all_images))
            prev_len = len(all_images)
            no_more = 0

def download_images(query):
    global threads_running
    print("Downloading "+str(len(all_images))+" images")
    if not os.path.exists(os.path.join("Images",f"{query}")):
        os.makedirs(os.path.join("Images",f"{query}"))

    for image_link,doc_id in all_images.items():
        image_save_path = os.path.join("Images",f"{query}",doc_id+get_extension(image_link))
        if os.path.exists(image_save_path):
            if os.path.getsize(image_save_path)>1:
                continue
        threading.Thread(target=threaded_download,kwargs={'url':image_link,'savepath':image_save_path}).start()
        threads_running+=1
        while threads_running>9:
            time.sleep(1)

    while threads_running>0:
        time.sleep(1)

if __name__=="__main__":
    parser = argparse.ArgumentParser(description = "Download images from google")
    parser.add_argument("-k", "--keyword", help = "Example: Help argument", required = True, default = "")
    arguments = parser.parse_args()
    keyword= arguments.keyword

    collect_images_from_google(keyword)
    download_images(keyword)


import json
from dataclasses import dataclass
from typing import Callable, Dict, List, Protocol

import pandas as pd
import requests

from oagdedupe._typing import Annotation, Project, Task, TaskList
from oagdedupe.settings import Settings

class SettingsEnabler(Protocol):
    settings: Settings
    url: str
    headers: Dict[str, str]
    get_tasks: Callable

class APIProjects(SettingsEnabler):
    """
    Interface to get/post Projects from LabelStudio
    """

    def list_projects(self) -> List[Project]:
        """
        Returns
        ----------
        List[Project]
        """
        content = requests.get(f"{self.url}/api/projects", headers=self.headers)
        return [
            Project.parse_obj(p) for p in json.loads(content.content)["results"]
        ]

    def create_project(
        self, title: str = "new project", description: str = "description"
    ):
        """
        Parameters
        ----------
        title: str
        description: str

        Returns
        ----------
        Project
        """

        new_project = {
            "title": title,
            "description": description,
            "label_config": """
                <View>
                    <Header value="Table with {key: value} pairs"/>
                    <Table name="table" value="$item"/>
                    <Choices name="choice" toName="table">
                        <Choice value="Match"/>
                        <Choice value="Not a Match"/>
                        <Choice value="Uncertain"/>
                    </Choices>
                </View>
            """,
        }

        resp = requests.post(
            f"{self.url}/api/projects", headers=self.headers, data=new_project
        )

        return Project.parse_obj(json.loads(resp.content))

class APITasks(SettingsEnabler):
    """
    Interface to get/post Tasks from LabelStudio
    """

    def get_tasks(self, project_id: int) -> TaskList:
        """
        Parameters
        ----------
        project_id: int

        Returns
        ----------
        TaskList
        """
        tasklist = json.loads(
            requests.get(
                f"{self.url}/api/tasks",
                headers=self.headers,
                data={"project": project_id},
            ).content
        )
        tasklist["tasks"] = [Task.parse_obj(t) for t in tasklist["tasks"]]
        return TaskList.parse_obj(tasklist)

    def post_tasks(self, df: pd.DataFrame, project_id: int):
        """
        Parameters
        ----------
        df: pd.DataFrame
        project_id: int
        """
        for _, row in df.iterrows():
            resp = requests.post(
                f"{self.url}/api/tasks",
                headers=self.headers,
                data={
                    "project": project_id,
                    "data": json.dumps({"item": row.to_dict()}),
                },
            )
            json.loads(resp.content)

class APIAnnotations(SettingsEnabler):
    """
    Interface to get/post Annotations from LabelStudio
    """

    def _get_annotationlist_for_task(self, task_id: int) -> List[Annotation]:
        """
        Parameters
        ----------
        task_id: int

        Returns
        ----------
        List[Annotation]
        """
        annotationlist = json.loads(
            requests.get(
                f"{self.url}/api/tasks/{task_id}/annotations/",
                headers=self.headers,
            ).content
        )
        if annotationlist:
            annotationlist = [Annotation.parse_obj(a) for a in annotationlist]
        return annotationlist

    def _latest_annotation(
        self, annotationlist: List[Annotation]
    ) -> Annotation:
        """
        Parameters
        ----------
        annotationlist: List[Annotation]

        Returns
        ----------
        Annotation
        """
        return sorted(annotationlist, key=lambda d: d.created_at)[0].label

    def get_new_labels(self, project_id: int) -> pd.DataFrame:
        """
        Parameters
        ----------
        project_id: int

        Returns
        ----------
        pd.DataFrame
        """
        labels = {}
        for task in self.get_tasks(project_id=project_id).tasks:
            annotationlist = self._get_annotationlist_for_task(task_id=task.id)
            if annotationlist:
                labels[task.id] = {
                    **{"label": self._latest_annotation(annotationlist)},
                    **task.data["item"],
                }
        return pd.DataFrame(labels).T

class APIWebhooks(SettingsEnabler):
    """
    Interface to get/post Webhooks from LabelStudio
    """

    def get_webhooks(self):
        resp = requests.get(f"{self.url}/api/webhooks", headers=self.headers)
        return json.loads(resp.content)

    def post_webhook(self, project_id: int):
        """
        Parameters
        ----------
        project_id: int

        Returns
        ----------
        None
        """
        query = {
            "project": project_id,
            "url": f"http://host.docker.internal:{self.settings.fast_api.port}/payload",
            "send_payload": False,
            "is_active": True,
            "actions": ["ANNOTATION_CREATED", "ANNOTATION_UPDATED"],
        }
        requests.post(
            f"{self.url}/api/webhooks", headers=self.headers, data=query
        )

@dataclass
class LabelStudioAPI(APIProjects, APITasks, APIAnnotations, APIWebhooks):
    """
    Interface to get/post Projects, Tasks, Annotations,
    Webhooks from LabelStudio
    """

    settings: Settings

    def __post_init__(self):
        self.url: str = self.settings.label_studio.url
        self.headers: Dict[str, str] = {
            "Authorization": f"""Token {self.settings.label_studio.api_key}"""
        }


"""
Hashcode 2020 data structures and parsing helpers
"""

import logging
from typing import List, Set, TextIO, Dict

class Library:  # pylint: disable=too-few-public-methods
    """Library"""

    def __init__(self, id_: int,  # pylint: disable=too-many-arguments
                 n_books: int, signup_delay: int, books_per_day: int, books: Set[int]):
        self.id_ = id_
        self.n_books = n_books
        self.signup_delay = signup_delay  # days
        self.books_per_day = books_per_day  # books/day
        self.books = books  # books id set

class InputDataSet:  # pylint: disable=too-few-public-methods
    """Input Data"""

    def __init__(self, n_books,  # pylint: disable=too-many-arguments
                 n_libraries, n_days, book_scores, libraries):
        self.n_books = n_books
        self.n_libraries = n_libraries
        self.n_days = n_days
        self.book_scores: List[int] = book_scores  # book i score = book_scores[i]
        self.libraries: List[Library] = libraries  # List[Library]

    def __str__(self):
        return f"B: {self.n_books} L: {self.n_libraries} D: {self.n_days}"

class LibraryOrder:  # pylint: disable=too-few-public-methods
    """Library Order"""

    def __init__(self, id_: int, n_books: int, books: List[int]):
        self.id_ = id_
        self.n_books = n_books
        self.books = books  # book id list (ordered)

class OutputDataSet:  # pylint: disable=too-few-public-methods
    """Output Data"""

    def __init__(self, n_libraries, library_orders: List[LibraryOrder]):
        self.n_libraries = n_libraries
        self.library_orders = library_orders  # List[LibraryOrder]

    def __str__(self):
        return f"L: {self.n_libraries}"

def parse_input_file(input_file) -> InputDataSet:
    """
    Parse input file and return an initialized InputDataSet struct
    :param input_file: file for parsing
    :return: InputDataSet
    """
    # B, L, D
    (total_books, total_libraries, total_days) = map(int, input_file.readline().split(' '))
    book_scores = list(map(int, input_file.readline().split(' ')))
    libraries = []
    for id_ in range(total_libraries):
        (n_books, signup_delay, books_per_day) = map(int, input_file.readline().split(' '))
        books = set(map(int, input_file.readline().split(' ')))
        libraries.append(Library(id_, n_books, signup_delay, books_per_day, books))
    return InputDataSet(total_books, total_libraries, total_days, book_scores, libraries)

def parse_output_file(output_file, n_libraries, n_books) -> (bool, OutputDataSet):
    """
    Parse output file and return an initialized OutputDataSet struct
    :param output_file: file for parsing
    :param n_libraries: number of libraries from input file
    :param n_books: number of books from input file
    :return: OutputDataSet
    """
    n_orders = int(output_file.readline())  # L
    library_orders = []
    library_found: Dict[int, int] = {}  # library, line 1st definition
    books_found: Dict[int, int] = {}  # book, line 1st definition
    for line in range(n_orders):
        current_line = 2 * line + 2
        library_definition = output_file.readline()
        try:
            (id_, lib_n_books) = map(int, library_definition.split(' '))
            if id_ < 0 or id_ >= n_libraries:
                logging.warning(f"line {current_line}: library id {id_} is invalid should be >= 0 and < {n_libraries}")
                continue
            if lib_n_books == 0:
                logging.warning(f"line {current_line}: library books sent {lib_n_books} == 0")
            if id_ in library_found:
                logging.warning(f"line {current_line}: library {id_} previously defined line {library_found[id_]}")
                continue
            else:
                library_found[id_] = current_line
        except ValueError as _:
            logging.warning(
                f"line {current_line}: invalid content: '{library_definition}' should be <library_id> <books>")
            continue
        current_line = 2 * line + 3
        library_books = output_file.readline()
        try:
            books = list(map(int, library_books.split(' ')))
            for book in books:
                if book < 0 or book >= n_books:
                    logging.warning(f"line {current_line}: book id {book} is invalid should be >= 0 and < {n_books}")
                if book in books_found:
                    logging.debug(f"line {current_line}: book {book} previously defined line {books_found[book]}")
                else:
                    books_found[book] = current_line
        except ValueError as _:
            logging.warning(f"line {current_line}: invalid content: '{library_books}' should be <books>...")
            continue
        if len(books) != lib_n_books:
            logging.warning(f"line {current_line}: number of books ({len(books)}) does not match declaration at line {current_line - 1} ({lib_n_books})")
        library_orders.append(LibraryOrder(id_, lib_n_books, books))
    return OutputDataSet(n_orders, library_orders)

def write_output_file(output_data: OutputDataSet, output_file: TextIO):
    """
    Dump output data (solution) to output file
    :param output_data: solution
    :param output_file: file for dumping
    """
    output_file.write(f"{output_data.n_libraries}\n")
    for library_order in output_data.library_orders:
        output_file.write(f"{library_order.id_} {library_order.n_books}\n")
        output_file.write(" ".join(str(lib) for lib in library_order.books) + "\n")

def build_signup_schedule(input_data_set: InputDataSet, output_data_set: OutputDataSet):
    """
    Return signup schedule i.e (day signup complete, library id) list

    Note: returned list in arbitrary order
    :param input_data_set:
    :param output_data_set:
    :return: (day signup complete, library id) list
    """
    day = 0
    signup_schedule = []
    for library_order in output_data_set.library_orders:
        library = input_data_set.libraries[library_order.id_]
        day += library.signup_delay
        signup_schedule.append((day, library.id_))
    return signup_schedule


#!/bin/bash
import os.path
from pathlib import Path
from shutil import copyfile, move

with open("paths.temp") as f:
    ls = f.readlines()
ls = [Path(i.rstrip()) for i in ls]

for i in ls:
    if (i/"CONTCAR").is_file():
        if os.path.getsize(i/"CONTCAR") > 0:
            try:
                print(f"Move CONTCAR to POSCAR for {i}")
                move(i/"POSCAR", i/"OLD_POSCAR")
                copyfile(i/"CONTCAR", i/"POSCAR")
            except:
                print(f"Error for: {i}")

    if not (i/"POSCAR").is_file() and not (i/"CONTCAR").is_file() and (i / "OLD_POSCAR").is_file() and os.path.getsize(i/"OLD_POSCAR")>0:
        move(i/"OLD_POSCAR", i/"POSCAR")
        print(f"Restore OLD_POSCAR to POSCAR for {i}")

important script and software/script/cont2pos.py


# -*- coding: utf-8 -*
"""
Created on Fri Sep 24 12:30:15 2021
@author: Aashish Rana

kpi =  only returns and points

strategy = breakout of prvious day high(pdh) and previous day low(pdl)

entry logic  = long if current candle high is greater than pdh
               short if current candle low is lower than pdl

stop loss(fixed) = if long, sl will be pdl
                   if short, sl will be pdh
                   if sl didn't trigger, exit at 15:26'
"""

# import necessary liabraries
import pandas as pd
import numpy as np
from openpyxl import load_workbook
import matplotlib.pyplot as plt

# read the trade log file
df = pd.read_excel('E:\\breakout_pdh_pdl\\breakout_pdh_pdl_trade_log.xlsx',sheet_name='main',parse_dates =['Entry_date'],index_col = 'Entry_date')

# group the trade log points annually
Annual_points = df.groupby(pd.Grouper(freq = 'Y'))['points','returns'].sum()

#saving the annual points and returns to existing file which is trade_log
path = 'E:\\breakout_pdh_pdl\\breakout_pdh_pdl_trade_log.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path,engine = 'openpyxl')
writer.book = book
Annual_points.to_excel(writer,sheet_name = 'points and returns')
writer.save()
writer.close()

Annual_points.to_excel('breakout_pdh_pdl_trade_log.xlsx',sheet_name = 'points and returns')

# group the trade log points annually
Annual_points = df.groupby(pd.Grouper(freq = 'Y'))['points'].sum()

# plotting the charts of annual points
Annual_points.plot(kind = 'bar',xlabel = 'year',ylabel = 'points',figsize = (9,3),title = 'NIFTY FUTURES')


import struct
from .utils import function_ranges2, op_uses_values, op_sets_result, simplify_op, op_is_comp, bin_ops, un_ops

class ASMInstruction:

    def __init__(self, op, arg1=None, arg2=None, comment=None, indent=True):
        self.op = op
        self.arg1 = arg1
        self.arg2 = arg2
        self.comment = comment
        self.indent = indent

    def __str__(self):
        if self.op is None:
            return '\t'.ljust(25) + ('' if self.comment is None else '\t# ' + self.comment)
        indent = '\t' if self.indent else ''
        instr = indent + self.op.ljust(5) + '\t' + ', '.join([el for el in [self.arg1, self.arg2] if el is not None])
        instr = instr.ljust(25)
        if self.comment is not None:
            instr += '\t# ' + self.comment
        return instr

    def __repr__(self):
        return self.__str__()

    def __eq__(self, other):
        return (self.op, self.arg1, self.arg2) == (other.op, other.arg1, other.arg2)

op_to_asm = {
    '+': 'add',
    'f+': 'faddp',
    '-': 'sub',
    'f-': 'fsubp',
    'fu-': 'fsubp',
    '*': 'imull',
    'f*': 'fmulp',
    '/': 'idivl',
    'f/': 'fdivp',
    '%': 'idivl',
    '==': 'sete',
    '!=': 'setne',
    '<=': 'setle',
    '>=': 'setge',
    'f>=': 'setnb',
    '<': 'setl',
    'f<': 'setnae',
    '>': 'setg',
}

def is_var_or_temp(arg):
    return type(arg) is str

def gen_stack_mapping(code, params):
    currmap = {}
    for tac in code:
        op, _, _, _ = tac
        op = simplify_op(op)
        if op == 'arr-def':
            op, _, name, _ = tac
            currmap[name] = -(len(currmap) + 1) * 4
        if op not in op_uses_values and op not in op_sets_result:
            continue
        for argpos in op_uses_values[op] + ([3]if op in op_sets_result else[]):
            arg = tac[argpos]
            if arg in currmap:
                continue
            if not is_var_or_temp(arg):
                if type(arg) is float:
                    currmap[arg] = -(len(currmap) + 1) * 4
                continue
            if arg in params:
                continue
            currmap[arg] = -(len(currmap) + 1) * 4
    for loc, param in enumerate(params):
        currmap[param] = (loc + 2) * 4
    return currmap

def calc_types(code):
    types = []
    tmptypes = {}

    def typeofarg(arg):
        if type(arg) is str:
            return tmptypes[arg]
        return 'int' if type(arg) is int else 'float'
    for line, (op, arg1, arg2, res) in enumerate(code):
        newtype = None
        if op == 'assign':
            if arg2 is None:
                newtype = typeofarg(arg1)
            else:
                newtype = arg2
            tmptypes[res] = newtype
        elif op == 'pop':
            newtype = arg2
            tmptypes[res] = newtype
        elif op == 'arr-def':
            newtype = res
            tmptypes[arg2] = newtype
        elif op == 'arr-acc':
            newtype = typeofarg(arg2)
            tmptypes[res] = newtype
        elif op in un_ops:
            newtype = typeofarg(arg1)
            tmptypes[res] = newtype
        elif op in bin_ops:
            arg1t, arg2t = typeofarg(arg1), typeofarg(arg2)
            if arg1t != arg2t:
                print(arg1t, arg2t, arg1, arg2)
                # TODO type coercion?
                raise Exception
            newtype = arg1t
            tmptypes[res] = newtype
        types.append(newtype)
    return types

def float_to_asm(f):
    return '$%s' % hex(struct.unpack('<I', struct.pack('<f', f))[0])

def fun_to_asm(code, assembly):

    def arg_to_asm(arg):
        if is_var_or_temp(arg) or type(arg) is float:
            return '%d(%%ebp)' % register_to_stack[arg]
        elif type(arg) is int:
            return '$%d' % arg
        else:
            raise NotImplementedError

    def add(op, arg1=None, arg2=None, comment=None, indent=True):
        assembly.append(ASMInstruction(op, arg1, arg2, comment, indent))

    def to_assembly(op, arg1, arg2, res, totype=None):
        if op == 'jump':
            add('jmp', res)
        elif op == 'jumpfalse':
            add('cmp', '$0', arg_to_asm(arg1), comment='if(' + str(arg1) + '==0) goto ' + res)
            add('je', res)
        elif op == 'label':
            add(res + ':', indent=False)
        elif op == 'function':
            raise NotImplementedError
        elif op == 'call':
            raise NotImplementedError
        elif op == 'end-fun':
            add(None)
        elif op == 'return':
            add('mov', '%ebp', '%esp')
            add('pop', '%ebp')
            add('ret')
        elif op == 'push':
            raise NotImplementedError
        elif op == 'pop':
            raise NotImplementedError
        elif op == 'assign':
            comment = res + ' := ' + str(arg1)
            need_accum = (is_var_or_temp(arg1) and is_var_or_temp(res)) or type(arg1) is float
            arg1, res = [arg_to_asm(el) for el in [arg1, res]]
            if need_accum:
                add('mov', arg1, '%eax', comment=comment)
                add('movl', '%eax', res)
            else:
                add('movl', arg1, res, comment=comment)
        elif op in ['*', '/', '%']:
            comment = res + ' = ' + str(arg1) + ' ' + op + ' ' + str(arg2)
            if totype == 'int':
                add('mov', arg_to_asm(arg1), '%eax', comment=comment)
                if op in ['/', '%']:
                    add('cdq')
                if is_var_or_temp(arg2):
                    add(op_to_asm[op], arg_to_asm(arg2))
                else:
                    add('mov', arg_to_asm(arg2), '%ecx')
                    add(op_to_asm[op], '%ecx')
                if op == '%':
                    add('mov', '%edx', arg_to_asm(res))
                else:
                    add('mov', '%eax', arg_to_asm(res))
            elif totype == 'float':
                op = 'f' + op
                add('flds', arg_to_asm(arg2), comment=comment)
                add('flds', arg_to_asm(arg1))
                add(op_to_asm[op], '%st', '%st(1)')
                add('fstps', arg_to_asm(res))
            else:
                raise NotImplementedError
        elif op in bin_ops:
            comment = res + ' = ' + str(arg1) + ' ' + op + ' ' + str(arg2)
            if op in op_is_comp:
                if totype == 'int':
                    add('mov', arg_to_asm(arg1), '%ebx', comment=comment)
                    add('mov', arg_to_asm(arg2), '%eax')
                    add('cmp', '%eax', '%ebx')
                elif totype == 'float':
                    op = 'f' + op
                    add('flds', arg_to_asm(arg2), comment=comment)
                    add('flds', arg_to_asm(arg1))
                    add('fcomip')
                    add('fstp', '%st(0)')
                else:
                    raise NotImplementedError
                add('movl', '$0', arg_to_asm(res))
                add(op_to_asm[op], arg_to_asm(res))
            else:
                if totype == 'int':
                    add('mov', arg_to_asm(arg1), '%eax', comment=comment)
                    add(op_to_asm[op], arg_to_asm(arg2), '%eax')
                    add('mov', '%eax', arg_to_asm(res))
                elif totype == 'float':
                    op = 'f' + op
                    add('flds', arg_to_asm(arg2), comment=comment)
                    add('flds', arg_to_asm(arg1))
                    add(op_to_asm[op], '%st', '%st(1)')
                    add('fstps', arg_to_asm(res))
                else:
                    raise NotImplementedError
        elif op == 'u-':
            comment = res + ' = ' + op[1:] + str(arg1)
            if totype == 'int':
                add('mov', '$0', '%eax', comment=comment)
                add('sub', arg_to_asm(arg1), '%eax')
                add('mov', '%eax', arg_to_asm(res))
            elif totype == 'float':
                op = 'f' + op
                add('flds', arg_to_asm(arg1), comment=comment)
                add('fldz')
                add(op_to_asm[op], '%st', '%st(1)')
                add('fstps', arg_to_asm(res))
            else:
                raise NotImplementedError
        elif op == 'u!':
            comment = res + ' = ' + op[1:] + str(arg1)
            add('mov', arg_to_asm(arg1), '%eax', comment=comment)
            add('movl', '$0', arg_to_asm(res))
            add('cmp', '$0', '%eax')
            add('sete', arg_to_asm(res))
        elif op == 'arr-def':
            name, size = str(arg2), arg1
            comment = 'new int ' + name + '[' + str(size) + ']'
            add('movl', arg_to_asm(size), '%ebx', comment=comment)
            add('leal', '(,%ebx, 4)', '%ebx')
            add('sub', '%ebx', '%esp')
            add('movl', '%esp', arg_to_asm(name))
        # TODO use esi register?, remove %
        elif op == 'arr-acc':
            name, index = arg2, arg1
            comment = res + ' = ' + str(name) + '[' + str(index) + ']'
            add('movl', arg_to_asm(index), '%ebx', comment=comment)
            add('movl', arg_to_asm(name), '%ecx')
            add('movl', '-4(%s,%s,4)' % ('%ecx', '%ebx'), '%eax')
            add('movl', '%eax', arg_to_asm(res))
        elif op == 'arr-ass':
            name, index, result = res, arg1, arg2
            comment = name + '[' + str(index) + ']' + ' = ' + str(result)
            add('movl', arg_to_asm(result), '%eax', comment=comment)
            add('movl', arg_to_asm(index), '%ebx')
            add('movl', arg_to_asm(name), '%ecx')
            add('movl', '%eax', '-4(%s,%s,4)' % ('%ecx', '%ebx'))
        else:
            raise NotImplementedError

    '''
    Almost all TAC's can be directly mapped to a set of ASM instructions.
    Special care has to be taken in 2 cases:
        * 'pop'
            could be a passed parameter in the beginning of the function
            or a return value of a function call
        * 'push'
            could be a passed argument before a function call
            or a return value at the end of a function

    To mitigate this problem we use a state-machine that operates on
    the TAC operation (with a lookahead to the next operation):

        'fun-def' state (we start in this state):
            'function','pop'
                don't get mapped to ASM instructions. (but we save the names)
            'any other operation'
                means we are at the end of the function definition. We create
                a new stackframe by allocating the appropriate space on the
                stack and saving the old base pointer.
                'n' parameters are now on the stack in the range
                    8(%ebp) to ((n+2)*4)(%ebp)
                and 'n' local variables are now in the range
                    -4(%ebp) to ((n+1)*-4)(%ebp)
                the next and final state is 'fun-body'

        'fun-body' state:
            'push'
                could be a argument push, or return value. Thus we look to the
                next operation. If it is a return operation we know that we
                have to move the result into the '%eax' register. Otherwise we
                just push the argument (and save its name).
            'call'
                we generate a 'call' ASM instruction and reset the stack pointer
                we also generate a nice comment with the arguments we pushed
                beforehand. By looking at the next operation we also know if
                the function returns a value.
            'pop'
                this has to be a return value from a function call (the other
                type of 'pop' only happens in function definitions). Thus we
                just assign the value of '%eax' to the register.
            'any other operation'
                uses the simple mapping defined in the 'to_assembly' function
    '''
    register_to_stack = None
    state, fname, params = 'fun-def', None, []
    line = 0
    args = []
    types = calc_types(code)
    while line < len(code):
        totype = types[line]
        tac = code[line]
        op, arg1, _, res = tac

        if state == 'fun-def':
            if op == 'function':
                fname = res
            elif op == 'pop':
                params.append(res)
            else:
                register_to_stack = gen_stack_mapping(code, params)
                registersinframe = len(register_to_stack) - len(params)
                # label
                add(fname + ':\t', indent=False, comment='%d params already on stack' % len(params))
                for var in params:
                    add(None, comment=var.rjust(5) + ' := ' + arg_to_asm(var))
                # stack frame
                add('push', '%ebp')
                add('mov', '%esp', '%ebp')
                add('sub', '$' + str(registersinframe * 4), '%esp',
                    comment='make space on stack for %d local registers' % registersinframe)
                for var in register_to_stack:
                    if var not in params:
                        add(None, comment=str(var).rjust(5) + ' := ' + arg_to_asm(var))
                for val, var in register_to_stack.items():
                    if type(val) is float:
                        add('movl', float_to_asm(val), '%d(%%ebp)' % var, comment=('const float: ' + str(val)))
                add(None)
                state = 'fun-body'
                continue

        elif state == 'fun-body':
            if op == 'push':
                nextop, _, _, _ = code[line + 1]
                if nextop == 'return':
                    add('mov', arg_to_asm(arg1), '%eax', comment='return ' + str(arg1))
                    args = []
                else:
                    args.append(arg1)
                    add('push', arg_to_asm(arg1))
            elif op == 'call':
                nextop, _, _, nextret = code[line + 1]
                retvalue = (nextret + ' := ') if nextop == 'pop' else ''
                comment = retvalue + res + '(' + ','.join([str(el) for el in reversed(args)]) + ')'
                add('call', res, comment=comment)
                if len(args) > 0:
                    add('add', '$' + str(len(args) * 4), '%esp')
                args = []
            elif op == 'pop':
                add('mov', '%eax', arg_to_asm(res))
            else:
                to_assembly(*tac, totype=totype)

        line += 1

def codetoassembly(code, verbose=0, assemblyfile=None):
    assembly = ['.globl main', '.text']
    fun_ranges = function_ranges2(code)
    for _, start, end in fun_ranges:
        fun_to_asm(code[start:end], assembly)

    if verbose > 0:  # pragma: no cover
        print('\n' + ' GNU Assembly '.center(40, '#'))
        print('\n'.join(map(str, assembly)))

    if assemblyfile is not None:  # pragma: no cover
        if verbose > -1:
            print('Writing assembly to: \'%s\'' % assemblyfile)
        with open(assemblyfile, 'w') as f:
            f.write('\n'.join(map(str, assembly)))
            f.write('\n')

    return assembly

if __name__ == '__main__':
    import argparse
    from .parser import parsefile
    from .three import asttothree
    from .bb import threetobbs
    from .lvn import lvn
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", help="The *.mc file to convert to GNU Assembly")
    parser.add_argument('--lvn', '-l', action='count', default=False)
    parser.add_argument('--verbose', '-v', action='count', default=0)
    args = parser.parse_args()
    bbs = threetobbs(
        asttothree(
            parsefile(
                args.filename,
                verbose=args.verbose - 2),
            verbose=args.verbose - 1),
        verbose=0 if args.lvn else args.verbose)
    if args.lvn:
        bbs = lvn(bbs, verbose=args.verbose)
    code = [tac for bb in bbs for tac in bb]
    codetoassembly(code, args.verbose + 1, args.filename + '.s')


import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

def interactive_visulization(points, labels, qcharges=None, rmsd=None, molecules=None, bg=None, bb=None, width=400, height=1000):
    """
    points: [num_frames, 2]
    molecules: [num_frames, 3, num_atoms]
    """

    # Preparing figure object
    if molecules is not None:
        fig = make_subplots(
            rows=1,
            cols=2,
            specs=[[{"type": "xy"}, {"type": "scene"}]],
        )
    else:
        fig = make_subplots(
            rows=1,
            cols=1,
            specs=[[{"type": "xy"}]],
        )
    if rmsd is not None and qcharges is not None:
        hovertext = list(map(str, zip(np.arange(points.shape[0]), qcharges, rmsd)))
    elif qcharges is not None:
        hovertext = list(map(str, zip(np.arange(points.shape[0]), qcharges)))
    elif rmsd is not None:
        hovertext = list(map(str, zip(np.arange(points.shape[0]), rmsd)))
    else:
        hovertext = np.arange(points.shape[0])

    points_plot = go.Scatter(
        x=points[:, 0],
        y=points[:, 1],
        hovertext=hovertext,
        #hovertext=np.arange( )
            mode="markers",
            name="latent_points",
            marker=dict(
                color=labels,
            )

    )

    fig.add_trace(points_plot, row=1, col=1)

    if molecules is not None:
        molecule_3d_plot = go.Scatter3d(
            x = molecules[0, 0, :],
            y = molecules[0, 1, :],
            z = molecules[0, 2, :],
            mode="markers",
            marker=dict(
                size=3,
            ),
            name="molecule",
        )

        fig.add_trace(molecule_3d_plot, row=1, col=2)

    layout = fig.layout
        # layout["xaxis"]["range"] = [-2.2, 2.2]
        # layout["yaxis"]["range"] = [-2.2, 2.2]
        # layout["zaxis"]["range"] = [-2.2, 2.2]

    if bg is not None:
        from PIL import Image
        from matplotlib import cm

        energy_array = np.load(bg)
        # energy_img = Image.fromarray(energy_array)
        energy_array = (energy_array - energy_array.min()) / \
                       (energy_array.max() - energy_array.min())

        energy_img = Image.fromarray(np.uint8(cm.viridis(energy_array)*255))
        # from matplotlib import pyplot as plt
        # plt.imshow(np.array(energy_img))
        # plt.show()

        x1,y1,x2,y2 = np.load(bb)
        fig.add_layout_image(
        dict(
            source=energy_img,
            #xref="x",
            #yref="y",
            x=x1,
            y=y2,
            sizex=x2-x1,
            sizey=y2-y1,
            sizing="stretch",
            opacity=0.5
            )
        #layer="below")
        )
    # fig.update_layout(template="plotly_white")
    fig.update_layout(
        width = width,
        height = height,
     )
    # Preparing interactive widget

    if molecules is not None:
        widget = go.FigureWidget([points_plot, molecule_3d_plot], layout=layout)
        molecule_3d_plot = widget.data[1]
    else:
        widget = go.FigureWidget([points_plot], layout=layout)

    points_plot = widget.data[0]

    # widget.layout.hovermode = 'closest'

    # Defining callback with 3 parameters: trace = graphical object with some changes, points and selector
    def callback(trace, points, selector):
        # A list with the size of the points, all of them have the same size
        # s = [5] * N # list(points_plot.marker.size)

        # Iteration over indexes, we want only one of the points
        for i in points.point_inds:
            # Imitating selection of the particular point
            # s[i] = 10

            # Go into batch_update for buffering

            with widget.batch_update():
                # coordinates of i frame
                molecule_3d_plot.x = molecules[i, 0, :]
                molecule_3d_plot.y = molecules[i, 1, :]
                molecule_3d_plot.z = molecules[i, 2, :]

                # changing sizes of all the points, it is necessary to make another list to make it work
                # points_plot.marker.size = s

            return

    # registring callback, possible modes (on_cxlick), (on_selection), etc
    if molecules is not None:
        points_plot.on_hover(callback)

    # Widget visualization
    return widget


from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

PATH_LIST = {
    "Windows": 'phantomjs-windows/bin/phantomjs.exe',
    "Linux": 'phantomjs-linux/bin/phantomjs',
    "Darwin": 'phantomjs-macos/bin/phantomjs'
}

def get_driver(os_system):
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap["phantomjs.page.settings.userAgent"] = (
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
        "(KHTML, like Gecko) Chrome/15.0.87"
    )
    path = PATH_LIST[os_system]
    driver = webdriver.PhantomJS(path, desired_capabilities=dcap)
    # driver = webdriver.Chrome('chromedriver.exe')
    driver.set_window_size(1920, 950)
    driver.implicitly_wait(10)
    return driver


import cv2
from glob import glob
from os import path

def split_image(file_name):
    print(file_name)
    img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED)
    image_width, image_height, image_channels = img.shape
    width_pieces = 2
    height_pieces = 2
    piece_width = image_width / width_pieces
    piece_height = image_height / height_pieces
    index = 0
    w_index = 0
    for w_itr in range(0, image_width, piece_width):
        h_index = 0
        for h_itr in range(0, image_height, piece_height):
            x1 = w_index * piece_width
            x2 = (w_index + 1) * piece_width
            y1 = h_index * piece_height
            y2 = (h_index + 1) * piece_height
            piece_i = img[x1:x2, y1:y2]
            base_file_name = path.basename(file_name)
            suffix_i = "_" + str(index) + ".jpg"
            base_file_name = base_file_name.replace(".JPG", suffix_i)
            out_file_name = path.dirname(file_name) + "/split/" + base_file_name
            print(out_file_name)
            cv2.imwrite(out_file_name, piece_i)
            h_index += 1
            index += 1
        w_index += 1

for file_name in glob('Images/pipes.jpg'):
    split_image(file_name)


import socket
import configparser
from Message import Message

class IRC(object):
    def __init__(self, testing=False):
        configHeading = "Bot Settings"
        if testing:
            configHeading = "Test Settings"
        config = configparser.RawConfigParser()
        config.read("botInfo.cfg")
        self.server = config.get(configHeading, "server").encode()
        self.channel = config.get(configHeading, "channel").encode()
        self.botNick = config.get(configHeading, "botNick").encode()
        self.updatingNames = False
        self.names = []

        self.connect()

    def connect(self):
        # Wait for the server to send it's initial messages to signal
        # to us that it is time to send user info

        self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ircsock.connect((self.server, 6667))

        while True:
            ircmsg = self.ircsock.recv(2048)
            ircmsg = ircmsg.decode()
            ircmsg = ircmsg.strip('\n\r')
            print(ircmsg)
            if ircmsg.find('Found your hostname') != -1:
                print("Found")
                break

        # Send nick info to IRC server
        self.send(b"NICK "+ self.botNick + b"\r\n")
        self.send(b"USER "+ self.botNick + b" * 8" + b" :" + self.botNick +b"\r\n")

        # Wait for the server to finish sending MOTD so that we can send
        # the commands to join our channel
        while True:
            ircmsg = self.ircsock.recv(2048)
            ircmsg = ircmsg.decode('ascii')
            ircmsg = ircmsg.strip('\n\r')
            print(ircmsg)
            if ircmsg.find("PING :") != -1:
                self.ping(self.getPingID(ircmsg))
            if ircmsg.find('End of /MOTD command.') != -1:
                print("End of MOTD")
                break

        self.joinChannel(self.channel)

        # Once we join the channel we need to wait to get op. This loop will
        # wait until that has been achieved
        while True:
          ircmsg = self.ircsock.recv(2048)
          ircmsg = ircmsg.decode()
          ircmsg = ircmsg.strip('\n\r')

          # Check to see if we have a message giving us op yet
          if ircmsg.split(" ")[1] == "MODE" and ircmsg.split(" ")[3] == "+o":
              break

    def say(self, contents):
        self.send(b"PRIVMSG " + self.channel + b" :" + contents.encode() + b"\r\n")

    def query(self, userName, contents):
        self.send(b"PRIVMSG " + userName.encode() + b" :" +
                contents.encode() + b"\r\n")

    def notice(self, userName, contents):
        self.send(b"NOTICE " + userName.encode() + b" :" +
                contents.encode() + b"\r\n")

    def send(self, contents):
        self.ircsock.send(contents)

    def ping(self, pingID):
        self.send(b"PONG :" + pingID.encode() +  b"\r\n")

    def joinChannel(self, channelName):
        self.send(b"JOIN "+ channelName + b"\r\n")

    def getPingID(self, msg):
        msgList = msg.split(':')
        return msgList[1]

    def receiveMessage(self):
        ircmsg = self.ircsock.recv(2048)
        ircmsg = ircmsg.decode()
        ircmsg = ircmsg.strip("\n\r")

        print(ircmsg)

        if ircmsg.find("PING :") != -1 or ircmsg.find("PONG") != -1:
            self.ping(self.getPingID(ircmsg))

        messageObject = Message(ircmsg)

        if messageObject.type == "353":
            self.names = messageObject.names
            self.updatingNames = False

        if len(ircmsg) == 0:
            time.sleep(60)
            self.connect()
            return "RECONNECT"

        return messageObject

    def getNames(self):
        self.send(b"NAMES " + self.channel + b"\r\n")
        self.updatingNames = True

    def setTopic(self, topicMessage):
        self.send(b"TOPIC " + self.channel + b" :" + topicMessage.encode() + b"\r\n")

if __name__ == "__main__":
    IRCObject = IRC(testing=True)
    IRCObject.say("HELLO")
    while True:
        message = IRCObject.receiveMessage()
        if message.contents:
            textFile = open('log.txt', 'a')
            print(message.raw)
            textFile.write(message.raw)
            textFile.close()


import fire
import requests

from multiprocessing import Pool

URL_TEMPLATE = "http://info.dla.go.th/public/surveyInfo.do?cmd=surveyForm&orgInfoId={key}"

YEARS = [2015, 2016, 2017, 2018, 2019]
YINDX = dict(zip(YEARS, range(3, 3 + len(YEARS))))
ORG_IDS_FILE = "./data/local-governor-ids.txt"

BASE_DIR = "./outputs/budgets"

def _build_key(year, org_id):
    return "%s%s" % (year, "%04d" % int(org_id))

def scrape_years(years, trial=None, dry_run=False, pool_size=10):
    for y in years:
        print("Working on Y=%s" % y)
        scrape_year(y, trial=trial, dry_run=dry_run, pool_size=pool_size)

def scrape_year(year, trial=None, dry_run=False, pool_size=5):
    with open(ORG_IDS_FILE, "r") as f:
        org_ids = f.readlines()[:trial]

    year_org_ids = map(lambda x: (year, x.strip()), org_ids)

    with Pool(pool_size) as p:
        p.map(do_job, year_org_ids)

    return p.join()

def do_job(args):
    year, org_id = args
    text = scrape_year_org(year, org_id)

    with open("%s/%s/%s.html" % (BASE_DIR, year, org_id), "w") as f:
        f.write(text)

def scrape_year_org(year, org_id):
    yidx = YINDX[year]
    url = URL_TEMPLATE.format(key=_build_key(yidx, org_id))

    print("Scraping %s" % url)

    r = requests.get(url)

    return r.text

if __name__ == "__main__":
    fire.Fire({
        "years": scrape_years,
        "year": scrape_year,
        "year-org": scrape_year_org
    })


import fastapi.routing
from sqlalchemy.orm import Session
from sqlalchemy.sql import select

from impressive_strawberry.database import tables
from impressive_strawberry.web import crud
from impressive_strawberry.web import deps
from impressive_strawberry.web import models
from impressive_strawberry.web import responses
from impressive_strawberry.web.errors import DuplicatingUnrepeatableUnlock, NotUnlockable
from impressive_strawberry.webhooks import notify_unlock

app_router = fastapi.routing.APIRouter(
    prefix="/api/unlock/v1",
    tags=[
        "Unlock v1",
    ],
)

group_router = fastapi.routing.APIRouter(
    prefix="/api/unlock-group/v1",
    tags=[
        "Unlock v1",
    ]
)

token_router = fastapi.routing.APIRouter(
    prefix="/api/unlock-direct/v1",
    tags=[
        "Unlock v1",
    ]
)

@group_router.get(
    "/",
    summary="List unlocked achievements obtained by a certain user in a certain group, both belonging to the same application you're authenticating as.",
    response_model=list[models.full.UnlockFull],
)
async def group_unlock_list(
        *,
        session: Session = fastapi.Depends(deps.dep_dbsession),
        group: tables.Group = fastapi.Depends(deps.dep_group_thisapp),
        user: tables.User = fastapi.Depends(deps.dep_user_thisapp),
):
    data = session.execute(
        select(tables.Unlock).where(tables.Unlock.user == user).join(tables.Achievement).where(tables.Achievement.group == group)
    ).all()
    return list(map(lambda d: d[0], data))

@app_router.post(
    "/",
    summary="Adds a certain achievement of a group to a user, both belonging to the same application you're authenticating as.",
    response_model=models.full.UnlockFull,
    status_code=201,
)
async def unlock_create(
        *,
        session: Session = fastapi.Depends(deps.dep_dbsession),
        achievement: tables.Achievement = fastapi.Depends(deps.dep_achievement_thisapp),
        user: tables.User = fastapi.Depends(deps.dep_user_thisapp),
):
    if not achievement.unlockable:
        raise NotUnlockable()
    if achievement in [u.achievement for u in user.unlocks] and not achievement.repeatable:
        raise DuplicatingUnrepeatableUnlock()
    unlock = crud.quick_create(session, tables.Unlock(achievement_id=achievement.id, user_id=user.id))
    await notify_unlock(group=achievement.group, unlock=unlock)
    return unlock

@app_router.delete(
    "/{unlock}",
    summary="Removes an unlock of an user belonging to the application you're authenticating as.",
    status_code=204,
)
async def unlock_delete(
        *,
        unlock: tables.Unlock = fastapi.Depends(deps.dep_unlock_thisapp),
        session: Session = fastapi.Depends(deps.dep_dbsession),
):
    session.delete(unlock)
    session.commit()
    return responses.raw.NO_CONTENT

@token_router.post(
    "/",
    summary="Adds a certain achievement of a group to a user, both belonging to the same application, using a token.",
    status_code=201,
)
async def direct_unlock_create(
        *,
        session: Session = fastapi.Depends(deps.dep_dbsession),
        achievement: tables.Achievement = fastapi.Depends(deps.dep_achievement_token),
        user: tables.User = fastapi.Depends(deps.dep_user_token)
):
    if not achievement.unlockable:
        raise NotUnlockable()
    if achievement in [u.achievement for u in user.unlocks] and not achievement.repeatable:
        raise DuplicatingUnrepeatableUnlock()
    unlock = crud.quick_create(session, tables.Unlock(achievement_id=achievement.id, user_id=user.id))
    await notify_unlock(group=achievement.group, unlock=unlock)
    return unlock


#Строковые занчения
'{} {}'.format('one', 'two') #one two

#Числовые значения
'{} {}'.format(1, 2) #1 2

#порядок значений можно указывать
'{1} {0}'.format('one', 'two') #two one

#Можно так же подставлять значения классов
class Data(object):

    def __str__(self):
        return 'str'

    def __repr__(self):
        return 'repr'

'{0!s} {0!r}'.format(Data()) #str repr

#Отступы и выравнивания
#по правому краю
'{:>10}'.format('test') #      test

#по левому краю
'{:_<10}'.format('test') #test______

#по центру
'{:^10}'.format('test') #   test

#срезы
'{:.5}'.format('xylophone') #xylop

#срезы и отступы
'{:10.5}'.format('xylophone') #xylop

#числа
'{:d}'.format(42) #42
'{:f}'.format(3.141592653589793) #3.141593

#числа и отступы
'{:4d}'.format(42) #  42
'{:06.2f}'.format(3.141592653589793) #003.14
'{:04d}'.format(42) # 0042

#знаковые числа
'{:+d}'.format(42) #+42
'{: d}'.format((- 23)) #-23
'{: d}'.format(42) # 42
'{:=5d}'.format((- 23)) #-  23
'{:=+5d}'.format(23) #+  23

#можно вставлять значения по именам
data = {'first': 'Hodor', 'last': 'Hodor!'}
'{first} {last}'.format(**data) #Hodor Hodor!
'{first} {last}'.format(first='Hodor', last='Hodor!') #Hodor Hodor!

#Формат даты и времени
from datetime import datetime
'{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5)) #2001-02-03 04:05


from celery import shared_task

from helper.celery import app

@shared_task
def create_dedup_event(data, dedup_key, task_pair_id):
    from helper.models import DedupEvent # avoid recursive import.
    return DedupEvent.objects.get_or_create(
        task_pair_id=task_pair_id, key=data[dedup_key])[1]

@shared_task
def dedup_effect_wrapper(data, dedup_key, task_pair_id, effect):
    from helper.models import DedupEvent # avoid recursive import.
    if DedupEvent.objects.get_or_create(task_pair_id=task_pair_id,
                                        key=data[dedup_key])[1]:
        effect.apply_async(
            (data,), link_error=remove_from_dedup_cache_on_error.s(
                dedup_key, task_pair_id, data))
        return data

@shared_task
def remove_from_dedup_cache_on_error(task_id, dedup_key, task_pair_id, data):
    from helper.models import DedupEvent # avoid recursive import.
    DedupEvent.objects.filter(task_pair_id=task_pair_id,
                              key=data[dedup_key]).delete()


import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from apachelogs import LogParser
from pathlib import Path
from ip2geotools.databases.noncommercial import DbIpCity

#function to ingest apache logs and parse out fields
def logFileParse(paths):
    for path in paths:
        path_in_str = str(path)
        with open(path_in_str) as fp:
            for entry in parser.parse_lines(fp):

                host = entry.directives["%h"]
                logName = (str(entry.directives["%l"]) == 'None' and '-') or str(entry.directives["%l"])
                user = (str(entry.directives["%u"]) == 'None' and '-') or str(entry.directives["%u"])
                time = str(entry.request_time)
                request = entry.directives["%r"]
                status = entry.directives["%>s"]
                bytesSent = entry.directives["%O"]

                lst.append([host,logName,user,time,request,status,bytesSent])

#function to call ip2geotools api and get country code by IP address
def getCountryByIP(lst):
    for i in lst:
        response = DbIpCity.get(i[0], api_key='free')
        i[0] = response.country

#define variables
projectLocation = '/home/jshivers/projects/apache-log-anonymizer'
parser = LogParser("%h %l %u %t \"%r\" %>s %O")
lst = []
paths = Path(projectLocation + '/logs').glob('*.log.*')
cols = ['host', 'logName', 'user', 'time', 'request', 'status', 'bytesSent']

#call ingest and transformation functions
logFileParse(paths)
getCountryByIP(lst)

#create pandas dataframe
df = pd.DataFrame(lst, columns=cols)

#create numpy array in case of further analysis
#a = np.array(lst)
#a[:,0]

#print cleaned data to txt file
with open(projectLocation + '/output/out.txt', 'w', encoding="utf-8") as x:
    for sub_list in lst:
        for item in sub_list:  #Since attempt to write whole giv error
            x.write(str(item) + ' ')
        x.write("\n")

#print dataframe to parquet file
table = pa.Table.from_pandas(df)
pq.write_table(table, projectLocation + '/output/out.parquet')

#read parquet file
#table2 = pq.read_table('C:/projects/apache-log-anonymizer/output/example.parquet')
#table2.to_pandas()


# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser. #http://127.0.0.1:8050/

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from joblib import load

app = dash.Dash(__name__)

report = load('./report.joblib')
fig_report = go.Figure(data=[go.Table(
    header=dict(values=list(report.columns),
                fill_color='paleturquoise',
                align='left'),
    cells=dict(values=report.transpose().values.tolist(),
               fill_color='lavender',
               align='left'))
])

result = load('./model_result.joblib')
history = pd.DataFrame(result['model_history'])
history['epoch'] = [i for i in range(1,history.shape[0]+1)]
fig_history =px.line(history, x=history.epoch, y=['loss', 'precision','accuracy','val_loss','val_precision','val_accuracy'])

app.layout = html.Div(children=[
    html.H1(children='Model Evaluation'),

    html.H5(children=' Classification report'),
    dcc.Graph(
        id='classification_report',
        figure=fig_report
    ),

    html.H5(children='parameters vs epoch'),
    dcc.Graph(
        id='model history',
        figure=fig_history
    )

])

if __name__ == '__main__':
    app.run_server(debug=True,host='0.0.0.0')


import requests
from bs4 import BeautifulSoup
import pandas as pd
import os
Year = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2010, 2011, 2012, 2013, 2014, 2015, 2016]
Month = []
Day = []

i = 1
while i < 13:
    Month.append(i)
    i += 1
i = 1
while i < 32:
    Day.append(i)
    i += 1

box_links = []
for y in Year:
    y = str(y)
    for m in Month:
        m = str(m)
        for d in Day:
            d = str(d)
            status = 1 - (Year[len(Year)-1] - (int(y)-1))/len(Year) + ((int(m)/12) * 1/len(Year)) + (int(d)/30 * 1/12 * 1/len(Year))
            print("{0:.8%}".format(status))
            try:
                date = str(m) + "/" + str(d) + "/" + str(y)
                link = "https://www.basketball-reference.com/boxscores/index.fcgi?month=" + m + "&day=" + d + "&year=" + y
                html = requests.get(link).content
                soup = BeautifulSoup(html, 'html.parser')
                table = soup.find_all('table')
                cell = table[1]
                z = cell.find_all('td')
                games = soup.find('div', attrs={"class": "section_heading"})

                if len(games.text) == 11:
                    game_count = games.text[0]
                else:
                    game_count = games.text[:1]

                gam = soup.find_all('div', attrs={"class": "game_summary expanded nohover"})

                z = 0
                game_bank = []
                running_record = []

                try:
                    while z < int(game_count):
                        t1 = gam[z].text
                        t2 = t1.split("\n")
                        for t in t2:
                            if len(t) < 2:
                                pass
                            else:
                                game_bank.append(t)
                        AQ = game_bank[8]
                        HQ = game_bank[10]
                        fin_game_bank = [game_bank[0], game_bank[1], game_bank[3], game_bank[4], AQ[:2], AQ[2:4], AQ[4:6],
                                         AQ[6:8], HQ[:2], HQ[2:4], HQ[4:6], HQ[6:8], date]
                        running_record.append(fin_game_bank)
                        game_bank.clear()
                        z += 1

                    Headers = ['Away Team', 'Away Total', 'Home Team', 'Home Total', 'AQ1', 'AQ2', 'AQ3', 'AQ4', 'HQ1', 'HQ2',
                               'HQ3', 'HQ4', 'Date']
                    game_record = pd.DataFrame(running_record, columns=Headers)
                    csv_file = game_record.to_csv(d + "_" + m + "_" + y + '_game_record.csv', index=True)
                except:
                    pass
            except:
                pass


# -*- coding: utf-8 -*-
from typing import Optional

from somali import aiorequests, config

class AI:
    base_url = config.ai_url
    key = config.ai_key

    @classmethod
    async def nlu(cls, text: str) -> Optional[dict]:
        url = f"{cls.base_url}/model/parse"
        params = {"token": cls.key}
        data = {"text": text}
        return await aiorequests.post(url, params=params, json=data)

    @classmethod
    async def predict(cls, text: str) -> dict:
        response = cls.nlu(text)
        intent: str = response["intent"]["name"]
        entity: Optional[str] = response["entities"][0]["value"] if response["entities"] else ""
        confidence: float = response["intent"]["confidence"]
        if intent == "nlu_fallback":
            intent = None
        elif entity and entity.startswith(
            ("o ", "a ", "do ", "da ", "no ", "na ", "em ")
        ):
            entity = entity.split(maxsplit=1)[1]
        return {"intent": intent, "entity": entity, "confidence": confidence}

    @staticmethod
    def small_talk(intent: str) -> Optional[str]:
        if intent == "ping":
            return "estou aqui"
        elif intent == "greet":
            return "oi"
        elif intent == "how_are_you":
            return "estou bem"
        elif intent == "who_are_you":
            return 'eu sou um bot, veja meus comandos digitando "%help"'
        elif intent == "praise":
            return "ðŸ˜Š"
        elif intent == "swear":
            return "ðŸ–•"


#!/usr/bin/env python3

"""A script to enforce versioning requirements for python packages.

Parsing requirements.txt files for requirements is nontrivial, so we use the
python pip.req module to do it for us, then use this script to wrap around
that functionality.
"""
import glob
import sys
import pip.req
import traceback

# Scan all directories for files named requirements.txt
requirements_files = glob.glob('**/requirements.txt', recursive=True)

for req_file in requirements_files:
    try:
        for item in pip.req.parse_requirements(req_file, session="somesession"):
            if isinstance(item, pip.req.InstallRequirement):

                # To check whether or not we have versioning, we check if specifiers
                # exist for a given install requirement.
                if not item.specifier:
                    print('{} has no version specifiers.'.format(item))
                    sys.exit(1)
    except Exception as exc:
        print('Exception while trying to parse requirements file {}: {}'.format(
            req_file, exc))
        sys.exit(1)

sys.exit(0)


from app.models import Patient, Embryo
from django.contrib.auth.models import User
from rest_framework import serializers
from datetime import datetime

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
	    view_name='user-detail',
	    lookup_field='username'
    )

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

#class PatientSerializer(serializers.ModelSerializer):
#    #user = serializers.HyperlinkedIdentityField(
#	   # view_name='user-detail',
#	   # lookup_field='id'
#    #)

#    class Meta:
#        model = Patient
#        fields = ('first_name','last_name','email','phone','user')

class PatientSerializer(serializers.ModelSerializer):
    #user = UserSerializer()# usting this will include the full user object data in the patient - not what we want
    #this code below will return the user id and it's essentially the same as the version above that uses a ModelSerializer
    #in both cases the user value is an id. removing the .lookup_field='username' yields the following error which I could not
    #get past: Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include
    #the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

    user = serializers.HyperlinkedIdentityField(
	    view_name='user-detail',
	    lookup_field='user'
    ).lookup_field='username'

    class Meta:
        model = Patient
        fields = ('first_name','last_name','email','phone','user')

#using the HyperlinkedModelSerializer requires the use of the url to reference the patient on POST/Create as so:http://localhost:8000/patient/1/
#instead of using the user id
#class EmbryoSerializer(serializers.HyperlinkedModelSerializer): #using this HyperlinkModelSelector returns the patient as url to locate the data
class EmbryoSerializer(serializers.ModelSerializer): #using the ModelSerializer returns the object's id instead of a hyperlink
    class Meta:
        model = Embryo
        fields = ('code_name','karyotype','patient')


import tensorflow as tf
import numpy as np
import tensorflow_probability as tfp

def inferenceLH(model,model_weights,xdata,rp):
    model.load_weights(model_weights)
    trained_model=model
    gpd_params=trained_model.predict(xdata)
    loc=0.0
    scale=tf.math.exp(gpd_params[:,0])
    conc=tf.nn.relu(gpd_params[:,1])
    dists=tfp.distributions.GeneralizedPareto(loc=0,scale=scale,concentration=conc,validate_args=False,allow_nan_stats=False,name='GeneralizedExtremeValue')

    intensity=dists.quantile((1-1/rp)).numpy()

    frequency=1/rp

    prob_occ=1-dists.cdf(0.1).numpy()

    landslide_hazard=prob_occ*intensity*frequency

    return landslide_hazard


from PyQt5.QtCore import QCoreApplication, QTimer

list1=[1,2,3,4,5]
delay = 2500

def calling_func():
    print("aqui")
    if list1:
        list_item = list1.pop()
        QTimer.singleShot(delay, lambda: target_func(list_item))

def target_func(list_item):
    print("fid= ",list_item)
    QTimer.singleShot(delay, calling_func)

app = QCoreApplication([])
calling_func()
app.exec_()


from flask import Flask
from flask_restful import Api, Resource,reqparse,abort , fields,marshal_with
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
api = Api(app)

#configuration setting for db
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///database.db' #relative path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

class VideoModel(db.Model): #this is to craete instance of record that will be eventually inserted into a table.
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100),nullable=False) #maximum is 100
    views = db.Column(db.Integer,nullable=False)
    likes = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return f"Video(name)={self.name}, views={self.views},likes={self.likes}"

#to actually create DB
"""db.create_all()
"""
#we only create db once. So after creating db, you should comment this out. Otherwise, you will overwrite things

video_put_args = reqparse.RequestParser()
video_put_args.add_argument("name",type=str,help="Name of the video is required",required=True)
video_put_args.add_argument("views",type=int,help="Views of the video is required",required=True)
video_put_args.add_argument("likes",type=int,help="Likes on the video is required",required=True)

#If for some reason your data object doesn’t have an attribute in your fields list,
# you can specify a default value to return instead of None.
resource_fields = {
    "id" : fields.Integer,
    "name" : fields.String,
    "views" : fields.Integer,
    "likes" : fields.Integer
}
#what I'm doing here is ... making dictionary that defines the fields
#from the VideoModel class

class Video(Resource):
    #with marshal_with , when we return, take this return value and
    #serialize it using fields from resource_fields
    @marshal_with(resource_fields)
    def get(self,video_id):
        #gonna query database and return something that fits the query that we have.
        result = VideoModel.query.filter_by(id=video_id).first() #it can be all() as well.

        if not result:
            abort(404, message="Couldn't find video with that id")

        return result
        #this will be the result of __repr__

    @marshal_with(resource_fields)
    def put(self,video_id):
        args = video_put_args.parse_args()

        #check validity of the query(because we are not going to put in instance of which the key has been already inserted in db)
        query = VideoModel.query.filter_by(id=video_id).first()
        if query:
            abort(409,message ="Video_id(Primary key) has been already taken")

        #to create instance that will be put in the db
        video = VideoModel(id=video_id, name=args['name'], views=args['views'], likes=args['likes'])

        #to insert the instance to db
        db.session.add(video)

        #commit()
        db.session.commit()

        #to check if the data has been inserted into db,
        return video,201

    # def delete(self,video_id):
    #     abort_if_video_id_doesnt_exist(video_id)
    #     #in case you get a request for deleting stuff,
    #     del videos[video_id]
    #     return "",204

api.add_resource(Video,"/video/<int:video_id>")

if __name__ == '__main__':
    app.run(debug=True)


import asyncio
from typing import Any, Dict, List, Tuple

from constants import API_URL
from helpers.api import fetch
from models.match import Match
from models.player import Player

class FetchDataException(Exception):
    message: str

    def __init__(self, message: str = "Data fetching failed."):
        super().__init__(message)
        self.message = message

async def fetch_last_match(params: Dict[str, Any]) -> Tuple[Match, List[Player], int]:
    response = await fetch(f"{API_URL}/player/matches", params)
    if response is None or len(response) == 0:
        raise FetchDataException("Player not found.")

    players = [Player(p) for p in response[0]["players"] if p["civ"] is not None]

    match = Match(response[0])
    match.set_num_ais(players)

    leaderboard_id = response[0]["leaderboard_id"] or 3

    return match, players, leaderboard_id

async def fetch_apply_leaderboard(params: Dict[str, Any], players: List[Player]) -> None:
    leaderboard = await asyncio.gather(
        *[
            fetch(f"{API_URL}/leaderboard", params={**params, "profile_id": p.profile_id})
            for p in players
            if not p.is_ai
        ]
    )
    for i, item in enumerate(leaderboard):
        players[i].from_leaderboard(item["leaderboard"])

async def fetch_apply_history(params: Dict[str, Any], players: List[Player]) -> None:
    history = await asyncio.gather(
        *[
            fetch(
                f"{API_URL}/player/ratinghistory",
                params={**params, "profile_id": p.profile_id, "count": 1},
            )
            for p in players
            if not p.is_ai
        ]
    )
    for i, item in enumerate(history):
        players[i].from_history(item)


from markdown import Markdown
from os import path,listdir
from Blask.errors import PageNotExistError
import math
import os

class BlogRenderer:
    """
    Class BlogRenderer: This class provides the feature for render posts from Markdown to HTML and search features.
    :Author: Zerasul <suarez.garcia.victor@gmail.com>
    Date: 2018-05-05
    Version: 0.1.0
    """
    postdir = None
    """
    Posts Directory
    """

    def __init__(self, postdir):
        """
        This is the constructor of the blog renderer.
        :param postdir: Posts Directory. See Settings py for more information.
        """
        self.postdir = postdir

    def renderfile(self, filename, abstract=None):
        """
            Render a markdown and returns the blogEntry.
        :param filename: Number of the file without extension.
        :return: BlogEntry.
        :raises PageNotExistError Raise this error if file does not exists.
        """
        filepath = path.join(self.postdir, filename + ".md")
        if not path.exists(filepath):
            raise PageNotExistError("{} does not exists".format(filename))
        with open(filepath, 'r', encoding='UTF-8') as content_file:
            content = content_file.read()
            if abstract is not None:
                content = content[:max(450, len(content)//10)]

            entry = self.rendertext(filename, content)
        return entry

    def rendertext(self,filename, text):
        """
         Render a Markdown Text and returns the BlogEntry.
        :param filename: filename or title of the post.
        :param text: Text write in Markdown.
        :return: BlogEntry.
        """
        md = Markdown(extensions=['full_yaml_metadata', 'codehilite', 'markdown.extensions.toc', 'mdx_math',
                                  'markdown_fenced_code_tabs'],
                      extension_configs={
                          'mdx_math': {'add_preview': True},
                          'markdown_fenced_code_tabs':{
                                'single_block_as_tab': False,
                                'active_class': 'active',
                                'template': 'default',
                            }
                      })
        entry = BlogEntry(filename, md, text)
        return entry

    def get_info(self, query=""):
        file = self.postdir + '/info.md'
        with open(file, 'r', encoding='UTF-8') as content_file:
            content = content_file.read()
        md = Markdown(extensions=['full_yaml_metadata', 'codehilite', 'markdown.extensions.toc', 'mdx_math'])
        content = md.convert(content)
        meta = md.Meta
        if meta is not None:
            return meta.get(query).split(",")

    def list_posts(self, tags=[], exclusions=["plda_algo.md", "404.md"], search="", category="", author=""):
        """
        Search a list of Posts returning a list of BlogEntry.
        :param tags: list of tags for searching.
        :param exclusions: list of name of posts with exclusions.
        :param search: string with the content what we want of search.
        :param category: list of category of the entry.
        :return: List of BlogEntry.
        """
        files = list(filter(lambda l: l.endswith('.md') and l not in exclusions, listdir(self.postdir)))
        mapfilter = list(map(lambda l: path.splitext(l)[0], files))
        entries = list(map(lambda l: self.renderfile(l), mapfilter))
        if tags:
            for tag in tags:
                entries = list(filter(lambda l: tag in l.tags, entries))
        if category:
            entries = list(filter(lambda c: c.category == category, entries))
        if author:
            entries = list(filter(lambda a: a.author == author, entries))
        if search:
            entries = list(filter(lambda l: search in l.content, entries))

        return entries

    def generatetagpage(self, postlist):
        """
        Get a HTML with links of the entries.
        :param postlist: List with BlogEntry.
        :return: String with the HTML list.
        """
        if postlist == []:
            return '
        Oops! seems like you get a bug.
'
        else:
            content = '
Search Result:
'
            content += '
'
            for post in postlist:
                entrycontent = "
{}
".format(post.name, post.name)
                content += entrycontent
            content += "
"
            return content

    def generate_abstract(self, entries, files):
        contents = []
        names = []
        dates = []
        for file in files:
            entry = self.renderfile(file, abstract=True)
            contents.append(entry.content)
            names.append(entry.name)
            dates.append(entry.date)
        entries.contents = contents
        entries.names = names
        entries.date = dates
        entries.type = 'index'
        entries.zip_datas = zip(dates, contents, names)
        return entries

    def get_recent_five_post(self, page):
        postdir = self.postdir
        ex = ['info.md']

        files = [s for s in os.listdir(postdir)
                 if os.path.isfile(os.path.join(postdir, s))]
        files = list(set(files).difference(set(ex)))
        print(files)
        page_num = math.ceil(len(files) / 5)
        print(page_num)
        files.sort(key=lambda s: os.path.getmtime(os.path.join(postdir, s)))
        files = files[::-1]
        files = files[(page-1)*5:(page-1)*5+5]
        fs = []
        for file in files:
            f = file[:-3]
            fs.append(f)
        files = fs
        return files, page_num

class BlogEntry:
    """"
    This class has the information about the Blog Posts.
    Author: Zerasul
    Version: 0.0.1.
    """
    content = None
    """Content of the post."""
    date = None
    """ Date of post creation"""
    tags = []
    """List of tags of the blog entry."""
    author = None
    """Author of the post"""
    category = None
    """category of the post"""
    template = None
    """Name of the template file"""
    name = None
    """ Name of the post"""
    type = None

    def __init__(self, name, md, content):
        """
        Default constructor
        :param name: name of the post
        :param md: Markdown information
        :param content: String with the Content in HTML.
        """
        self.content = md.convert(content)
        self.name = name
        meta = md.Meta
        if md.toc is not None:
            self.toc = md.toc
        if meta is not None:
            self.date = meta.get('date')
            self.tags = meta.get('tags').split(",")
            self.template = meta.get('template')
            self.category = meta.get('category')
            self.author = meta.get('author')
            self.type = meta.get('type')

    def __str__(self):
        string = "['content': {}, 'name': {}, 'date': {}, 'tags':[{}], 'author': {}, 'category': {}, template': {}]".format(self.content,self.name,self.date,self.tags,self.author,self.category,self.template)
        return string


# 기끔씩 이론과 현실사이에 괴리가 생길때가 있죠. 그때는 현실부정을 하면 됩니다. 그래서 만들엇습니다. 주작버전. 데이터를 마음껏(그러나 걸리지 않게 조심조심) 조작하십시오!

# 주작버전. A_B_Angle 값을 이용한 계산을 통해 그림을 그리지만, 벡터의 크기, 각의 크기 등 표시는 직접 입력하는거.
# https://www.youtube.com/watch?v=QUXKib-jfEM 처럼 유투브 링크 있는데에다가는 뭘 넣어도 상관없음. 걍 표시용이라. 유튜브 링크 대신 원하는 숫자를 넣으면 됨.
# 아 글고 저 노래들은내가 개인적으로 좋아하는 노래들임. 그 외에는 별 의미 없음. 뭘 넣어도 된다는것을 보여주기 위한거.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import math

X = np.array((0))
Y= np.array((0))

#------여기 아래에 값 넣으면 됨----------

a = 49.5 # 벡터 1 의 x 성분. 여기서는 49.5. 계산에 쓰이는 숫자.
b = 0    # 벡터 1 의 y 성분. 걍 0으로 두자....제바ㄹ
# 참고로 b =! 0 이면 터질수도 있음. 실험 안해봤고 해보기도 싫어. 코드 고치기 그ㅟ찮아앙

Vector_2_Length = 49.8 # 벡터 2 의 크기. 여기서는 49.8. 계산에 쓰이는 숫자.

A_B_Angle = 120 # 벡터 1과 벡터 2 사이 각. 계산에 쓰이는 숫자.
A_C_Angle = '요루시카 - 비와 카푸치노: https://www.youtube.com/watch?v=PWbRleMGagU'  # 벡터 1과 벡터 3 사이 각. 표시용이라 뭐든지 들어가도 됨
B_C_Angle = '술탄 오브 더 디스코 - 사라지는 꿈: https://www.youtube.com/watch?v=7fsavq0mU2k'  # 벡터 2와 벡터 3 사이 각. 표시용이라 뭐든지 들어가도 됨

label_1 = '달의하루 - 염라: https://www.youtube.com/watch?v=jv543Nk5s18' # 벡터 1 이름.
label_2 = '달의하루 - 너로피어오라 https://www.youtube.com/watch?v=3vhA8njtoQg' # 벡터 2 이름.
label_3 = '요루시카 - 그래서 나는 음악을 그만두었다. : https://www.youtube.com/watch?v=KTZ-y85Erus' # 벡터 3 이름. 글고 이름들은 ' ' 안에 넣어야함.

# x,y 축 최대. 최소 표시값. 그래프 잘리면 늘리고 너무 크면 좀 줄여. -80,80/ -60,60 하면 엔간한건 그려짐.
# 고치기 귀찮으면 실행시 뜨는 그래프에서 우클릭+드래그 하면 확대/축소됨.
xmin = -80
xmax = 80
ymin = -60
ymax = 60

# --------------------------------------

c = Vector_2_Length*(math.cos(math.radians(A_B_Angle)))
d = Vector_2_Length*(math.sin(math.radians(A_B_Angle)))

e = a + c
f = b + d

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     ansdist = round(dist,2)
     return ansdist

def angle_of_vectors(A,B,C,D):

     dotProduct = A*C + B*D
     modOfVector1 = math.sqrt(A*A + B*B)*math.sqrt(C*C + D*D)
     angle = dotProduct/modOfVector1
     angleInDegree = math.degrees(math.acos(angle))
     return angleInDegree

angle_1 = round(float(angle_of_vectors(a,b,c,d)),2)
angle_2 = round(float(angle_of_vectors(a,b,-e,-f)),2)
angle_3 = round(float(angle_of_vectors(c,d,-e,-f)),2)

angle_plot_1 = mpatches.Arc([0, 0], 15, 15, 0, 0, angle_1)

angle_plot_21 = mpatches.Arc([0, 0], 13, 13, -angle_2, 0, angle_2)
angle_plot_22 = mpatches.Arc([0, 0], 11, 11, -angle_2, 0, angle_2)

angle_plot_31 = mpatches.Arc([0, 0], 9, 9, angle_1, 0, angle_3)
angle_plot_32 = mpatches.Arc([0, 0], 10, 10, angle_1, 0, angle_3)
angle_plot_33 = mpatches.Arc([0, 0], 11, 11, angle_1, 0, angle_3)

fig, ax = plt.subplots()
q = ax.quiver(X, Y, a, b, units='xy' ,scale=1, color= 'r', label=label_1)
r = ax.quiver(X, Y, c, d,units='xy' ,scale=1, color= 'b', label=label_2)
s = ax.quiver(X, Y, e, f,units='xy' ,scale=1, color = 'k') # 합벡터 안 그릴려면 이 줄 주석처리.
t = ax.quiver(X, Y, -e, -f,units='xy' ,scale=1, color = 'g', label=label_3)

ax.add_patch(angle_plot_1)
ax.add_patch(angle_plot_21)
ax.add_patch(angle_plot_22)
ax.add_patch(angle_plot_31)
ax.add_patch(angle_plot_32)
ax.add_patch(angle_plot_33)

text_1 = str(A_B_Angle) + '°'
text_2 = str(A_C_Angle) + '°'
text_3 = str(B_C_Angle) + '°'

plt.text(7,5, text_1) #벡터 1--2 각도 표시. 7,5,가 글씨의 x,y좌표. 알아서 옮겨
plt.text(7,-7, text_2) #벡터 1--3 각도 표시. 7,-7,가 글씨의 x,y좌표. 알아서 옮겨
plt.text(-20,2, text_3) #벡터 2--3 각도 표시. -20,2,가 글씨의 x,y좌표. 알아서 옮겨

x_1 = [a, e]
y_1 = [b, f]
x_2 = [c, e]
y_2 = [d, f]

plt.plot(x_1, y_1, 'k--', alpha = 0.7) # 합벡터에서 평햏사변형 점선 안 그리려면 이거 주석처리.
plt.plot(x_2, y_2, 'k--', alpha = 0.7) # 합벡터에서 평행사변형 점선 안 그리려면 이거도 주석처리.

major_ticks = np.arange(-100, 120, 20)
minor_ticks = np.arange(-100, 120, 5)

ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)

ax.grid(which='both')

ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=1)
ax.set_axisbelow(True)

plt.axis('equal')

plt.axis([xmin, xmax, ymin, ymax])

plt.gca().set_aspect("equal")

plt.legend(loc='upper left', fontsize= 8)

#plt.title('Run #1')

plt.show()

벡터그리는거 V3(결과주작용).py

벡터그리는거 V3(결과주작용).py


from numpy import *
from numpy import matlib

from matplotlib import pyplot as plt
from PIL import Image
from scipy import misc
from scipy.interpolate import interp2d
plt.ion()

##if __name__ == "__main__":
##
##    main()

####mat=empty([100,100])
##periode = 2**arange(1,9,1)
##freq=1/periode
##
##for i in x
##    xlim0 = i//periode * periode
##    xlim1 = xlim0 + periode
##    for j in y
##        ylim0 = j//periode * periode
##        ylim1 = ylim0 + periode
##
size=1000
size_turbx=1000
size_turby=300
lambdax = 3
lambday = 40
xpower = 25
ypower = 160
nb_smooth = 7
persistence = 0.5
a=0
b=0
l=0
plt.figure()
while l<2 :
    if b == 0 :
        size_turb=size_turbx
        b=1
    else :
        size_turb=size_turby

    mat0=random.rand(size_turb,size_turb)
##    mat0[250:350:,250:350:]=random.rand(100,100)/2+0.5
    x0=arange(size_turb)
    y0=arange(size_turb)
    grids=[]

    pts=vstack((x0,y0))
    for j in arange(1,nb_smooth,1) :
        i=2**j
        x1=arange(0,size_turb,i)
        y1=arange(0,size_turb,i)
        f = interp2d(x0, y0, mat0)
        grid_z1=f(x1,y1)
        grids.append(grid_z1)

    per=1
    perlin_mat=empty((size_turb,size_turb))
    for mat in grids :
        mat=misc.imresize(mat,(size_turb,size_turb))
        per=per/persistence
        perlin_mat = perlin_mat + mat*per

    perlin_mat=perlin_mat/amax(perlin_mat)
    perlin_cut=perlin_mat.copy()
##    perlin_cut[0,0]=1.6
    plt.imshow(perlin_cut,cmap='autumn')
    plt.show()
    plt.pause(0.001)
    l=l+1

    perlin_cut=misc.imresize(perlin_cut,(size,size))
    perlin_cut=perlin_cut/amax(perlin_cut)
    if a == 0 :
        turb_x0=perlin_cut
        a=1
    else :
        turb_y0=perlin_cut
##misc.imread
##misc.imsave
##interp1

##    mat0[250:350:,250:350:]=random.rand(100,100)
x0=arange(-floor(size/2),floor(size/2),1)
y0=arange(-floor(size/2),floor(size/2),1)
X,Y=meshgrid(x0,y0)
##mat0=sin(sqrt(((X+turb_x0*xpower)**2)/lambdax+((Y+turb_y0*ypower)**2)/lambday))
mat0=sin((X+turb_x0*xpower)/lambdax)+0.1*sin((X+turb_y0*ypower)/lambday)
plt.imshow(mat0,cmap='gray')
plt.figure()