import torch
import os,glob
import random,csv

from torch.utils.data import Dataset,DataLoader
from torchvision import transforms
from PIL import Image

class AEGeneset(Dataset):
    def __init__(self,root,resize,mode):
        super(Dataset,self).__init__()
        self.root = root
        self.resize = resize

        self.name2label = {} # ”sq...“ : 0
        for name in sorted(os.listdir(os.path.join(root))):
            if not os.path.isdir(os.path.join(root,name)):
                continue
            self.name2label[name] = len(self.name2label.keys())

        #print(self.name2label)
        # image, label
        self.images,self.labels = self.load_csv('iamges.csv')

        if mode == 'train':
            self.images = self.images[:int(0.8 * len(self.images))]
            self.labels = self.labels[:int(0.8 * len(self.labels))]
        elif mode == 'all':
            self.images = self.images[:]
            self.labels = self.labels[:]
        else:
            self.images = self.images[int(0.8*len(self.images)):]
            self.labels = self.labels[int(0.8 * len(self.labels)):]

    def load_csv(self, filename):

        if not os.path.exists(os.path.join(self.root,filename)):

            images = []
            for name in self.name2label.keys():
                # 'data\\0\\0.jpg'
                images += glob.glob(os.path.join(self.root,name,'*jpg'))
            # 301, 'data\\1\\36.jpg'
            print(len(images),images)

            random.shuffle(images)
            with open(os.path.join(self.root, filename), mode='w',newline='') as f:
                writer = csv.writer(f)
                for img in images: # 'data\\0\\0.jpg'
                    name = img.split(os.sep)[-2]
                    label = self.name2label[name]
                    # 'data\\0\\0.jpg', 0
                    writer.writerow([img, label])
                print('writen into csv file:', filename)

        # read from csv file
        images,labels = [],[]
        with open(os.path.join(self.root,filename)) as f:
            reader = csv.reader(f)
            for row in reader:
                # 'data\\0\\0.jpg', 0
                img,label = row
                #name = img.split(os.sep)[-1][0]
                label = int(label)

                #names.append(name)
                images.append(img)
                labels.append(label)
        assert  len(images) == len(labels)

        return  images,labels

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        # idx~[0~len(images)]
        # self.images, self.labels
        # img: 'data\\0\\0.jpg'
        # label； 0
        img, label = self.images[idx],self.labels[idx]
        tf = transforms.Compose([
            lambda x:Image.open(x),
            #lambda x: Image.open(x).convert('RGB'), # string path =>  image data
            transforms.Resize((self.resize,self.resize)),
            transforms.ToTensor()
        ])
        name = torch.tensor(int(img.split(os.sep)[-1][:-4]))
        img = tf(img)
        all_label = torch.randn(2)
        all_label[0] = int(name)
        all_label[1] = label

        return img,all_label

def main():

    import visdom
    import time

    viz = visdom.Visdom()
    db = Geneset('data',100,'train')
    #
    x,y = next(iter(db))
    print('sample:',x.shape,y.shape,y[0],y[1])
    viz.images(x,win='sample_x',opts=dict(title='sample_x'))

    loader = DataLoader(db,batch_size=32,shuffle=True,)

    for x,y in loader:
        viz.images(x,nrow=8,win='batch',opts=dict(title='batch'))
        viz.text(str(y.numpy()),win='label',opts=dict(title='batch_y'))
        time.sleep(10)

if __name__ == '__main__':
    main()


from django.db import IntegrityError
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import ValidationError

from issuetrackingsystem.models import Project, Issue, Comment, Contributor
from issuetrackingsystem.serializers import (
    ProjectDetailSerializer,
    IssueDetailSerializer,
    CommentDetailSerializer,
    ContributorDetailSerializer,
    ProjectListSerializer,
    CommentListSerializer,
    IssueListSerializer,
    ContributorListSerializer
    )
from issuetrackingsystem.permissions import (
    IsUserContributor,
    IsAuthor,
    ContributorIsNotCreator,
    HasProjectWritePermission
)

def create_contributor(user, project):
    contributor = Contributor.objects.create(
        user_foreign_key=user,
        project_foreign_key=project,
        user_id=user.user_id,
        project_id=project.project_id
    )
    contributor.role = contributor.Role.CREATOR
    contributor.permission = Contributor.Permission.CRUD
    return contributor.save()

class MultipleSerializerMixin:
    detail_serializer_class = None

    def get_serializer_class(self):
        if self.detail_serializer_class is not None:
            if self.action == "retrieve":
                return self.detail_serializer_class
            elif self.action == "create":
                return self.detail_serializer_class
            elif self.action == "update":
                return self.detail_serializer_class
            elif self.action == "partial_update":
                return self.detail_serializer_class
        return super().get_serializer_class()

class ProjectViewset(MultipleSerializerMixin, ModelViewSet):

    serializer_class = ProjectListSerializer
    detail_serializer_class = ProjectDetailSerializer

    permission_classes = [IsAuthenticated, HasProjectWritePermission]

    def get_queryset(self):
        return Project.objects.filter(
            contributors__user_id=self.request.user.user_id
        )

    def perform_create(self, serializer):
        project_save = serializer.save()
        project = Project.objects.get(project_id=project_save.project_id)
        user = self.request.user
        create_contributor(user, project)
        return serializer

class ContributorViewset(MultipleSerializerMixin, ModelViewSet):

    serializer_class = ContributorListSerializer
    detail_serializer_class = ContributorDetailSerializer

    permission_classes = [
        IsAuthenticated, IsUserContributor, ContributorIsNotCreator
    ]

    def get_queryset(self):
        return Contributor.objects.filter(project_id=self.kwargs["project_pk"])

    def perform_create(self, serializer):
        project = Project.objects.get(project_id=self.kwargs["project_pk"])
        try:
            serializer.save(
                user_id=serializer.validated_data["user_foreign_key"].user_id,
                project_id=self.kwargs["project_pk"],
                project_foreign_key=project,
            )
        except IntegrityError as e:
            raise ValidationError({"detail": e})
        return serializer

    def perform_update(self, serializer):
        try:
            serializer.save(
                user_id=serializer.validated_data["user_foreign_key"].user_id,
            )
        except IntegrityError as e:
            raise ValidationError({"detail": e})
        return serializer

class IssueViewset(MultipleSerializerMixin, ModelViewSet):

    serializer_class = IssueListSerializer
    detail_serializer_class = IssueDetailSerializer

    permission_classes = [IsAuthenticated, IsUserContributor, IsAuthor]

    def get_queryset(self):
        return Issue.objects.filter(project_id=self.kwargs["project_pk"])

    def perform_create(self, serializer):
        try:
            project = Project.objects.get(project_id=self.kwargs["project_pk"])
            Contributor.objects.get(
                user_foreign_key=serializer.validated_data["assignee_user_id"],
                project_foreign_key=project
            )
        except Exception as e:
            raise ValidationError({"detail": e})
        serializer.save(
            author_user_id=self.request.user,
            project_id=self.kwargs["project_pk"],
            project_foreign_key=project,
        )
        return serializer

    def perform_update(self, serializer):
        try:
            project = Project.objects.get(project_id=self.kwargs["project_pk"])
            Contributor.objects.get(
                user_foreign_key=serializer.validated_data["assignee_user_id"],
                project_foreign_key=project
            )
        except Exception as e:
            raise ValidationError({"detail": e})
        serializer.save(
            project_id=self.kwargs["project_pk"],
            project_foreign_key=project,
        )
        return serializer

class CommentViewset(MultipleSerializerMixin, ModelViewSet):

    serializer_class = CommentListSerializer
    detail_serializer_class = CommentDetailSerializer

    permission_classes = [IsAuthenticated, IsUserContributor, IsAuthor]

    def get_queryset(self):
        try:
            Issue.objects.get(id=self.kwargs["issue_pk"])
        except Issue.DoesNotExist as e:
            raise ValidationError({"detail": e})
        return Comment.objects.filter(issue_id=self.kwargs["issue_pk"])

    def perform_create(self, serializer):
        try:
            issue = Issue.objects.get(id=self.kwargs["issue_pk"])
        except Issue.DoesNotExist as e:
            raise ValidationError({"detail": e})
        serializer.save(
            author_user_id=self.request.user,
            issue_id=issue,
        )
        return serializer


# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    generator.py                                       :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: alvgomez <alvgomez@student.42.fr>          +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2023/03/24 12:29:48 by alvgomez          #+#    #+#              #
#    Updated: 2023/04/13 16:22:26 by alvgomez         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

from random import randint

def generator(text, sep=" ", option=None):
    '''Divide el texto de acuerdo al valor de sep y producirá las sub-strings.
        option especifica si una acción se realizará sobre las sub-strings antes de ser producidas.
    '''
    if isinstance(text, str) and isinstance(sep, str) and (option == None or option == "shuffle" or option == "unique" or option == "ordered"):
        list = text.split(sep)
        if option == "ordered":
            list.sort()
            return list
        elif option == "unique":
            new = set(list)
            return new
        elif option == "shuffle":
            new = []
            while len(new) != len(list):
                pos = randint(0, len(list) - 1)
                if list[pos] not in new:
                    new.append(list[pos])
            return new
        else:
         return list
    else:
        print("ERROR")
        exit()

#if __name__ == "__main__":
#    text = "Le Lorem Ipsum est simplement du faux texte."
#    for word in generator(text, sep=" ", option="shuffle"):
#        print(word)


import os,sys

sys.path.append(os.getcwd())
import yaml

def base_analyse(filename,data_key):
    file="."+os.sep+"data"+os.sep+filename
    with open(file,"r") as f:
        data=yaml.load(f,Loader=yaml.FullLoader)
        arrs=[]
        for i in data[data_key].values():
            # print(i)
            arrs.append(i)
        return arrs

if __name__ == '__main__':
    data=base_analyse("new_msg.yaml","test_new_msg")
    print(data)


import matplotlib.pyplot as plt
import pandas as pd
import pprint
from tabulate import tabulate

pp = pprint.PrettyPrinter(depth=4)

from history import get_history
from ichimoku import get_ichimoku
from strategybasic import basic_strategy

#---------------INTERFACE---------------------
symbol = 'ETHUSDT'
limit = 300 #max 1000
indicatorlimit = 100

strategy_obj = {
    'focus_zone': False,     # make true when focus zone starts
    'focus_start' : None,    # will have start time and price
    'focus_end' : None,      # will have end time and price
    'start_price' : None,    # price at start of focus zone
    'end_price' : None,      # price at end of focus zone
    'trade' : -1,        # only one trade per focus zone
                         # -1: no trade | 0: in trade | 1: trade completed for this focus
}
#---------------------------------------------

data = get_history(symbol, limit)

result = []

frame = data[0:indicatorlimit]    #used for making the indicator
backtestdata = data[-1*(data.shape[0]-indicatorlimit):] #rest of the data used for testing further

#for loop for running backtest
for index, row in backtestdata.iterrows():
    print("index:", index)
    frame = frame.iloc[1:]      #for poping first row
    frame = frame.append(row)   #for adding new row
    print('frame')
    print(frame)
    strategy_obj = basic_strategy(frame, strategy_obj)
    print("strategy_obj")
    pp.pprint(strategy_obj)

    #appending the focus zones
    if strategy_obj['focus_end'] and strategy_obj['focus_start']:
        result.append(strategy_obj)
        #resting the strategy object
        strategy_obj = {
            'focus_zone': False,
            'focus_start' : None,
            'start_price' : None,
            'end_price' : None,
            'focus_end' : None,
            'trade' : -1,

        }

    print('---------------------------------------------------------------------------------')

print('result')
print('symbol: ', symbol)
print('data points: ', limit)

result = pd.DataFrame(result)
result = result.drop('focus_zone', axis=1)
print(tabulate(result, headers = 'keys', tablefmt = 'psql'))


"""
Course: cmps 4883
Assignemt: A03
Date: 02/06/2019
Github username: aanaree
Repo url: https://github.com/aanaree/4883-SWTools-greene
Name: Ackeem Greene
Description:
        This progroms scrapes NFL gameddata from their website and used the data
        to extract the infomation and store it into a '.json' file
"""
from beautifulscraper import BeautifulScraper
from pprint import pprint
import urllib
import json
import sys
from time import sleep

scraper = BeautifulScraper()
delays = [.01,.02,.03,.04,.05]
pages = [x+1 for x in range(5)]

f = open("nfl_dat.json","w")

#used to change auto matically changed the years and weeks in each website
years = [x for x in range (2009,2019)]
weeks = [x for x in range (1,18)]

sREG = "REG"
sPOST = "POST"
#stores each game id
gameids =[]

#REGULAR SEASON STATS
for year in years:
        #gameids[year] = []
        for week in weeks:
                url = "http://www.nfl.com/schedules/%s/%s%s"%(year,sREG,str(week))
                page = scraper.go(url)

                #extracts the Game ID using the div from the nfl website
                divs = page.find_all('div',{"class":"schedules-list-content"})
                for div in divs:
                        gameids.append(div['data-gameid'])

#uses scraped game ID's to extract each games data and store it into a .json file
for gameid in gameids:
        url2 = "http://www.nfl.com/liveupdate/game-center/%s/%s_gtd.json"%(gameid,gameid)
        #stores the game data into the .json file
        urllib.request.urlretrieve(url2, 'nfl_json/'+gameid)

#POST SEASON STATS
for year in years:
        url = "http://www.nfl.com/schedules/%s/%s"%(year,sPOST)
        page = scraper.go(url)

        #extracts the Game ID using the div from the nfl website
        divs = page.find_all('div',{"class":"schedules-list-content"})
        for div in divs:
                gameids.append(div['data-gameid'])

#uses scraped game ID's to extract each games data and store it into a .json file
for gameid in gameids:
        url2 = "http://www.nfl.com/liveupdate/game-center/%s/%s_gtd.json"%(gameid,gameid)
        #stores the game data into the .json file
        urllib.request.urlretrieve(url2, 'nflp_json/'+gameid+'.json')

f.write(json.dumps(gameids))


import unittest

from anthrax.container import Form, Container
from anthrax.field import TextField, IntegerField
from util import dummy_frontend

class Test(unittest.TestCase):

    def setUp(self):
        class TestForm(Form):
            __frontend__ = dummy_frontend
            name = TextField(
                regexp=r'^[A-Z][a-z]+$',
                regexp_message='Write your name with a capital',
                max_len=20, max_len_message='You must be kidding!',
            )
            nickname = TextField(max_len=30)
            age = IntegerField(min=7, max=99)
        self.form = TestForm()
        class TestForm2(Form):
            __frontend__ = dummy_frontend
            class personals(Container):
                name = TextField(
                    regexp=r'^[A-Z][a-z]+$',
                    regexp_message='Write your name with a capital',
                    max_len=20, max_len_message='You must be kidding!',
                )
                nickname = TextField(max_len=30)
            age = IntegerField(min=7, max=99)
        self.form2 = TestForm2()

    def test_conversion(self):
        """Test a 'to raw' conversion."""
        self.form['name'] = 'Sir Galahad'
        self.form['nickname'] = 'The Pure'
        self.form['age'] = 20
        self.assertDictEqual(dict(self.form.__raw__), {
            'name': 'Sir Galahad', 'nickname': 'The Pure', 'age': '20'
        })

    def test_nested_conversion(self):
        """Test a 'to raw' conversion with fieldsets."""
        self.form2['personals-name'] = 'Sir Galahad'
        self.form2['personals-nickname'] = 'The Pure'
        self.form2['age'] = 20
        self.assertDictEqual(dict(self.form2.__raw__), {
            'personals': {'name': 'Sir Galahad', 'nickname': 'The Pure'},
            'age': '20'
        })

    def test_empty(self):
        """Test a raw value of an empty form."""
        self.assertEqual(len(self.form.__raw__), 3)
        self.assertDictEqual(dict(self.form.__raw__), {
            'name': '', 'nickname': '', 'age': ''
        })


import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import sys

CCRF_LUT_R = np.zeros((256,256))
CCRF_LUT_G = np.zeros((256,256))
CCRF_LUT_B = np.zeros((256,256))

def run_CCRF(f2q,fq):
    CCRF = np.zeros(fq.shape)
    for i in range(f2q.shape[0]):
        for j in range(f2q.shape[1]):
            X_B = fq[i][j][0]
            Y_B = f2q[i][j][0]
            CCRF[i][j][0] = CCRF_LUT_B[X_B][Y_B]
            X_G = fq[i][j][1]
            Y_G = f2q[i][j][1]
            CCRF[i][j][1] = CCRF_LUT_G[X_G][Y_G]
            X_R = fq[i][j][2]
            Y_R = f2q[i][j][2]
            CCRF[i][j][2] = CCRF_LUT_R[X_R][Y_R]
    return CCRF

CCRF_LUT_R = np.loadtxt('img/CCRF_R.txt')
CCRF_LUT_G = np.loadtxt('img/CCRF_G.txt')
CCRF_LUT_B = np.loadtxt('img/CCRF_B.txt')

f2q = cv2.imread("img/ccrf_6.jpg")
fq = cv2.imread("img/ccrf_5.jpg")
ccrf = run_CCRF(f2q,fq)
cv2.imwrite('img/ccrf_1_5.jpg',ccrf)


from libavg import avg, gesture, app

import gestures

RESOLUTION = avg.Point2D(800, 600)

nodeList = []
nodesEnabled = True

def abortAll():
    for node in nodeList:
        node.recognizer.abort()

def switchNodesEnabled():
    global nodesEnabled
    nodesEnabled = not nodesEnabled
    for node in nodeList:
        node.recognizer.enable(nodesEnabled)

class TapButton(gestures.TextRect):
    def __init__(self, text, **kwargs):
        super(TapButton, self).__init__(text, **kwargs)

        self.recognizer = gesture.TapRecognizer(node=self,
                    possibleHandler=self._onPossible, detectedHandler=self._onDetected,
                    failHandler=self._onFail)

    def _onPossible(self):
        self.rect.fillcolor = "FFFFFF"

    def _onDetected(self):
        self.rect.fillcolor = "000000"
        self.rect.color = "00FF00"

    def _onFail(self):
        self.rect.fillcolor = "000000"
        self.rect.color = "FF0000"

class AbortButton(TapButton):
    def __init__(self, text, **kwargs):
        super(AbortButton, self).__init__(text, **kwargs)

    def _onPossible(self):
        super(AbortButton, self)._onPossible()
        self.words.color = "000000"

    def _onDetected(self):
        super(AbortButton, self)._onDetected()
        abortAll()
        self.words.color = "FFFFFF"

    def _onFail(self):
        super(AbortButton, self)._onFail()
        self.words.color = "FFFFFF"

class EnableButton(TapButton):
    def __init__(self, text, **kwargs):
        super(EnableButton, self).__init__(text, **kwargs)

        self.words.color = "FF0000"

    def changeText(self):
        if(nodesEnabled):
            self.words.text = "Disable all"
            self.words.color = "FF0000"
        else:
            self.words.text = "Enable all"
            self.words.color = "00FF00"

    def _onDetected(self):
        super(EnableButton, self)._onDetected()
        switchNodesEnabled()
        self.changeText()

class GestureDemoDiv(app.MainDiv):

    def onInit(self):

        avg.WordsNode(text='''a - abort recognition

                d - enable/disable recognition

                or use the buttons on the right side''',
                pos=(20, 510), parent=self)

        nodeList.append(gestures.HoldNode(text="HoldRecognizer", pos=(20,20),
                parent=self))

        nodeList.append(gestures.DragNode(text="DragRecognizer
friction",
                pos=(200,20), friction=0.05, parent=self))

        nodeList.append(gestures.TransformNode(text="TransformRecognizer",
                ignoreRotation=False, ignoreScale=False, pos=(380,20), parent=self))

        self.abortButton = AbortButton(text="Abort all", pos = (630, 490), parent=self)

        self.enableButton = EnableButton(text="Disable all", pos = (630, 540),
                parent=self)

        app.keyboardmanager.bindKeyDown(text="a", handler=abortAll,
                help="abort recognition")
        app.keyboardmanager.bindKeyDown(text="d", handler=self.onEnableKey,
                help="Enable/disable recognition")

    def onEnableKey(self):
        switchNodesEnabled()
        self.enableButton.changeText()

if __name__ == '__main__':
    app.App().run(GestureDemoDiv(), app_resolution="800,600")


#!/usr/bin/env python

# Each entry in the __objc_methlist has an entry like this
# struct method_t {
#     SEL name;
#     const char *types;
#     IMP imp;
# }
# The normal Xref only finds xrefs the imp field which is dynamically resolved at Obj-C runtime
# from the name by objc_msgSend. This script finds the __objc_selrefs address (SEL) of the
# desired function and Xref it, without modifying with the idb
# Note: Only works after IDA finishes caching the functions

import ida_kernwin
import ida_xref
import idautils

def find_sel_ref(ea):
    for xref in idautils.XrefsTo(ea, 0):
        if get_segm_name(xref.frm) == '__objc_methlist':
            sel_ref_addr = ida_xref.get_first_dref_from(xref.frm - 8)
            if get_segm_name(sel_ref_addr) == '__objc_selrefs':
                return sel_ref_addr
    return None

def xref_selref():
    sel_ref_addr = find_sel_ref(here())
    if sel_ref_addr != None:
        ida_kernwin.open_xrefs_window(sel_ref_addr)

hotkey_ctx = ida_kernwin.add_hotkey("Alt-Shift-X", xref_selref)


from flask import Flask, render_template
from testModelDB import db, User

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

@app.route("/")
def hello():
    db.drop_all()
    db.create_all()
    db.session.add(User("John Doe", "john.doe@example.com"))
    db.session.add(User("Bill Smith", "smith.bill@example.com"))
    db.session.commit()
    all_users = User.query.all()
    return render_template('index.html', all_users=all_users)

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


from airflow.utils.dates import days_ago
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
from airflow.contrib.kubernetes.secret import Secret
from airflow.contrib.kubernetes.volume import Volume
from airflow.contrib.kubernetes.volume_mount import VolumeMount
from airflow.contrib.kubernetes.pod import Port

args = {
    'owner': 'Airflow',
    'start_date': days_ago(2),
}

dag = DAG(
    dag_id='fire-data-processing',
    default_args=args,
    schedule_interval=None,
    tags=['afms']
)

env_vars = {
    'SSH_CRED':'/gadi/id_rsa_gadi',
    'WALD_SETTINGS':'/opt/fire/config/defaults-mk.json'
}

data_key = Secret('volume', '/gadi_orig', 'gadi','id_rsa_gadi')
data_id = Secret('volume','/etc/ssh/gadi-id','gadi-id','fingerprint')
config_file = Secret('volume','/opt/fire/config','fmc-config','defaults-mk.json')
secrets = [data_key,data_id,config_file]
# secret_env  = Secret('env', 'SQL_CONN', 'airflow-secrets', 'sql_alchemy_conn')
# secret_all_keys  = Secret('env', None, 'airflow-secrets-2')
volume_mount = VolumeMount('g-data',
                            mount_path='/g/data',
                            sub_path=None,
                            read_only=False)
# port = Port('http', 80)
# configmaps = ['test-configmap-1', 'test-configmap-2']

volume_config= {
    'persistentVolumeClaim':
      {
        'claimName': 'g-data'
      }
    }

pod_volume = Volume(name='g-data', configs=volume_config)

# affinity = {
#     'nodeAffinity': {
#       'preferredDuringSchedulingIgnoredDuringExecution': [
#         {
#           "weight": 1,
#           "preference": {
#             "matchExpressions": {
#               "key": "disktype",
#               "operator": "In",
#               "values": ["ssd"]
#             }
#           }
#         }
#       ]
#     },
#     "podAffinity": {
#       "requiredDuringSchedulingIgnoredDuringExecution": [
#         {
#           "labelSelector": {
#             "matchExpressions": [
#               {
#                 "key": "security",
#                 "operator": "In",
#                 "values": ["S1"]
#               }
#             ]
#           },
#           "topologyKey": "failure-domain.beta.kubernetes.io/zone"
#         }
#       ]
#     },
#     "podAntiAffinity": {
#       "requiredDuringSchedulingIgnoredDuringExecution": [
#         {
#           "labelSelector": {
#             "matchExpressions": [
#               {
#                 "key": "security",
#                 "operator": "In",
#                 "values": ["S2"]
#               }
#             ]
#           },
#           "topologyKey": "kubernetes.io/hostname"
#         }
#       ]
#     }
# }

# tolerations = [
#     {
#         'key': "key",
#         'operator': 'Equal',
#         'value': 'value'
#      }
# ]

AU_TILES = [
    "h27v11", "h27v12", "h28v11", "h28v12",
    "h28v13", "h29v10", "h29v11", "h29v12",
]
#     "h29v13", "h30v10", "h30v11", "h30v12",
#     "h31v10", "h31v11", "h31v12", "h32v10",
#     "h32v11"
# ]

run_this = BashOperator(
    task_id='run_after_loop',
    bash_command='echo 1',
    dag=dag,
)

debug_task_one = KubernetesPodOperator(dag=dag,
                                namespace='default',
                                image="anuwald/fire-data-processing",
                                cmds=["ls", "-lh","/gadi","/etc/ssh","/opt/fire/config"],
                                # cmds=["python", "update_fmc.py","-t",tile,"-y","2020","-dst","/g/data/fmc_%s.nc"%tile],
                                arguments=[],
                                labels={"foo": "bar"},
                                env_vars=env_vars,
                                secrets=secrets,
                                # ports=[port]
                                volumes=[pod_volume],
                                volume_mounts=[volume_mount],
                                name='debug_task_one',
                                task_id='debug_task_one',
                                # affinity=affinity,
                                is_delete_operator_pod=True,
                                hostnetwork=False,
                                startup_timeout_seconds=360
                                # tolerations=tolerations,
                                # configmaps=configmaps
                                )

debug_task_one >> run_this

debug_task_one = KubernetesPodOperator(dag=dag,
                                namespace='default',
                                image="anuwald/fire-data-processing",
                                cmds=["env"],
                                # cmds=["python", "update_fmc.py","-t",tile,"-y","2020","-dst","/g/data/fmc_%s.nc"%tile],
                                arguments=[],
                                labels={"foo": "bar"},
                                env_vars=env_vars,
                                secrets=secrets,
                                # ports=[port]
                                volumes=[pod_volume],
                                volume_mounts=[volume_mount],
                                name='debug_task_two',
                                task_id='debug_task_two',
                                # affinity=affinity,
                                is_delete_operator_pod=True,
                                hostnetwork=False,
                                startup_timeout_seconds=360
                                # tolerations=tolerations,
                                # configmaps=configmaps
                                )

debug_task_one >> run_this

fmc_mosaic_task_name="DUMMY_update_fmc_mosaic"
fmc_mosaic_task = KubernetesPodOperator(dag=dag,
                                namespace='default',
                                image="anuwald/fire-data-processing",
                                cmds=["python", "-c","print('Dummy task - pretending to process FMC mosaic')"],
                                # cmds=["python", "update_fmc.py","-t",tile,"-y","2020","-dst","/g/data/fmc_%s.nc"%tile],
                                arguments=[],
                                labels={"foo": "bar"},
                                env_vars=env_vars,
                                secrets=secrets,
                                # ports=[port]
                                volumes=[pod_volume],
                                volume_mounts=[volume_mount],
                                name=fmc_mosaic_task_name,
                                task_id=fmc_mosaic_task_name,
                                # affinity=affinity,
                                is_delete_operator_pod=True,
                                hostnetwork=False,
                                startup_timeout_seconds=360
                                # tolerations=tolerations,
                                # configmaps=configmaps
                                )

flam_mosaic_task_name="DUMMY_update_flammability_mosaic"
flam_mosaic_task = KubernetesPodOperator(dag=dag,
                                namespace='default',
                                image="anuwald/fire-data-processing",
                                cmds=["python", "-c","print('Dummy task - pretending to process Flammability mosaic')"],
                                # cmds=["python", "update_fmc.py","-t",tile,"-y","2020","-dst","/g/data/fmc_%s.nc"%tile],
                                arguments=[],
                                labels={"foo": "bar"},
                                env_vars=env_vars,
                                secrets=secrets,
                                # ports=[port]
                                volumes=[pod_volume],
                                volume_mounts=[volume_mount],
                                name=flam_mosaic_task_name,
                                task_id=flam_mosaic_task_name,
                                # affinity=affinity,
                                is_delete_operator_pod=True,
                                hostnetwork=False,
                                startup_timeout_seconds=360
                                # tolerations=tolerations,
                                # configmaps=configmaps
                                )

fmc_mosaic_task >> run_this
flam_mosaic_task >> run_this

for tile in AU_TILES:
    fmc_task_name="update_fmc_%s"%tile
    fmc_commands = [
        "ln -s /etc/ssh/gadi-id/fingerprint /etc/ssh/ssh_known_hosts",
        "mkdir /gadi",
        "cp /gadi_orig/id_rsa_gadi /gadi",
        "chmod 600 /gadi/id_rsa_gadi",
        "python update_fmc.py -t %s -d 2020 -dst /g/data/fmc_%s.nc -tmp /tmp"%(tile,tile)
    ]
    fmc_task = KubernetesPodOperator(dag=dag,
                                 namespace='default',
                                 image="anuwald/fire-data-processing",
                                 cmds=[
                                     "/bin/bash","-c",
                                     " && ".join(fmc_commands)
                                 ],
                                #  cmds=[
                                #      "python", "update_fmc.py",
                                #      "-t",tile,
                                #      "-d","2020",
                                #      "-dst","/g/data/fmc_%s.nc"%tile,
                                #      "-tmp","/tmp"],
                                 arguments=[],
                                 labels={"foo": "bar"},
                                 env_vars=env_vars,
                                 secrets=secrets,
                                 # ports=[port]
                                 volumes=[pod_volume],
                                 volume_mounts=[volume_mount],
                                 name=fmc_task_name,
                                 task_id=fmc_task_name,
                                 # affinity=affinity,
                                 is_delete_operator_pod=True,
                                 hostnetwork=False,
                                 startup_timeout_seconds=360
                                 # tolerations=tolerations,
                                 # configmaps=configmaps
                                 )
    fmc_task >> fmc_mosaic_task

    flam_task_name="DUMMY_update_flam_%s"%tile
    flam_task = KubernetesPodOperator(dag=dag,
                                 namespace='default',
                                 image="anuwald/fire-data-processing",
                                #  cmds=["python", "update_flammability.py","-t",tile,"-y","2020","-dst","/g/data/fmc_%s.nc"%tile],
                                 cmds=["python", "-c","print('Dummy task - pretending to process flammability for tile %s')"%tile],
                                 arguments=[],
                                 labels={"foo": "bar"},
                                 env_vars=env_vars,
                                 secrets=secrets,
                                 # ports=[port]
                                 volumes=[pod_volume],
                                 volume_mounts=[volume_mount],
                                 name=flam_task_name,
                                 task_id=flam_task_name,
                                 # affinity=affinity,
                                 is_delete_operator_pod=True,
                                 hostnetwork=False,
                                 startup_timeout_seconds=360
                                 # tolerations=tolerations,
                                 # configmaps=configmaps
                                 )

    fmc_task >> flam_task
    flam_task >> flam_mosaic_task


# Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
#
#
#
# Example 1:
#
# Input: root = [3,1,4,null,2], k = 1
#       3
#      / \
#     1   4
#      \
#       2
# Output: 1
# Example 2:
#
# Input: root = [5,3,6,2,4,null,null,1], k = 3
#       5
#      / \
#     3   6
#    / \
#   2   4
#  /
# 1
# Output: 3
# Follow up:
# What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
#
#
#
# Constraints:
#
# The number of elements of the BST is between 1 to 10^4.
# You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

from utils import *

class Solution:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        # recursive solution
        # self.k = k
        #
        # def inorder_traverse(root):
        #     if root is None:
        #         return
        #
        #     l_res = inorder_traverse(root.left)
        #     if l_res:
        #         return l_res
        #
        #     # inorder traverse visits values from smallest to largest
        #     self.k -= 1
        #     if self.k == 0:
        #         return root
        #
        #     r_res = inorder_traverse(root.right)
        #     if r_res:
        #         return r_res
        #
        # return inorder_traverse(root).val

        # iterative solution: iterations are easier to control
        # iterative inorder traverse: #0094
        stack = []

        def traverse_left(node):
            while node is not None:
                stack.append(node)
                node = node.left

        traverse_left(root)
        while len(stack) > 0:
            node = stack.pop()
            k -= 1
            if k == 0:
                return node.val
            traverse_left(node.right)


import os

def get_type(type_name: str) -> str:
    """
    The function returns the relative path to files of the specified type when instances end, the function returns None.
    """
    path = os.path.join('dataset', type_name)
    list_img = os.listdir(path)
    list_img.append(None)
    for i in range(len(list_img)):
        yield os.path.join(path, list_img[i]) if list_img[i] is not None else None

def main() -> None:

    type1 = 'polar_bear'
    # type2 = 'brown_bear'

    print(*get_type(type1))

if __name__ == "__main__":
    main()


import cv2
import numpy as np
import math

if __name__ == '__main__':
    vn = input('Video Name or type * to run on a whole directory:  ')
    if '*' in vn:
        print('This feature is not supported yet')
    if '.' in vn:
        vidcap = cv2.VideoCapture('./data/'+vn)
    else:
        vidcap = cv2.VideoCapture('./data/'+vn+'.MP4')

    success, image = vidcap.read()
    fr = math.ceil(vidcap.get(cv2.CAP_PROP_FPS))
    count = 0
    calibration = None
    # Get the first frame
    r = []
    r_timer = []
    thresehold = 0.05
    t_sum = 0

    with open('roi.txt', 'r') as f:
        for val in f:
            r.append(int(val))

    with open('roi_timer.txt', 'r') as f:
        for val in f:
            r_timer.append(int(val))

    while success:
        success, image = vidcap.read()
        if count == 0:
            # crop the image
            calibration = image[int(r[1]):int(
                r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
            count += 1
            continue
        try:
            selection = image[int(r[1]):int(r[1]+r[3]),
                              int(r[0]):int(r[0]+r[2])]
            diff = 255 - cv2.absdiff(calibration, selection)
            data = np.asarray(diff, dtype="int32")
            sum_ = (diff.sum())
            if count == 1:
                t_sum = sum_
                count += 1
                continue
            count += 1
            if ((sum_ > t_sum+t_sum*thresehold) or (sum_ < t_sum-t_sum*thresehold)):
                timer = image[int(r_timer[1]):int(
                    r_timer[1]+r_timer[3]), int(r_timer[0]):int(r_timer[0]+r_timer[2])]
                print(f'Occured at time: {count/fr}s, Occured on frame: {count}, Detected Frame Rate: {fr}')
                cv2.imshow("Image", selection)
                cv2.imshow("timer", timer)
                cv2.waitKey(1)
                inp = input('Type y to confirm this is correct: ')
                if (inp == 'y'):
                    break

        except:
            print("failed img reading")


# Read the initial sequences
sequence1 = set(map(int, input().split()))
sequence2 = set(map(int, input().split()))

# Read the number of commands
n = int(input())

# Process the commands
for _ in range(n):
    command = input().split()
    operation = command[0]
    numbers = set(map(int, command[2:]))

    if operation == "Add":
        if command[1] == "First":
            sequence1.update(numbers)
        elif command[1] == "Second":
            sequence2.update(numbers)
    elif operation == "Remove":
        if command[1] == "First":
            sequence1 -= numbers
        elif command[1] == "Second":
            sequence2 -= numbers
    elif operation == "Check":
        if sequence1.issubset(sequence2) or sequence2.issubset(sequence1):
            print("True")
        else:
            print("False")

# Print the final sequences
sequence1 = sorted(sequence1)
sequence2 = sorted(sequence2)
print(", ".join(map(str, sequence1)))
print(", ".join(map(str, sequence2)))

# First, you will be given two sequences of integer values on different lines.
# The values of the sequences are separated by a single space between them.
# Keep in mind that each sequence should contain only unique values.
# Next, you will receive a number - N. On the following N lines, you will receive one of the following commands:
#     • "Add First {numbers, separated by a space}" - add the given numbers at the end of the first sequence of numbers.
#     • "Add Second {numbers, separated by a space}" - add the given numbers at the end of the second sequence of numbers.
#     • "Remove First {numbers, separated by a space}" - remove only the numbers contained in the first sequence.
#     • "Remove Second {numbers, separated by a space}" - remove only the numbers contained in the second sequence.
#     • "Check Subset" - check if any of the given sequences are a subset of the other. If it is, print "True".
# Otherwise, print "False".In the end, print the final sequences, separated by a comma and a space ", ".
# The values in each sequence should be sorted in ascending order.
#
# Input:
#     1 2 3 4 5
#     1 2 3
#     3
#     Add First 5 6
#     Remove Second 8 9 11
#     Check Subset
# Output:
#     True
#     1, 2, 3, 4, 5, 6
#     1, 2, 3


import argparse
import subprocess
import sys
import os

# Argument parser setup
parser = argparse.ArgumentParser(description="Clean directories using ./exec")
parser.add_argument("path", help="Path of the original lvm")
parser.add_argument("--max-processes", type=int, default=3, help="Maximum number of concurrent processes")

# Parse arguments
args = parser.parse_args()

# Path of the original lvm
file_path = args.path

# Maximum number of concurrent processes
max_processes = args.max_processes

# List of processes
processes = []

def run_commands_in_parallel(commands):
    for command in commands:
        # If we've reached the maximum number of concurrent processes, wait for one to finish
        if len(processes) >= max_processes:
            # Wait for the first process to finish and remove it from the list
            processes.pop(0).wait()

        # Spawn a process for the command and add it to the list of processes
        processes.append(subprocess.Popen(command))

    # Wait for all remaining processes to complete
    for process in processes:
        process.wait()

def check_file_exists(file_path):
    if os.path.isfile(file_path):
        return True
    else:
        return False

def perform_split(file_path):
    print('Performing split...')
    with open(file_path, 'r') as file:
        os.system(f'split {file_path} -C300MB')

def move_chunks_to_folders(prefix):
    print('Moving chunks to folders...')
    index = 1
    chunks = []
    for file in os.listdir():
        if file.startswith('x'):
            chunks.append(file)

    chunks.sort()

    max_digits = len(str(len(chunks)))

    folders = []

    for chunk in chunks:

        folder = f'{prefix}-{str(index).zfill(max_digits)}'
        # We create the directory in case it doesn't exist
        if not os.path.exists(folder):
            os.system(f'mkdir {folder}')

        # We move the chunk to the directory
        os.system(f'mv {chunk} {folder}/data.lvm')

        folders.append(folder)

        index += 1

    return folders

def complete_chunks(folders):
    print('Completing chunks...')

    commands = [['../DropFinder/drop_finder', 'fill_gaps', f'{folder}/data.lvm'] for folder in folders]

    run_commands_in_parallel(commands)

def compute_dynamic_avg(folders):
    print('Computing dynamic average...')

    commands = [
        ['../DropFinder/drop_finder',
        'compute_dynamic_avg',
        f'{folder}/data_complete.lvm',
        f'{folders[i-1]}/data_complete.lvm' if i > 0 else '',
        f'{folders[i+1]}/data_complete.lvm' if i < len(folders) - 1 else '' ]
    for i, folder in enumerate(folders)]

    run_commands_in_parallel(commands)

if __name__ == "__main__":

    if not check_file_exists(file_path):
        print("File does not exist")
        exit(2)

    perform_split(sys.argv[1])

    [year, month, day] = file_path.split('-')[:3]

    folders = move_chunks_to_folders(f'{year}-{month}-{day}')

    complete_chunks(folders)

    compute_dynamic_avg(folders)

    exit(0)


# The selenium.webdriver module provides all the WebDriver implementations.
# Currently supported WebDriver implementations are Firefox, Chrome, IE and Remote.
# The Keys class provide keys in the keyboard like RETURN, F1, ALT etc.
# The By class is used to locate elements within a document.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# Adding the default driver path
driver_path = r'geckodriver.exe'

# turning off the logs
PATH_TO_DEV_NULL = 'nul'

# Next, the instance of Firefox WebDriver is created.
driver = webdriver.Firefox(executable_path=driver_path,service_log_path=PATH_TO_DEV_NULL)

# The driver.get method will navigate to a page given by the URL.
# WebDriver will wait until the page has fully loaded
# (that is, the “onload” event has fired) before returning control to your test or script.
# Be aware that if your page uses a lot of AJAX on
# load then WebDriver may not know when it has completely loaded:
driver.get("http://www.python.org")

# The next line is an assertion to confirm that title has the word “Python” in it:
assert "Python" in driver.title

# WebDriver offers a number of ways to find elements using the find_element method.
# For example, the input text element can be located by
# its name attribute using the find_element method and using By.NAME
# as its first parameter.
# A detailed explanation of finding elements is available in the Locating Elements chapter:
elem = driver.find_element(By.NAME, "q")

# Next, we are sending keys, this is similar to entering keys using your keyboard.
# Special keys can be sent using the
# Keys class imported from selenium.webdriver.common.keys. To be safe, we’ll
# first clear any pre-populated text in the input field (e.g. “Search”) so it
# doesn’t affect our search results:
elem.clear()
elem.send_keys(Keys.RETURN)

# After submission of the page, you should get the result if there is any.
# To ensure that some results are found, make an assertion:
assert "No results found." not in driver.page_source

# Finally, the browser window is closed. You can also call the
# quit method instead of close. The quit method will exit the browser
# whereas close will close one tab, but if just one tab was open, by
# default most browsers will exit entirely.:
driver.close()


import numpy as np

A = np.array([[1,0,3], [0,5,6]])
B =np.ones_like(A)
print(A)

ind = np.nonzero(A)
print(ind)
B[ind] = A[ind]
print(B)


"""Get weather from OpenWeather Map"""
import time
import os
import re
import requests
from flask import Blueprint, request
from labbot.controllers import utils

omw_api_key = os.environ.get("OMW_KEY")

#influx = InfluxDBClient(config['influx']['host'], 8086, config['influx']\
# ['username'], config['influx']['password'], 'weather')
weather = Blueprint('weather', __name__, template_folder='templates/weather')
@weather.route('/weather', defaults={'page': 'current'})
@weather.route('/weather/current', methods=['POST'])
def get_weather():
    """Get the current weather
    """

    utils.validate_token(request.form.get('token', None))
    text = request.form.get('text', None)
    #channel = request.form.get('channel_id', None)
    args = text.split()
    omw_zip = os.environ.get("OMW_ZIP")

    unit = 'c'
    regex = re.compile(r"\d\d\d\d\d")
    if len(args) > 0:
        if len(args[0]) == 1:
            if ('f' in args[0] or 'c' in args[0] or 'k' in args[0]):
                unit = args[0]
        if regex.match(args[0]):
            omw_zip = args[0] + ",us"
    if len(args) > 1:
        if regex.match(args[1]):
            omw_zip = args[1] + ",us"

    omw_weather = get_owm(omw_zip)
    temperature = Temperature(omw_weather['main']['temp'])

    message = f"The current temperature is {getattr(temperature, unit):.1f}{unit.upper()}"
    print(text)
    return message
    #payload = {'text': json.dumps(request.form)}
    #return jsonify(payload)

def every(delay, task, *args, **kwargs):
    """Perform action every x"""
    next_time = time.time() + delay
    print(time.time(), next_time)
    while True:
        time.sleep(max(0, next_time - time.time()))
        #try:
        task(*args, **kwargs)
        #except Exception:
        #    traceback.print_exception()
            # in production code you might want to have this instead of course:
            # logger.exception("Problem while executing repetitive task.")
        # skip tasks if we are behind schedule:
        next_time += (time.time() - next_time) // delay * delay + delay

class Temperature:
    """Store a temperature and return"""
    def __init__(self, temp, unit="k"):
        """Store a temperature"""
        if unit == "c":
            self.temp_kelvin = convert_c_to_k(temp)
        elif unit == "f":
            self.temp_kelvin = convert_f_to_k(temp)
        elif unit == "k":
            self.temp_kelvin = int(temp)
        else:
            raise SystemError

    def get_fahrenheit(self):
        """Return Fahrenheit"""
        return convert_k_to_f(self.temp_kelvin)

    def get_celsius(self):
        """Return Celsius"""
        return convert_k_to_c(self.temp_kelvin)

    def get_kelvin(self):
        """Return Kelvin"""
        return self.temp_kelvin

def convert_c_to_k(celsius):
    """Convert a temperature from Celsius to Kelvin"""
    return celsius + 273.15

def convert_f_to_k(fahrenheit):
    """Convert a temperature from Fahrenheit to Kelvin"""
    return (fahrenheit - 32) * 5 / 9 + 273.15

def convert_k_to_f(kelvin):
    """Convert a temperature from Kelvin to Fahrenheit"""
    return (kelvin - 273.15) * 9 / 5 + 32

def convert_k_to_c(kelvin):
    """Convert a temperature from Kelvin to Celsius"""
    return kelvin - 273.15

def get_owm(omw_zip):
    """Get the JSON from OMW"""
    base_url = "https://api.openweathermap.org/data/2.5/weather?"
    complete_url = f"{base_url}zip={omw_zip}&appid={omw_api_key}"
    print(complete_url)
    response = requests.get(complete_url)
    response_json = response.json()
    if response_json["cod"] != "404":
        return response_json
    return None

def influx_temp(omw_zip):
    """Some deprecated specific data for Influx"""
    raw_data = get_owm(omw_zip)
    tags = { "city": "North Hollywood",
             "datasource": "OpenWeatherMaps",
             "state": "CA",
             "note:": "Commercial",
             "latitude": raw_data["coord"]["lat"],
             "longitude": raw_data["coord"]["lon"],}

    data = []

    data.append({"measurement": "sky",
                "tags": tags,
                "fields": {"value": raw_data["weather"][0]["main"]}
                })

    data.append({ "measurement": "temperature",
              "tags": tags,
              "fields": { "value": raw_data["main"]["temp"]}
              })
    data.append({ "measurement": "pressure",
              "tags": tags,
              "fields": { "value": raw_data["main"]["pressure"]}
              })
    data.append({ "measurement": "humidity",
              "tags": tags,
              "fields": { "value": raw_data["main"]["humidity"]}
              })
    data.append({ "measurement": "visibility",
              "tags": tags,
              "fields": { "value": raw_data["visibility"]}
              })
    data.append({ "measurement": "wind_speed",
              "tags": tags,
              "fields": { "value": raw_data["wind"]["speed"]}
              })
    data.append({ "measurement": "wind_direction",
              "tags": tags,
              "fields": { "value": raw_data["wind"]["deg"]}
              })
    data.append({ "measurement": "clouds",
              "tags": tags,
              "fields": { "value": raw_data["clouds"]["all"]}
              })

    return data


import numpy as np
from scipy.optimize import *

from models import Location

class Trilateration(object):
    def locate(self, dists, positions, start_pos=(3, 5)):
        pos_mtrx, dists_squared = self.matrices(dists, positions)
        size = len(dists_squared)
        start_pos = np.array(start_pos)

        def fun(xs):
            xys = np.array([[xs[0], xs[1], -2]] * size)
            dif = pos_mtrx - xys
            squared_sum = np.square(dif).sum(1)
            return np.abs((squared_sum - dists_squared)).sum()

        res = minimize(fun, start_pos, bounds=[(0, 33), (0, 12)])
        return Location(float(res.x[0]), float(res.x[1]), -2.0)

    def matrices(self, dists, positions):
        dists_squared = []
        positions_mtrx = []
        for mac, dist in dists.items():
            dists_squared.append(dist**2)
            location = positions[mac]
            positions_mtrx.append([location.x, location.y, location.z])
        return np.array(positions_mtrx), np.array(dists_squared)


from django.shortcuts import get_object_or_404
from rest_framework import filters, viewsets
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import AllowAny

from posts.models import Follow, Group, Post
from .permissions import IsOwnerOrReadOnly
from .serializers import (
    CommentSerializer,
    FollowSerializer,
    GroupSerializer,
    PostSerializer,
)

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [IsOwnerOrReadOnly]
    pagination_class = LimitOffsetPagination

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

class GroupViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [AllowAny]

class CommentViewSet(viewsets.ModelViewSet):
    serializer_class = CommentSerializer
    permission_classes = [IsOwnerOrReadOnly]

    def get_queryset(self):
        post_id = self.kwargs.get("post_id")
        post = get_object_or_404(Post, pk=post_id)
        return post.comments

    def perform_create(self, serializer):
        post_id = self.kwargs.get("post_id")
        serializer.save(
            author=self.request.user, post=get_object_or_404(Post, pk=post_id)
        )

class FollowViewSet(viewsets.ModelViewSet):
    queryset = Follow.objects.all()
    serializer_class = FollowSerializer
    http_method_names = ["get", "post", "head"]
    filter_backends = (filters.SearchFilter,)
    search_fields = (
        "user__username",
        "following__username",
    )

    def get_queryset(self):
        return Follow.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)


import numpy as np
import matplotlib.pyplot as plt

x_train = np.arange(-1,1,0.05)
y_train = 1.2*np.sin(np.pi*x_train) - np.cos(2.4*np.pi*x_train)

x_test = np.arange(-1,1,0.01)
y_test = 1.2*np.sin(np.pi*x_test) - np.cos(2.4*np.pi*x_test)

np.random.seed(2)
g_noise = np.random.normal(0,1,y_train.shape[0])
y_train_n = y_train + 0.3*g_noise

sigma = 0.1
lmd_set = [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 3, 5, 10]

def guassian(r):
    return np.exp(-r**2/(2*sigma**2))

def phi_matrix(x):
    d = x.shape[0]
    phi = np.zeros((d,d))
    for i in range(d):
        for j in range(d):
            r = x[i]-x[j]
            phi[i][j] = guassian(r)
    return phi

def weights(x,d,lmd):
    phi = phi_matrix(x)
    I = np.identity(phi.shape[0])
    return np.linalg.inv(np.dot(phi.T,phi) + lmd*I).dot(phi.T).dot(d)

def predict(x_train,x_test,weights):
    y_pre = []
    for j in range(x_test.shape[0]):
        y = 0
        for i in range(x_train.shape[0]):
            r = x_test[j] - x_train[i]
            y += weights[i]*guassian(r)
        y_pre.append(y)
    return y_pre

pre_test = []
for i in range(len(lmd_set)):
    w = weights(x_train,y_train_n,lmd_set[i])
    pre_test.append(predict(x_train,x_test,w))

plt.figure()
for i in list(range(0,9,2)):
    plt.subplot(1,2,1)
    plt.plot(x_test,y_test,color = 'red',label ='Test Output')
    plt.plot(x_test,pre_test[i],color = 'blue',label ='Predicted Output')
    plt.title('The result of the exact interpolation with regularization λ = '+ str(lmd_set[i]))
    plt.ylim(-3,3)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()

    plt.subplot(1,2,2)
    plt.plot(x_test,y_test,color = 'red',label ='Test Output')
    plt.plot(x_test,pre_test[i+1],color = 'blue',label ='Predicted Output')
    plt.title('The result of the exact interpolation with regularization λ = '+ str(lmd_set[i+1]))
    plt.ylim(-3,3)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.show()
    plt.clf()

err_set = []
for i in range(len(lmd_set)):
    err_set.append(np.mean(abs((pre_test[i] - y_test)/y_test)))
print(min(err_set))
plt.plot(lmd_set,err_set)
plt.xlabel('Regularization Factor')
plt.ylabel('Average Relative Error')
plt.title('Average Relative Error versus the Regularization Factor on Test Data Set')
plt.show()


n = int(input('Digite quantos elemento de fibonacci voce que ver? '))
print('~~'*28)
orden = 0
t1 = 0
t2 = 1
print('{} -> {}'.format(t1,t2), end="")
while orden != n:
    fibonacci = t1 + t2
    t1 = t2
    t2 = fibonacci
    print(' -> {}'.format(fibonacci), end='')
    orden +=1
print(' -> FIM')


from sys import path
import re,os
import urllib.request
import ssl
import scrapy
from sys import path
path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../..")))
from scrapy.http import Request
from taobao.items import TaobaoItem
from scrapy_redis.spiders import RedisCrawlSpider
from scrapy.spiders import CrawlSpider, Rule

class TbSpider(RedisCrawlSpider):
    name = "tb"
    #allowed_domains = ["taobao.com"]
    redis_key = 'mycrawler:start_urls'
    start_urls = ['http://taobao.com/']

    def __init__(self, key=None,page=0, *args, **kwargs):
        super(TbSpider, self).__init__(*args, **kwargs)
        self.key=key
        self.page=int(page)

    def parse(self, response):
    	for i in range(0,self.page): #此处可以控制爬取该商品的页数，每页大约44件商品
    	    url = "https://s.taobao.com/search?q="+str(self.key)+"&search_type=item&s="+str(44*i)
    	    yield Request(url=url,callback=self.pages)

    def pages(self,response):
        body = response.body.decode("utf8","igrone")
        item = TaobaoItem()
        price = re.compile('"view_price":"(.*?)"').findall(body)
        title = re.compile('"raw_title":"(.*?)"').findall(body)
        comment_count = re.compile('"comment_count":"(.*?)"').findall(body)
        sales = re.compile('"view_sales":"(.*?)"').findall(body)
        location = re.compile('"item_loc":"(.*?)"').findall(body)
        kuaidi = re.compile('"view_fee":"(.*?)"').findall(body)
        shopname = re.compile('"nick":"(.*?)"').findall(body)
        isTmall = re.compile('"isTmall":(.*?),').findall(body)
        allitemid = re.compile('"nid":"(.*?)"').findall(body)
        allshopid = re.compile('"nid":"(.*?)"').findall(body)
        user_id = re.compile('"user_id":"(.*?)"').findall(body)

        for i in range(0,len(allitemid)):
            if isTmall[i]=='true':
                item["link"]="https://detail.tmall.com/item.htm?id="+str(allitemid[i])
                allrateContent=[]
                allUserNick=[]
                allrateDate=[]
                allauctionSku=[]
                for z in range(1,3):
                    url = "https://rate.tmall.com/list_detail_rate.htm?itemId="+str(allitemid[z])+"&sellerId="+str(user_id[z])+"&order=3&currentPage="+str(z)
                    body=urllib.request.urlopen(url).read().decode("gbk")
                    rateContent=re.compile('"rateContent":"(.*?)"').findall(body)
                    UserNick=re.compile('"displayUserNick":"(.*?)"').findall(body)
                    rateDate=re.compile('"rateDate":"(.*?)"').findall(body)
                    auctionSku=re.compile('"auctionSku":"(.*?)"').findall(body)
                    allrateContent.extend(rateContent)
                    allUserNick.extend(UserNick)
                    allrateDate.extend(rateDate)
                    allauctionSku.extend(auctionSku)

            else:
                item["link"]="https://item.taobao.com/item.htm?id="+str(allitemid[i])
                allrateContent=[]
                allUserNick=[]
                allrateDate=[]
                allauctionSku=[]
                for z in range(1,3):
                    url = "https://rate.taobao.com/feedRateList.htm?auctionNumId="+str(allitemid[z])+"&userNumId="+str(user_id[z])+"&currentPageNum="+str(z)+"&pageSize=20"
                    body=urllib.request.urlopen(url).read().decode("gbk")
                    rateContent=re.compile('":null,"content":"(.*?)"').findall(body)
                    UserNick=re.compile('"nick":"(.*?)"').findall(body)
                    rateDate=re.compile('"date":"(.*?)"').findall(body)
                    auctionSku=re.compile('"sku":"(.*?)"').findall(body)
                    allrateContent.extend(rateContent)
                    allUserNick.extend(UserNick)
                    allrateDate.extend(rateDate)
                    allauctionSku.extend(auctionSku)

            item["allrateContent"]=allrateContent
            item["allUserNick"]=allUserNick
            item["allrateDate"]=allrateDate
            item["allauctionSku"]=allauctionSku

            item["price"]=price[i]
            item["title"]=title[i]
            item["comment_count"]=comment_count[i]
            item["sales"]=sales[i]
            item["shopname"]=shopname[i]
            item["location"] = location[i]
            item["kuaidi"] = kuaidi[i]

            yield item

#os.system('scrapy crawl tb')


#!/usr/bin/python
# coding:utf-8

import os
import cv2
import numpy as np
import shutil
from ctools.basic_func import get_all_files

input_dir_o = "/home/cobot/Desktop/bad"
output_dir = "/home/cobot/Desktop/bad2"

for input_dirs in os.listdir(input_dir_o):
    input_dir = os.path.join(input_dir_o, input_dirs)
    files = os.listdir(input_dir)

    warp_mode = cv2.MOTION_TRANSLATION
    number_of_iterations = 100
    termination_eps = 1e-8
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps)

    results = np.zeros((len(files), len(files)))
    for i in range(len(files)):
        img1 = cv2.imread(os.path.join(input_dir, files[i]))
        img1_gray = np.zeros(img1.shape[0:2], dtype=np.uint8)
        cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY, img1_gray)
        for j in range(i, len(files)):
            img2 = cv2.imread(os.path.join(input_dir, files[j]))
            img2_gray = np.zeros(img2.shape[0:2], dtype=np.uint8)
            cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY, img2_gray)

            warp_matrix = np.eye(2, 3, dtype=np.float32)
            try:
                (cc, M) = cv2.findTransformECC(img1_gray, img2_gray, warp_matrix, warp_mode, criteria)
            except:
                cc = 0
            results[i, j] = cc
            results[j, i] = cc

    numbers = 20
    ind = [0] * numbers
    # find the first one
    ind[0] = np.argmax(np.sum(results, axis=0))

    # find the next one
    temp = results[ind[0], :]
    for i in range(numbers - 1):
        results[:, ind[i]] = 1
        ind[i + 1] = np.argmin(temp)
        temp = temp + results[ind[i + 1], :]

    for i in range(5):
        print(i, ind[i], files[ind[i]])
        src = os.path.join(input_dir, files[ind[i]])
        dst = os.path.join(output_dir, input_dirs + os.sep + files[ind[i]])
        if not os.path.exists(os.path.dirname(dst)):
            os.makedirs(os.path.dirname(dst))
        shutil.copy(src, dst)

        # ind[2] = np.argmin(results[ind[0], :] + results[ind[1], :])


import numpy as np
from matplotlib import pyplot as plt
from collections import defaultdict

def draw_circles(p, r):
	n = p.shape[0]
	for i in range(n):
		t = np.linspace(0, 2*np.pi)
		x = p[i, 0] + r[i] * np.cos(t)
		y = p[i, 1] + r[i] * np.sin(t)
		plt.plot(x, y)

def iterate(p, v, r):
	dt = 0.2
	n = p.shape[0]
	colls = defaultdict(list)
	for i in range(n):
		p0 = p[i,:]
		v0 = v[i,:]
		r0 = r[i]
		for j in range(i+1, n):
			p1 = p[j,:]
			v1 = v[j,:]
			r1 = r[j]
			p_dist = np.linalg.norm(p0-p1)
			v_dist = np.linalg.norm(v0-v1)

			p_dist = np.linalg.norm(p0-p1)
			v_dist = np.linalg.norm(v0-v1)
			qp = 2*np.dot(p0-p1, v0-v1) / v_dist**2
			qq = (p_dist**2 - (r0+r1)**2) / v_dist**2
			if qp*qp/4 > qq:
				t = -qp/2 - (qp*qp/4-qq)**.5
				if 0 < t < dt:
					colls[i].append((j, t))
					colls[j].append((i, t))

	for i in range(n):
		if len(colls[i]) == 0:
			p[i,:] += v[i,:] * dt
		else:
			j, t = colls[i][0]
			p[i,:] += v[i,:] * t

	for i in range(n):
		if len(colls[i]) > 0:
			j, t = colls[i][0]
			p_rel = p[i,:] - p[j,:]
			# print(p_rel)
			n = p_rel / np.linalg.norm(p_rel)
			v[i,:] -= 2*np.dot(v[i,:], n) * n
			p[i,:] += v[i,:] * (dt-t)

n = 10
# p = np.array([[np.random.random(), np.random.random()] ])
p = np.random.rand(n, 2)*100
# v = np.array([[3.0, 0.0], [0.0, 2.0]])
v = np.random.randn(n, 2)*10
r = np.abs(np.random.randn(n))
draw_circles(p, r)

for i in range(200):
	iterate(p, v, r)
	draw_circles(p, r)
plt.axis('square')

plt.xlim([0, 100])
plt.ylim([0, 100])
plt.show()


import http.client
import logging
import os
import shutil
import subprocess
import sys
import time
from copy import copy

import pytest
import requests

import settings
from client.api_client import ApiClient

repo_root = os.path.abspath(os.path.join(__file__, os.pardir))
repo_root_2 = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir))
if sys.platform.startswith('win'):
    venv_dir = 'Scripts'
    python_name = 'python'
    exit_code = 1
else:
    venv_dir = 'bin'
    python_name = 'python3.10'
    exit_code = -15
python_path = os.path.join(repo_root_2, 'venv', venv_dir, python_name)

def wait_ready(host, port):
    started = False
    st = time.time()
    while time.time() - st <= 5:
        try:
            requests.get(f'http://{host}:{port}')
            started = True
            break
        except requests.exceptions.ConnectionError:
            pass

    if not started:
        raise RuntimeError('App did not started in 5s!')

def pytest_configure(config):
    base_test_dir = os.path.join(repo_root, 'tests_logs')

    if not hasattr(config, 'workerinput'):
        if os.path.exists(base_test_dir):
            shutil.rmtree(base_test_dir)
        os.makedirs(base_test_dir)

    config.base_test_dir = base_test_dir

    if not hasattr(config, 'workerinput'):
        ######### app configuration #########

        app_path = os.path.join(repo_root, 'application', 'app.py')

        env = copy(os.environ)
        env.update({'APP_HOST': settings.APP_HOST, 'APP_PORT': settings.APP_PORT})
        env.update({'MOCK_HOST': settings.MOCK_HOST, 'MOCK_PORT': settings.MOCK_PORT})

        app_stderr_path = os.path.join(repo_root, 'tmp', 'app_stderr')
        app_stdout_path = os.path.join(repo_root, 'tmp', 'app_stdout')
        app_stderr = open(app_stderr_path, 'w')
        app_stdout = open(app_stdout_path, 'w')

        app_proc = subprocess.Popen([python_path, app_path], stderr=app_stderr, stdout=app_stdout, env=env)
        config.app_proc = app_proc
        config.app_stderr = app_stderr
        config.app_stdout = app_stdout
        wait_ready(settings.APP_HOST, settings.APP_PORT)

        ######### mock configuration #########

        mock_path = os.path.join(repo_root, 'mock', 'flask_mock.py')

        mock_stderr_path = os.path.join(repo_root, 'tmp', 'mock_stderr')
        mock_stdout_path = os.path.join(repo_root, 'tmp', 'mock_stdout')
        mock_stderr = open(mock_stderr_path, 'w')
        mock_stdout = open(mock_stdout_path, 'w')

        mock_proc = subprocess.Popen([python_path, mock_path], stderr=mock_stderr, stdout=mock_stdout, env=env)
        config.mock_proc = mock_proc
        config.mock_stderr = mock_stderr
        config.mock_stdout = mock_stdout
        wait_ready(settings.MOCK_HOST, settings.MOCK_PORT)

@pytest.fixture(scope='function')
def temp_dir(request):
    test_dir = os.path.join(request.config.base_test_dir, request._pyfuncitem.nodeid)
    test_dir = os.path.abspath(test_dir).replace('::', '_')
    os.makedirs(test_dir)
    return test_dir

@pytest.fixture(scope='session')
def api_client():
    return ApiClient()

@pytest.fixture(scope='function', autouse=True)
def logger(temp_dir):
    log = logging.getLogger(__name__)
    http.client.HTTPConnection.debuglevel = 1

    def print_to_log(*args):
        log.debug(' '.join(args))
    http.client.print = print_to_log

    log_file = os.path.join(temp_dir, 'test.log')
    log_level = logging.DEBUG

    file_handler = logging.FileHandler(log_file, 'w')
    file_handler.setLevel(log_level)

    log.propagate = False
    log.setLevel(log_level)
    log.handlers.clear()
    log.addHandler(file_handler)

    yield log

    for handler in log.handlers:
        handler.close()

def pytest_unconfigure(config):
    config.app_proc.terminate()
    exit_code_app = config.app_proc.wait()

    config.app_stderr.close()
    config.app_stdout.close()

    config.mock_proc.terminate()
    exit_code_mock = config.mock_proc.wait()

    config.mock_stderr.close()
    config.mock_stdout.close()

    assert exit_code_app == exit_code
    assert exit_code_mock == exit_code


from django.conf.urls import url
from .views import AuthorEdit, AuthorList, author_create_many, books_authors_create_many
from django.urls import path, include

app_name = 'p_library'
urlpatterns =([
    url('author_book/create_many', (books_authors_create_many), name='books_authors_create_many'),
    url('author/create_many', (author_create_many), name='author_create_many'),
    url('author/create', AuthorEdit.as_view(), name='author_create'),
    url('authors', AuthorList.as_view(), name='author_list'),
], 'p_library')

urlpattern = [
    path('p_library', include(urlpatterns)),
]


# leetcode challenge 496: next greater element I
# given two arrays without duplicates A1 and A2 where A1 elements
# are a subset of A2
# find the next greater elements of A1 in A2, or return -1 if not exists

# input: A1 = [4, 1, 2] A2 = [1, 3, 4, 2]
# output: [-1, 3, -1]

# input: A1 = [2, 4] A2 = [1, 2, 3, 4]
# output: [3, -1]

def next_greater(A1, A2):
    """
    Find the next greater element.

    Time complexity: O(n^2) because we iterate through remaining elements
                     for each element
    Space complexity: O(n) we store the results in a list of size n

    :param list[int] A1:
    :param list[int] A2:
    :returns: list[int]
    """
    result = []
    for i in A1:
        appended = False
        for j in A2[A2.index(i):]:
            if j > i:
                result.append(j)
                appended = True
                break
        if not appended:
            result.append(-1)

    return result

def next_greater2(A1, A2):
    """
    Find the next greater element.

    Time complexity: O(n) we iterate through A1 then through A2 sequentially
    Space complexity: O(n) we store the results in a list of size n

    :param list[int] A1:
    :param list[int] A2:
    :returns: list[int]
    """
    result = []
    stack = []
    next_greater = {}

    # we first store v the next greater element than k in a dictionary {k: v}
    # all elements are stored in a stack and we compare stack elements to
    # following elements to find the next greater, if found we pop
    for n in A2:
        while stack and stack[-1] < n:
            next_greater[stack.pop()] = n
        stack.append(n)

    # we build results by checking if we have found a greater element, if not
    # we add -1 to the results
    for n in A1:
        result.append(next_greater.get(n, -1))

    return result

def test1():
    A1 = [4, 1, 2]
    A2 = [1, 3, 4, 2]
    assert next_greater(A1, A2) == [-1, 3, -1]
    print("test 1 successful")

def test2():
    A1 = [4, 1, 2]
    A2 = [1, 3, 4, 2]
    assert next_greater2(A1, A2) == [-1, 3, -1]
    print("test 2 successful")

def test3():
    A1 = [2, 4]
    A2 = [1, 2, 3, 4]
    assert next_greater(A1, A2) == [3, -1]
    print("test 3 successful")

def test4():
    A1 = [2, 4]
    A2 = [1, 2, 3, 4]
    assert next_greater2(A1, A2) == [3, -1]
    print("test 4 successful")

def main():
    test1()
    test2()
    test3()
    test4()

if __name__ == "__main__":
    main()


import random
import numpy as np
import os
import time
from IPython.display import clear_output

class Sokoban:
  def __init__(self, grid_size=(6, 6), state_scale=2):
    self.size = grid_size
    self.state_scale = state_scale
    self.state_width = 1+ 2 * self.state_scale
    self.state_size = (self.state_width)**2
    self.action_size = 4
    self.codes = ['　', '▣', '■', 'О', 'Χ', '▨']
    self.matrix = [[0 for _ in range(self.size[0])] for i in range(self.size[1])]
    self.pos = [0, 0]
    self.box = [0, 0]
    self.goal = [0, 0]
    self.end = [0, 0]

    self.unavailable_reward = -1
    self.goal_reward = 1
    self.timestep_reward = -0.1

    self.max_timestep = 10000
    self.timestep = 0
    self.DELAY = 1

  def reset(self):
    self.matrix = [[0 for _ in range(self.size[0])] for i in range(self.size[1])]

    self.pos[0] = random.randrange(0, self.size[0])
    self.pos[1] = random.randrange(0, self.size[1])

    self.box[0] = self.randompick((1, self.size[0]-1), (self.pos[0],))
    self.box[1] = self.randompick((1, self.size[1]-1), (self.pos[1],))

    self.goal[0] = self.randompick((1, self.size[0]-1), (self.pos[0], self.box[0]))
    self.goal[1] = self.randompick((1, self.size[1]-1), (self.pos[1], self.box[1]))

    self.end[0] = self.randompick((1, self.size[0]-1), (self.pos[0], self.box[0], self.goal[0]))
    self.end[1] = self.randompick((1, self.size[1]-1), (self.pos[1], self.box[1], self.goal[1]))

    self.matrix[self.pos[1]][self.pos[0]] = 1
    self.matrix[self.box[1]][self.box[0]] = 2
    self.matrix[self.goal[1]][self.goal[0]] = 3
    self.matrix[self.end[1]][self.end[0]] = 4

    self.timestep = 0

    return self.get_state()

  def randompick(self, range, sub):
    done = False
    n = 0
    while not done:
      n = random.randrange(range[0], range[1])
      if not n in sub:
        done = True
    return n

  def get_state(self):
    state = [0] * (self.state_size)
    for i in range(0, self.state_width):
      if not self.pos[1] - self.state_scale + i in range(self.size[1]):
        for j in range(0, self.state_width):
          state[i * self.state_width + j] = 5
      else:
        for j in range(0, self.state_width):
          if not self.pos[0] - self.state_scale + j in range(self.size[0]):
            state[i * self.state_width + j] = 5
          else:
            state[i * self.state_width + j] = self.matrix[self.pos[1] - self.state_scale + i][self.pos[0] - self.state_scale + j]

    return state

  def step(self, action, show=False):
    self.timestep += 1
    if self.timestep > self.max_timestep:
      return self.get_state(), -1, True

    done = False
    next_state = None
    next_pos = [self.pos[0], self.pos[1]]
    next_box_pos = [self.box[0], self.box[1]]
    reward = 0

    def check(pos1, pos2):
      return pos1[0]==pos2[0] and pos1[1]==pos2[1]

    if action == 0:
      if self.pos[1] - 1 < 0:
        #reward += self.unavailable_reward
        pass
      else:
        next_pos = [self.pos[0], self.pos[1] - 1]
    elif action == 1:
      if self.pos[1] + 1 >= self.size[1]:
        #reward += self.unavailable_reward
        pass
      else:
        next_pos = [self.pos[0], self.pos[1] + 1]
    elif action == 2:
      if self.pos[0] - 1 < 0:
        #reward += self.unavailable_reward
        pass
      else:
        next_pos = [self.pos[0] - 1, self.pos[1]]
    elif action == 3:
      if self.pos[0] + 1 >= self.size[0]:
        #reward += self.unavailable_reward
        pass
      else:
        next_pos = [self.pos[0] + 1, self.pos[1]]

    if check(self.end, next_pos) or check(self.goal, next_pos):
      reward += self.unavailable_reward
      done = True
    elif check(self.box, next_pos): #박스와 충돌
      if action == 0:
        next_box_pos = [self.box[0], self.box[1] - 1]
      elif action == 1:
        next_box_pos = [self.box[0], self.box[1] + 1]
      elif action == 2:
        next_box_pos = [self.box[0] - 1, self.box[1]]
      elif action == 3:
        next_box_pos = [self.box[0] + 1, self.box[1]]

      if not next_box_pos[0] in range(0, self.size[0]) or not next_box_pos[1] in range(0, self.size[1]): #박스가 벽에 닿음
        reward += self.unavailable_reward
        done = True
        next_box_pos = [self.box[0], self.box[1]]
      elif check(next_box_pos, self.end):
        reward += self.unavailable_reward
        done = True
      elif check(next_box_pos, self.goal):
        reward += self.goal_reward
        done = True

    self.pos = next_pos
    self.box = next_box_pos
    self.matrix = [[0 for _ in range(self.size[0])] for i in range(self.size[1])]

    self.matrix[self.goal[1]][self.goal[0]] = 3
    self.matrix[self.end[1]][self.end[0]] = 4
    self.matrix[self.pos[1]][self.pos[0]] = 1
    self.matrix[self.box[1]][self.box[0]] = 2
    reward += self.timestep_reward
    if show:
      self.show(action, reward)
    return self.get_state(), reward, done

  def show(self, action, reward):
    os.system('cls')
    clear_output()
    state = self.get_state()
    print(f"action:{action}    reward:{reward}    step:{self.timestep}/{self.max_timestep}")
    print(f"┏{'━'*(self.size[0])}┓    ┏{'━'*(self.state_width)}┓")
    for i in range(self.size[1]):
      print(f"┃", end="")
      for a in self.matrix[i]:
        print(self.codes[a], end="")
      print(f"┃    ", end="")
      if i in range(self.state_width):
        print(f"┃", end="")
        for a in range(self.state_width):
          print(self.codes[state[i * self.state_width + a]], end="")
        print(f"┃")
      elif i == self.state_width:
        print(f"┗{'━'*(self.state_width)}┛")
      else:
        print()
    print(f"┗{'━'*(self.size[0])}┛")
    time.sleep(self.DELAY)


"""
https://leetcode-cn.com/problems/matrix-cells-in-distance-order/
给出 R 行 C 列的矩阵，其中的单元格的整数坐标为 (r, c)，满足 0 <= r < R 且 0 <= c < C。

另外，我们在该矩阵中给出了一个坐标为 (r0, c0) 的单元格。

返回矩阵中的所有单元格的坐标，并按到 (r0, c0) 的距离从最小到最大的顺序排，其中，两单元格(r1, c1) 和 (r2, c2) 之间的距离是曼哈顿距离，|r1 - r2| + |c1 - c2|。（你可以按任何满足此条件的顺序返回答案。）

示例 1：
    输入：R = 1, C = 2, r0 = 0, c0 = 0
    输出：[[0,0],[0,1]]
    解释：从 (r0, c0) 到其他单元格的距离为：[0,1]

示例 2：
    输入：R = 2, C = 2, r0 = 0, c0 = 1
    输出：[[0,1],[0,0],[1,1],[1,0]]
    解释：从 (r0, c0) 到其他单元格的距离为：[0,1,1,2]
    [[0,1],[1,1],[0,0],[1,0]] 也会被视作正确答案。

示例 3：
    输入：R = 2, C = 3, r0 = 1, c0 = 2
    输出：[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
    解释：从 (r0, c0) 到其他单元格的距离为：[0,1,1,2,2,3]
    其他满足题目要求的答案也会被视为正确，例如 [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]。

提示：
    1 <= R <= 100
    1 <= C <= 100
    0 <= r0 < R
    0 <= c0 < C

"""
class Solution:
    def allCellsDistOrder(self, R, C, r0, c0):
        dist_list = [[] for i in range(200)]
        for i in range(R):
            for j in range(C):
                distinct = abs(r0-i) + abs(c0-j)
                dist_list[distinct].append([i, j])
        result = []
        for i in dist_list:
            if i:
                result.extend(i)
            else:
                break
        return result

if __name__ == "__main__":
    R = 2
    C = 3
    r0 = 1
    c0 = 2
    sol = Solution()
    result = sol.allCellsDistOrder(R, C, r0, c0)
    print(result)

1030_距离顺序排列矩阵单元格.py


'''A program should read “n” positive integers from the user and
append it to the list, and if the list contains prime numbers return “True” and
number of prime numbers, else “False”.'''
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

n = int(input("Enter the number of integers: "))

num_list = [int(input("Enter number ")) for i in range(n)]

prime_numbers = [num for num in num_list if is_prime(num)]

if prime_numbers:
    print("True")
    print("Number of prime numbers:", len(prime_numbers))
else:
    print("False")


#エスケープ
test = r'sSay hi to bob\'s mother'
print(test)

#raw文字列   文字列の中のエスケープ文字を無視してバックスラッシュをバックスラッシュとして扱う

#３連クゥートによる複数行文字列　\nというエスケープ文字を使わなくてもよくなる
print("""pfdsa
      fdsafas
      fdas""")

#startswith(),endswith()
print("hello".endswith("lo"))

#join　リストを指定した文字で繋げる
# split 文字列を分割する。改行したい時なんかに使える
"""
>>> "".join(["sss","bb"])
'sssbb'
>>> ",".join(["sss","bb"])
'sss,bb'
"""

#rjust,ljust(),center()  表形式のデータを正しくスペースを開けて表示したい時に便利
a = 'Hello'.rjust(10)
print(a)
b = 'Hello'.rjust(10, "+")
print(b)

#strip()　文字列の冒頭と末尾の空白文字を除去した新しい文字列を返す.引数を渡すと、渡した文字列を両端から削除する
#rstrip() lstrip() 冒頭もしくは末尾の空白を除去して返すメソッド
spam = '     table   '
spam.strip()
spam.lstrip()

#pyperclip　クリップボードを操作する
import pyperclip
pyperclip.copy('hello world')
pyperclip.paste()

文字列操作/文字列操作.py


#
# PySNMP MIB module ACCEDIAN-SMI (http://snmplabs.com/pysmi)
# ASN.1 source file:///Users/davwang4/Dev/mibs.snmplabs.com/asn1/ACCEDIAN-SMI
# Produced by pysmi-0.3.4 at Mon Apr 29 16:57:24 2019
# On host DAVWANG4-M-1475 platform Darwin version 18.5.0 by user davwang4
# Using Python version 3.7.3 (default, Mar 27 2019, 09:23:15)
#
OctetString, ObjectIdentifier, Integer = mibBuilder.importSymbols("ASN1", "OctetString", "ObjectIdentifier", "Integer")
NamedValues, = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues")
ConstraintsUnion, ValueRangeConstraint, ConstraintsIntersection, ValueSizeConstraint, SingleValueConstraint = mibBuilder.importSymbols("ASN1-REFINEMENT", "ConstraintsUnion", "ValueRangeConstraint", "ConstraintsIntersection", "ValueSizeConstraint", "SingleValueConstraint")
NotificationGroup, ModuleCompliance = mibBuilder.importSymbols("SNMPv2-CONF", "NotificationGroup", "ModuleCompliance")
NotificationType, Bits, Counter32, MibScalar, MibTable, MibTableRow, MibTableColumn, ModuleIdentity, Gauge32, IpAddress, ObjectIdentity, MibIdentifier, Integer32, TimeTicks, Counter64, Unsigned32, enterprises, iso = mibBuilder.importSymbols("SNMPv2-SMI", "NotificationType", "Bits", "Counter32", "MibScalar", "MibTable", "MibTableRow", "MibTableColumn", "ModuleIdentity", "Gauge32", "IpAddress", "ObjectIdentity", "MibIdentifier", "Integer32", "TimeTicks", "Counter64", "Unsigned32", "enterprises", "iso")
TextualConvention, DisplayString = mibBuilder.importSymbols("SNMPv2-TC", "TextualConvention", "DisplayString")
accedianMIB = ModuleIdentity((1, 3, 6, 1, 4, 1, 22420))
accedianMIB.setRevisions(('2006-08-06 01:00',))
if mibBuilder.loadTexts: accedianMIB.setLastUpdated('200608060100Z')
if mibBuilder.loadTexts: accedianMIB.setOrganization('Accedian Networks, Inc.')
acdProducts = ObjectIdentity((1, 3, 6, 1, 4, 1, 22420, 1))
if mibBuilder.loadTexts: acdProducts.setStatus('current')
acdMibs = ObjectIdentity((1, 3, 6, 1, 4, 1, 22420, 2))
if mibBuilder.loadTexts: acdMibs.setStatus('current')
acdTraps = ObjectIdentity((1, 3, 6, 1, 4, 1, 22420, 3))
if mibBuilder.loadTexts: acdTraps.setStatus('current')
acdExperiment = MibIdentifier((1, 3, 6, 1, 4, 1, 22420, 4))
acdServices = ObjectIdentity((1, 3, 6, 1, 4, 1, 22420, 5))
if mibBuilder.loadTexts: acdServices.setStatus('current')
mibBuilder.exportSymbols("ACCEDIAN-SMI", acdTraps=acdTraps, acdServices=acdServices, PYSNMP_MODULE_ID=accedianMIB, accedianMIB=accedianMIB, acdMibs=acdMibs, acdExperiment=acdExperiment, acdProducts=acdProducts)


"""
Программа: Тренировка данных
Версия: 1.0
"""

import optuna
from lightgbm import LGBMRegressor

from optuna import Study

from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import pandas as pd
import numpy as np
from ..data.split_dataset import get_train_test_data
from ..train.metrics import save_metrics

def objective(
    trial,
    data_x: pd.DataFrame,
    data_y: pd.Series,
    n_folds: int = 5,
    random_state: int = 10
) -> np.array:
    """
    Целевая функция для поиска параметров
    :param trial: кол-во trials
    :param data_x: данные объект-признаки
    :param data_y: данные с целевой переменной
    :param n_folds: кол-во фолдов
    :param random_state: random_state
    :return: среднее значение метрики по фолдам
    """
    lgb_params = {
        "n_estimators": trial.suggest_categorical("n_estimators", [348]),
        # "n_estimators": trial.suggest_int("n_estimators", 100, 1000, log=True),
        "learning_rate": trial.suggest_categorical("learning_rate", [0.025261718741189964]),
        # "learning_rate": trial.suggest_float("learning_rate", 0.001, 0.3, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 20, 500, step=10),
        "max_depth": trial.suggest_int("max_depth", 6, 10),
        "max_bin": trial.suggest_int("max_bin", 50, 300),
        "min_child_samples": trial.suggest_int("min_child_samples", 10, 100, step=3),
        'reg_alpha': trial.suggest_loguniform('reg_alpha', 1e-3, 20.0),
        'reg_lambda': trial.suggest_loguniform('reg_lambda', 1e-3, 10.0),
        "subsample": trial.suggest_float("subsample", 0.1, 1.0),
        "colsample_bytree": trial.suggest_float("colsample_bytree", 0.1, 1.0),
        "random_state": trial.suggest_categorical("random_state", [random_state])}

    cv = KFold(n_splits=n_folds, shuffle=True)

    cv_predicts = np.empty(n_folds)
    for idx, (train_idx, test_idx) in enumerate(cv.split(data_x, data_y)):
        X_train, X_test = data_x.iloc[train_idx], data_x.iloc[test_idx]
        y_train, y_test = data_y.iloc[train_idx], data_y.iloc[test_idx]

        pruning_callback = optuna.integration.LightGBMPruningCallback(
            trial, "l2")
        model = LGBMRegressor(**lgb_params)
        model.fit(X_train,
                  y_train,
                  eval_set=[(X_test, y_test)],
                  eval_metric='rmse',
                  early_stopping_rounds=100,
                  callbacks=[pruning_callback],
                  verbose=0)

        preds = model.predict(X_test)
        cv_predicts[idx] = np.sqrt(mean_squared_error(y_test, preds))

    return np.mean(cv_predicts)

def find_optimal_params(
    data_train: pd.DataFrame, data_test: pd.DataFrame, **kwargs
) -> Study:
    """
    Пайплайн для тренировки модели
    :param data_train: датасет train
    :param data_test: датасет test
    :return: [LGBMClassifier tuning, Study]
    """
    x_train, x_test, y_train, y_test = get_train_test_data(
        data_train=data_train, data_test=data_test, target=kwargs["target_column"]
    )

    study = optuna.create_study(direction="minimize", study_name="LGB")
    function = lambda trial: objective(
        trial, x_train, y_train, kwargs["n_folds"], kwargs["random_state"]
    )
    study.optimize(function, n_trials=kwargs["n_trials"], show_progress_bar=True)
    return study

def train_model(
    data_train: pd.DataFrame,
    data_test: pd.DataFrame,
    study: Study,
    target: str,
    metric_path: str,
) -> LGBMRegressor:
    """
    Обучение модели на лучших параметрах
    :param data_train: тренировочный датасет
    :param data_test: тестовый датасет
    :param study: study optuna
    :param target: название целевой переменной
    :param metric_path: путь до папки с метриками
    :return: LGBMRegressor
    """
    # get data
    x_train, x_test, y_train, y_test = get_train_test_data(
        data_train=data_train, data_test=data_test, target=target
    )

    # training optimal params
    model = LGBMRegressor(**study.best_params, silent=True, verbose=-1)
    model.fit(x_train, y_train, verbose=-1)

    # save metrics
    save_metrics(data_x=x_test, data_y=y_test, model=model, metric_path=metric_path)
    return model


from gerador import pontof, palpt, numCPU, vez

import os
import time

if palpt == numCPU:
    print("\n\033[0;36m  Uuuuhhhhh.... Você acertou o número ",numCPU," e foram ",vez,"vezes.")
    print("  E a tua Pontuação do Jogo foi: ",pontof,"pontos.")
    if pontof >= 50:
        print("  A tua Pontuação do Jogo foi Muito Excelente!!!!! Parabéns...\033[m\n")
    else:
        print("  A tua Pontuação do Jogo foi Bom!!!!! Parabéns...\033[m\n")
else:
    print("\n  Não foi desta vez.... O Número Secreto era",numCPU,".\n")
    print("  E a tua Pontuação do Jogo foi:",pontof,"pontos.\033[m\n")
    if pontof >= 5:
        print("  A tua Pontuação do Jogo foi Razoável...\033[m\n")
    else:
        print("  A tua Pontuação do Jogo foi muito Baixo...\033[m\n")
resp = input("\033[0;33m  Deseja Jogar novamente[S/N]?\033[m]").upper()
if resp == 'S':
    #return comeco()    inicial.py   ## Não consegui voltar no começo...
    exit()
else:
    print("\n\033[1;36m         Feito por Sérgio Renato Steglich - SRSistemas\033[m\n")
    print("\n\033[1;31m   Encerrado, até próxima!!!\033[m\n")
    time.sleep(3)
    os.system('cls' if os.name == 'nt' else 'clear')
    exit()


import time
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from selenium import webdriver
driver = webdriver.Chrome("/home/mj/Documents/scrape/selscrape/chromedriver")

pages=set()
extract={}
baseurl="https://www.quora.com"

flag=1
def get_topic_list(pagenum,word):
    n=len(pagenum)
    temp=0
    i=0
    for i in range(0,n):

        linktoclick=pagenum[i]
        html=urlopen(linktoclick)
        bsobj=BeautifulSoup(html)
        maindiv=bsobj.find("div",{"class":"ContentWrapper"})
        ls=maindiv.find("a",href=re.compile("(/topic/)"))
        if word.lower()>ls.get_text().lower():
        	temp=i

        elif word.lower()<ls.get_text().lower() and word.lower() not in ls.get_text().lower():
        	flag=-1
        	break

    if i==temp:
        try:
          linktoclick=pagenum[i]
          html=urlopen(linktoclick)
          bsobj=BeautifulSoup(html)
          maindiv=bsobj.find("div",{"class":"ContentWrapper"})
          ls=maindiv.findAll("a",href=re.compile("(/topic/)"))
          if word.lower()>ls[0].get_text().lower() and word.lower()<=ls[len(ls)-1].get_text().lower() :
             for k in ls :
                m= re.search( r'topic/(.*)',k.attrs["href"], re.M|re.I)
                m=m.group(1)
                extract[k.get_text().lower()]=m
        except:
          pass

    else:
	    for j in range(temp,i):
	        linktoclick=pagenum[j]
	        html=urlopen(linktoclick)
	        bsobj=BeautifulSoup(html)
	        maindiv=bsobj.find("div",{"class":"ContentWrapper"})
	        ls=maindiv.findAll("a",href=re.compile("(/topic/)"))
	        for k in ls :
	            m= re.search( r'topic/(.*)',k.attrs["href"], re.M|re.I)
	            m=m.group(1)
	            extract[k.get_text().lower()]=m

def begin(pageUrl,word):
    pagenum=[]
    linktoclick=baseurl+pageUrl+word[:2].lower()
    pagenum.append(linktoclick)

    while 1:
        html=urlopen(linktoclick)
        bsobj=BeautifulSoup(html)
        maindiv=bsobj.find("div",{"class":"ContentWrapper"})
        pages_head=maindiv.find("h2")
        cur=pages_head.find("strong").get_text()
        pagen=pages_head.findAll("a")

        for i in pagen:

                if i.get_text()!="Next" and i.get_text()!="Previous" and int(i.get_text())>int(cur) :
                    pagenum.append(baseurl+i.attrs["href"])
        get_topic_list(pagenum,word)

        if (pagen[len(pagen)-1].get_text()!="Next" or flag==-1):
        	return extract

        linktoclick=pagenum[len(pagenum)-1]
        driver.get(linktoclick)
        pagenum=[]

def binary_search(word,ls):
    l=0;
    r=len(ls)-1
    mid=l+(r-l)//2
    while l<=r:
        if ls[mid].lower()==word:
            return mid
        elif ls[mid].lower()>word:
            r=mid-1
        else:
            l=mid+1
        mid=l+(r-l)//2
    return -1

# ,href=re.compile("(/{wo}/)".format(wo=word))
ans={}
ansl=[]

def getLinks(pageUrl,word):
    k=0

    global  pages
    html=urlopen(pageUrl)
    bsObj=BeautifulSoup(html)
    for link in bsObj.findAll("a",{"class":"question_link"}):
        if 'href' in link.attrs:
            if link.attrs['href'] not in  pages:

                newPage=link.attrs['href']
                pages.add(newPage)
                ansl.append(newPage)
    # for link in bsObj.findAll("span",{"class":"count"}):

    # 	ans[ansl[k]]=link.get_text()
    # 	k=k+1

def go_to_topic(word):
	word=word.lower()
	di=begin("/sitemap/alphabetical_topics/",word)
	ls=sorted(di.keys())

	ind=binary_search(word,ls)
	if ind!=-1:
		topic_url=di[ls[ind]]
		linktoclick="https://www.quora.com/topic/{t}".format(t=topic_url)
		driver.get(linktoclick)
		getLinks(linktoclick,word)
		# for i in ans.keys():
		# 	print(i,"--->",ans[i],"\n\n")
		for i in ansl:
			print(baseurl+i,"\n")

	else:
		print("Not found!!\n\nSuggested Searches:\n\n")
		for i in ls:
			print(i,"\n")

go_to_topic("data")

# # driver function
# def main():

#     # Input keys (use only 'a' through 'z' and lower case)
#     keys = ["the","a","there","anaswe","any",
#             "by","their"]
#     output = ["Not present in trie",
#               "Present in tire"]

#     # Trie object
#     t = Trie()

#     # Construct trie
#     for key in keys:
#         t.insert(key)

#     # Search for different keys
#     print("{} ---- {}".format("the",output[t.search("the")]))
#     print("{} ---- {}".format("these",output[t.search("these")]))
#     print("{} ---- {}".format("their",output[t.search("their")]))
#     print("{} ---- {}".format("thaw",output[t.search("thaw")]))


store={
    'HP': 20,
    'DELL': 50,
    'MACBOOK': 12,
    'ASUS': 30,
    'ACER': 25,
}
prize={
    'HP': 600,
    'DELL': 650,
    'MACBOOK': 12000,
    'ASUS': 400,
    'ACER': 350,
}
m= 0
for i in store.keys():
    for k in range(1):
        n= store[i]*prize[i]
        print('Tổng giá của ', i, 'là: ', n)
        m += n
print('Tổng giá trị toàn bộ các máy trong kho: ',m)


# coding= utf-8
from multiprocessing import Process

class SunProcess(Process):
    def __init__(self, name):
        super().__init__()
        self.name = name
    def run(self):
        print("1234")

if __name__ == '__main__':
    p = SunProcess("sun")
    p.start()
    p.join()