# get FASTQ files
# import pickle
import glob
import os

import numpy as np
import pandas as pd

# part 3: Get the fastq files for the specified project ID, verify technology
# downloading the fastq files is the significant bottleneck of the whole process...

### Config ####

species = "mouse"   # mouse or human for now

getAll = False          # Boolean
getRecent = True        # Boolean - ignored if getAll == True
lastNdays = 5           # only used if getRecent == True
nCore = 30              # max cores allows for this project accession
suppDirec = "/home/johlee/SCQC/SupplementData"    # supplemental data directory.
metaDirec = "/home/johlee/SCQC/MetaData"    # MetaData Directory - should contain <run>_MetaData_TP.tsv
fastqStore="/home/johlee/SCQC/FASTQ"              # Temporarily store fastq files here
STARout = "/home/johlee/SCQC/STARout"             # Temporily store star outputs

project_acc = "SRP258272"
# project_acc = 'SRP300819'       # 10x example
# project_acc = 'SRP288492'       # SS example
# project_acc = 'ERP122986'       # SS example that breaks
        # - failed to resolve accession 'ERR4354523' - no data ( 404 )
        # 2021-05-19T19:42:11 fasterq-dump.2.11.0 err: invalid accession 'ERR4354523'
        # fasterq-dump quit with error code 3

### End Config ###

# open the project specific dataframe, and unroll lists.
def loadProjectData(project_acc,metaDirec = "MetaData" ) :
    dfpath = metaDirec + "/" + project_acc +"_MetaData.tsv"

    df = pd.read_csv(dfpath, sep= "\t" )
    #### There's probably a better way to do this #####
    # unwrap the Runs, species, taxon
    dfout = pd.DataFrame()
    for  i in range(df.shape[0]) :
        runs = df.Runs.loc[i].replace("['","").replace("']", "")
        runs = runs.split("', '")

        organism = df.Organism.loc[i].replace("['","").replace("']", "")
        organism = organism.split("', '")

        taxid = df.Taxon_ID.loc[i].replace("['","").replace("']", "")
        taxid = taxid.split("', '")

        pdat = df.Date.loc[i].replace("['","").replace("']", "")
        pdat = pdat.split("', '")

        tmpdf = pd.DataFrame( )
        tmpdf['Run'] = runs
        tmpdf["Submission"] = df.Submission.loc[i]
        tmpdf["Project"] = df.Project.loc[i]
        tmpdf['Experiment'] = df.Experiment.loc[i]
        tmpdf["Organism"] = organism
        tmpdf["Taxon_ID"] = taxid
        tmpdf["Date"] = pdat
        tmpdf["Method"] = df.Method.loc[i]

        dfout = pd.concat([dfout , tmpdf],ignore_index=True)

    return (dfout)

# given 10x runs, figure out what version of 10x was used by length of spots
def getReadFilesIn_10x(run , project_acc, fastqstoredir) :

    # look at the first spot -
    # can edit to avoid writing to disk.
    tmpFile = fastqstoredir+"/"+run+"__temp__.tsv"
    os.system("fastq-dump -X 1 -Z --split-spot "+ run +" > " + tmpFile)
    with( open(tmpFile ,'r') ) as spot :
        dat = spot.readlines()
        N = len(dat) //4    # number of fastq files that will be pulled.

    os.remove(tmpFile)

    lengths = {}
    it=1
    for line in dat[0::4] : # look at every 4 lines for the length of the read
        lengths[run +"_"+str(it) +".fastq"] = line.split("length=")[-1].replace("\n","")
        it += 1

    # which read is the longest?
    l =  [  int(l) for l in lengths.values()  ]
    ind = l.index(max(l))
    read_bio = list(lengths.keys())[ind]

    # which read contains the barcode + UMI?
    # l =  list(lengths.values() )
    tech = "unknown"
    for i in range(len(l)) :
        n = int(l[i])

        if n == 24 :
            tech = "10xv1"
            ind2=i
        elif n == 26 :
            ind2=i
            tech = "10xv2"
        elif n == 28 :
            ind2=i
            tech = "10xv3"

    # is tech still unknown? Skip it.
    if tech =="unknown" :  # doesn't match.. guess the tech by length of cb+umi
        print("... Length of UMI+CB does not match the standard for 10x v1, v2, or v3 for run: "+run+".")
        return (None, None)

        # print("... Run: "  + run +" is not 10x - unable to get technology.")
    else : # tech was found to be a specific 10x version
        read_tech = list(lengths.keys())[ind2]
        readsIn = fastqstoredir+"/"+project_acc+"/"+read_bio + " " +fastqstoredir+"/"+project_acc+"/"+ read_tech

    if len(set(lengths.values())) < 2 : # lengths of reads are identical, unable to distinguish cDNA / CB+UMI
        print("... Lengths of all reads are identical. Unable to distinguish cDNA from CB+UMI for run: "+run+".")
        return (None, None)

    return(readsIn , tech)

# given the 10x df, download the fastq files. Create new file to indicate fastqs are all downloaded.
def getFASTQfiles_10x( df ,fastqstoredir="FASTQ" ,nCore = 30,prefetch =True) :

    # 3210.6243612766266 False - but didn't download any fastq...
    # 5150.785088777542 True - downloaded properly
    # 8779.405247211456 False

    # ti = time.time()
    try :   # make the directory if not already there.
        os.makedirs(fastqstoredir)
    except OSError as err:
        pass

    # parallelize this. partition nCores / df.shape[0] cores for each run
    # nCoreDump =nCore // df.shape[0]
    nCoreDump = nCore    # temp - pre-parallelized
    AllReadsIn = []
    allTechs= []

    for i in range(df.shape[0]) :
        SRR = df.Run.loc[i]
        SRP = df.Project.loc[i]
        fastqdirec = fastqstoredir+"/"+SRP

        readsIn, tech  = getReadFilesIn_10x(run=SRR,project_acc = SRP,fastqstoredir=fastqstoredir)
        AllReadsIn.append(readsIn)
        allTechs.append(tech)

        isThere = [ os.path.exists(f) for f in readsIn.split(" ")]

        if readsIn != None  and not any (isThere) :
            if prefetch :
                fastqprefix = fastqdirec +"/"+SRR
                print( "... Prefetch SRA file for ",SRR)
                os.system("prefetch --max-size 30000000 -O "+fastqdirec+ " "+ SRR  )

                print("... Dumping FASTQ files for "+ SRR)
                os.system("fasterq-dump --split-files --include-technical --threads "+ str(nCoreDump) + " -O "+fastqdirec+ " "+ fastqprefix +".sra" )
            else :
                print("... Dumping FASTQ files for "+ SRR)
                os.system("fasterq-dump --split-files --include-technical --threads "+ str(nCoreDump) + " -O "+fastqdirec+ " "+ SRR )

        # clean up sra files.
        files = glob.glob(fastqdirec +'/*.sra')

        for f in files:
            try:
                os.remove(f)
            except OSError as e:
                print("Error: %s : %s" % (f, e.strerror))

    # tf = time.time()
    # print(tf - ti , prefetch)
    return (AllReadsIn, allTechs)

def buildSmartSeqManifest(project_acc="SRP288492" ,fastqdir="FASTQ" ,metaDirec ="MetaData") :

    fastqdir = fastqdir + "/" + project_acc
    # list of all fastq files found in the directory
    fastq1files = glob.glob(fastqdir + "/*_1.fastq" )
    fastq2files = glob.glob(fastqdir + "/*_2.fastq" )
    fastqfiles = glob.glob(fastqdir + "/*.fastq" )

    # get the files that dont have a _1 or _2
    fastqfiles = np.setdiff1d(np.setdiff1d(fastqfiles,fastq2files),fastq1files)
    runs = [file.split("_")[0].replace(".fastq","").replace(fastqdir+"/","") for file in fastqfiles ]

    df = pd.DataFrame({"read1":fastqfiles , "read2" :"-", "cellID" : runs} )

    if fastq1files != None  and  fastq2files != None  :
        # contains paired reads.
        pairedruns = [file.split("_")[0].replace(".fastq","").replace(fastqdir+"/","") for file in fastq1files ]
        df2 = pd.DataFrame({"read1":fastq1files , "read2" :fastq2files, "cellID" : pairedruns} )
        df = pd.concat([df,df2])

        fname = metaDirec + "/"+project_acc+"_SS_manifest.tsv"
        df.to_csv( fname, sep="\t", index=False, header=False)
    return (df)

# given the SS df, download the fastq files. Create new file to indicate fastqs are all downloaded.
def getReadFilesIn_SS(df, fastqstoredir="FASTQ",metaDirec = "MetaData",nCore = 30) :
    # download the first spot of all reads.

    try :   # make the directory if not already there.
        os.makedirs(fastqstoredir)
    except OSError as err:
        pass

    runList = df.Run.values

    # this will be unique
    project_acc = df.Project.values[0]

    fastqprefix = "/".join([fastqstoredir , project_acc] )

    # batch this
    start = 0
    batchSize = 100
    n = len(runList)
    while start < n :
        print(start)
        endpoint = min(start+batchSize, n)
        rl = " ".join(runList[start:endpoint] )
        rl2 = [fastqstoredir+"/"+ s+".sra" for s in runList[start:endpoint] ]

        # # this runs sequentially. Is this necessary? Significantly faster with this
        os.system("prefetch --max-size 30000000 -O "+fastqprefix + " "+ rl  )

        # only get the SRA files that downloaded properly
        sraFiles = glob.glob(fastqprefix+"/*.sra" )
        sraFiles =  list(set(sraFiles)& set(rl2) )
        sraFiles =" ".join(sraFiles)

        # which runs don't have a fastq?
        # fastqFiles =  glob.glob(fastqstoredir+"/*fastq" )
        os.system("fasterq-dump --split-files --include-technical --threads "+ str(nCore) + " -O "+fastqprefix+  " "+ rl )

        start = min(start + batchSize , n)
        # clean up the sra files.
        # os.remove(fastqstoredir + "*.sra")

        # clean up sra files.
        files = glob.glob(fastqprefix +'/*.sra')

        for f in files:
            try:
                os.remove(f)
            except OSError as e:
                print("Error: %s : %s" % (f, e.strerror))

    # project is done - build the manifest
    df = buildSmartSeqManifest(project_acc ,fastqstoredir ,metaDirec)
    return(df )

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

# this just downloads the FASTQ files
def main( project_acc ="SRP288492", species ="mouse",metaDirec ="MetaData",fastqstoredir ="FASTQ", nCore=30) :

    df = loadProjectData(project_acc, metaDirec )

    # split the data into 10x portion and SS portion.
    dfByMethod ={ x:y for x,y in  df.groupby("Method") }

    key = list(dfByMethod.keys())
    if "isSome10x" in key :
        df10x = dfByMethod["isSome10x"]
        AllReadsIn, allTechs = getFASTQfiles_10x(df10x, fastqstoredir,nCore ,True)
        # AllReadsIn2, allTechs2 = getFASTQfiles_10x(df10x, fastqstoredir ,nCore ,True)
        # Store these to a file indicating the fastq files for the project are all downloaded.

        #which runs failed?
        # df10x.loc[ np.where(allTechs == None) ,:]
        filename = metaDirec + "/" + project_acc + "_10x_MetaData.tsv"
        df10x["readsIn"]= AllReadsIn
        df10x["Method"] =  allTechs
        df10x.to_csv(filename , sep ="\t" ,index=False)

        # Separately run star for all of these readsIn

    if "isSS" in key :
        dfSS = dfByMethod["isSS"]
        manifestPath =getReadFilesIn_SS(dfSS, fastqstoredir ,nCore)

        filename = metaDirec + "/Projects/" + species + "/" + project_acc + "_SS_MetaData.tsv"
        # df10x["readsIn"]= manifestPath
        dfSS["Method"] =  "SMART-Seq"

        dfSS.to_csv(filename , sep ="\t" ,index=False)
        maniname = metaDirec + "/Projects/" + species + "/" + project_acc + "_SS_STAR_manifest.tsv"
        manifestPath.to_csv(maniname,  sep ="\t" ,index=False)

main( project_acc , species ,metaDirec ,fastqStore , nCore)


def solution(array, n):
    distance = list()
    array.sort()

    for i in range(len(array)):
        distance.append(abs(n-array[i]))

    answer = distance.index(min(distance))
    answer = array[answer]

    return answer

프로그래머스/lv0/120890. 가까운 수/가까운 수.py


import time
import random
import multiprocessing as mp
from pipert2 import FPSRoutine, Data, STOP_EVENT_NAME
from pipert2.utils.annotations import class_functions_dictionary

class MiddleBufferingRoutine(FPSRoutine):
    events = class_functions_dictionary()

    def __init__(self, buffer, limit, name, sleeping_time=0):
        super().__init__(name)
        self.custom_event_notifies = mp.Event()
        self.middle_event_notifies = mp.Event()
        self.buffer = buffer
        self.limit = limit
        self.index = 0
        self.sleeping_time = sleeping_time
        self.event_param = mp.Value('i', 0)

    def main_logic(self, data: Data = None) -> Data:
        if self.sleeping_time > 0:
            time.sleep(self.sleeping_time)

        data.additional_data["val"] += self.buffer
        self.index += 1

        if self.index == self.limit:
            self.notify_event(STOP_EVENT_NAME, specific_routine=self.name)

        time.sleep(random.randint(1, 10) / 10)
        return data

    @events("CUSTOM_EVENT")
    def custom_event(self):
        self.custom_event_notifies.set()

    @events("MIDDLE_EVENT")
    def middle_event(self):
        self.middle_event_notifies.set()

    @events("CUSTOM_EVENT_PARAM")
    def custom_event_with_param(self, param):
        self.custom_event_notifies.set()
        self.event_param.value = param


#!/usr/bin/env python3

"""Tools to compare new molecules to the chemical space in the NP Atlas DB"""

import pandas as pd
from pathlib import Path

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit import DataStructs

def generate_fingerprint(smiles):
    """Calculate molecular fingerprint using Morgan fingerprint radius 2"""

    mol = Chem.MolFromSmiles(smiles)
    return AllChem.GetMorganFingerprint(mol, 2)

def calculate_similarity(atlas_fp, input_fp):
    """Calculate similarity score between two rdkit fingerprints using Dice similarity scoring"""

    return round(DataStructs.DiceSimilarity(atlas_fp, input_fp), 2)

if __name__ == "__main__":

    atlas_tsv_path = Path("/Users/roger/Git/rgl_utilities/data/NP_Atlas/NPAtlas_2022_09.tsv")
    new_compound_name = "Abyssomicin 2"
    new_compound_smiles = "C[C@H]1CCC(=O)C2=C3O[C@@]4(C)[C@@H](C)C[C@]3(OC2=O)[C@@H](/C=C/C1=O)[C@@H]4O"
    new_compound_fingerprint = generate_fingerprint(new_compound_smiles)

    input_df = pd.read_csv(atlas_tsv_path, delimiter='\t')
    input_df["molecule_fingerprint"] = input_df["compound_smiles"].map(generate_fingerprint)
    input_df["Dice_similarity_score"] = input_df["molecule_fingerprint"].apply(calculate_similarity,
                                                                               args=(new_compound_fingerprint, ))
    input_df = input_df.drop(["molecule_fingerprint"], axis=1)

    output_path = Path(atlas_tsv_path.parent, new_compound_name + "_atlas_structure_similarity.tsv")
    input_df.to_csv(output_path, index=False, sep='\t', line_terminator='\n')


import json
import urllib2

from django.shortcuts import render
from django.http.response import JsonResponse, HttpResponseForbidden, HttpResponse
from django.http.request import HttpRequest

from models import *

def require_login(func):
    @json_response
    def dec(request, *args, **kwargs):
        uuid = request.META.get('HTTP_UUID', '')
        if not uuid:
            return HttpResponseForbidden()

        user = User.create_or_get(uuid=uuid)
        if not user:
            return HttpResponseForbidden()
        request.current_user = user
        return func(request, *args, **kwargs)

    return dec

def json_response(func):
    def dec(request, *args, **kwargs):
        res = func(request, *args, **kwargs)
        if isinstance(res, HttpResponse):
            return res

        res = JsonResponse(res, safe=False)
        return res
    return dec

@json_response
def wx_login(request):
    code = request.GET.get('code')

    # app_id = ''
    # secret = ''
    # wx_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code' % (
    #     app_id,
    #     secret,
    #     code,
    # )
    #
    # rep = urllib2.urlopen(wx_url)
    # data = json.loads(rep.read())
    # openid = data['openid']
    #
    openid = code
    user = User.create_or_get(openid)

    return {
        'uuid': user.uuid.hex,
    }

@require_login
def post_topic(request):
    data = json.loads(request.body)

    topic = Topic()
    topic.creator = request.current_user
    topic.content = data['content']

    if data['content']:
        topic.save()

    return {
        'rc': 0
    }

@require_login
def fetch_topics(request):
    order = request.GET.get('order', 'new')

    raw_topics = Topic.objects.all().order_by('-create_time')
    topics = []
    for topic in raw_topics:
        comments = Comment.all_comments(topic, request.current_user)
        topics.append({
            'id': topic.pk,
            'content': topic.content,
            'time': topic.create_time,
            'vote': {
                'number': VoteOnTopic.vote_of_topic(topic),
                'status': VoteOnTopic.vote_status(request.current_user, topic)
            },
            'comments': comments
        })

    return {
        'data': topics
    }

@require_login
def vote_topic(request):
    topic_id = request.GET.get('topic_id')
    topic = Topic.get_by_id(topic_id)
    vote_type = request.GET.get('type', '1')

    if not topic:
        return False

    vote = VoteOnTopic.create_or_get(request.current_user, topic)
    vote.vote_type = int(vote_type)

    vote.save()

    return True

@require_login
def post_comment_on_topic(request):
    topic_id = request.GET.get('topic_id')
    data = json.loads(request.body)
    topic = Topic.get_by_id(topic_id)

    if not topic:
        return False

    comment = Comment()
    comment.creator = request.current_user
    comment.content = data['content']
    comment.topic = topic

    comment.save()

    return True

def post_comment_on_comment(request):
    comment_id = request.GET.get('comment_id')
    reply_id = request.GET.get('reply_id', '-1')
    comment = Topic.get_by_id(comment_id)
    data = json.loads(request.body)

    if not comment:
        return False

    comment.reply(request.current_user, data['content'], reply_id)

    return True

@require_login
def vote_comment(request):
    comment_id = request.GET.get('comment_id')
    comment = Comment.get_by_id(comment_id)
    vote_type = request.GET.get('type', '1')

    if not comment:
        return False

    vote = VoteOnComment.create_or_get(request.current_user, comment)
    vote.vote_type = int(vote_type)

    vote.save()

    return True


#!/usr/bin/env python
import sys
import getopt
import struct
import time
import socket
import random
import json

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from pyrip_lib import *

'''
    Provides methods to handle RIP routes
'''
class IRoute(object):
    def __init__(self, prefix=0, prefixLen=0, nextHop=0, afi=0):
        self.afi = afi
        self.prefix = prefix
        self.prefixLen = prefixLen
        self.nextHop = nextHop

    def __cmp__(self, other):
        if self.afi < other.afi:
            return -1
        elif self.afi > other.afi:
            return 1

        if self.prefix < other.prefix:
            return -1
        elif self.prefix > other.prefix:
            return 1

        if self.prefixLen < other.prefixLen:
            return -1
        elif self.prefixLen > other.prefixLen:
            return 1

        if self.nextHop < other.nextHop:
            return -1
        elif self.nextHop > other.nextHop:
            return 1

        return 0

    def __lt__(self, other):
        return self.__cmp__(other)<0
    def __le__(self, other):
        return self.__cmp__(other)<=0
    def __eq__(self, other):
        return self.__cmp__(other)==0
    def __ne__(self, other):
        return self.__cmp__(other)!=0
    def __gt__(self, other):
        return self.__cmp__(other)>0
    def __ge__(self, other):
        return self.__cmp__(other)>=0

class RipRoute(IRoute):
    def __init__(self, prefix, prefixLen, nextHop, metric=RIP_METRIC_MIN, routeTag=0, family=RIP_ADDRESS_FAMILY):
        self.family = family
        self.routeTag = routeTag
        self.prefix = prefix
        self.prefixLen = prefixLen
        self.nextHop = nextHop
        self.afi = 1 # IPv4

        if metric >= RIP_METRIC_INFINITY:
            self.metric = RIP_METRIC_INFINITY
        elif metric <= RIP_METRIC_MIN:
            self.metric = RIP_METRIC_MIN
        else:
            self.metric = metric

    def pack(self):
        mask = PrefixLen2MaskInt(self.prefixLen)
        return struct.pack(RIP_ENTRY_PACK_FORMAT,
            self.family, self.routeTag, self.prefix, mask, self.nextHop, self.metric)

    def __str__(self):
        return '{'+'{:s}/{:d}, {:s}, {:d}, {:d}'.format(Int2IP(self.prefix), self.prefixLen, Int2IP(self.nextHop), self.metric, self.routeTag)+'}'

    def __repr__(self):
        return '{:08x}/{:d}->{:08x} m{:d} t{:d}'.format(self.prefix, self.prefixLen, self.nextHop, self.metric, self.routeTag)

'''
    Provides methods to handle RIP packets
'''
class RipPacket(object):
    def __init__(self, cmd = RIP_COMMAND_RESPONSE, version = 2):
        self.command = cmd
        self.version = version
        self.entry = []

    @property
    def size(self):
        return RIP_HEADER_SIZE + len(self.entry)*RIP_ENTRY_SIZE

    def pack(self):
        header = struct.pack(RIP_HEADER_PACK_FORMAT,self.command,self.version,0)
        entries = b''
        for ent in self.entry:
            entries = entries + ent.pack()
        packet = header + entries
        return packet

    def unpack(data):
        pkt = RipPacket()
        hdr = data[:RIP_HEADER_SIZE]
        data = data[RIP_HEADER_SIZE:]
        pkt.command, pkt.version, zero = struct.unpack(RIP_HEADER_PACK_FORMAT, hdr)
        while len(data) > 0 and len(data)%RIP_ENTRY_SIZE == 0:
            ent = data[:RIP_ENTRY_SIZE]
            data = data[RIP_ENTRY_SIZE:]
            family, tag, prefix, mask, nextHop, metric = struct.unpack(RIP_ENTRY_PACK_FORMAT, ent)
            pkt.entry.append(RipRoute(
                prefix, MaskInt2PrefixLen(mask), nextHop, metric, tag, family))
        return pkt

    def addEntry(self, prefix, prefixLen, nextHop, metric, routeTag=0, family=RIP_ADDRESS_FAMILY):
        self.entry.append(RipRoute(prefix, prefixLen, nextHop, metric, routeTag, family))
        return True

    def removeEntry(self, prefix, prefixLen, nextHop):
        for ent in self.entry:
            if ent.prefix == prefix and ent.prefixLen == prefixLen and ent.nextHop == nextHop:
                self.entry.remove(ent)
                return True
        return False

    def __getitem__(self, key):
        return self.entry[key]

    def __setitem__(self, key, value):
        self.entry[key] = value

    def __repr__(self):
        if self.command == RIP_COMMAND_REQUEST:
            cmd = 'request'
        elif self.command == RIP_COMMAND_RESPONSE:
            cmd = 'response'

        ret = 'v{:d}, {:s}, {:d} entries'.format(self.version,cmd,len(self.entry))
        return ret

'''
    main RIP process. RIP version 2.
    version 1 should be added in future.
'''
class RIP(DatagramProtocol):
    def __init__(self, inputDict):
        self.ttl = 1 # link-local. Not going to follow history ttl=2.
        self.updateTime = RIP_DEFAULT_UPDATE
        self.timeoutTime = RIP_DEFAULT_TIMEOUT
        self.garbageTime = RIP_DEFAULT_GARBAGE
        self.jitterScale = RIP_DEFAULT_JITTER_SCALE
        self.RIB = []
        self.loadConfigurationFile(inputDict['configFileName'])

    ''' Twisted Functions '''
    def startProtocol(self):
        self.transport.setTTL(self.ttl)
        self.transport.joinGroup(RIP_MULTICAST_ADDR)
        self.transport.setLoopbackMode(False)
        self.transport.setBroadcastAllowed(True)
        #
        self.requestAllRoutes()
        reactor.callLater(self.getUpdateTime(), self.sendRegularUpdate)

    def datagramReceived(self, datagram, addrPort):
        pkt = RipPacket.unpack(datagram)
        print('r', pkt, repr(addrPort))

        # we only deal with RIPv2 now.
        if pkt.version != 2:
            return

        if pkt.command == RIP_COMMAND_REQUEST:
            self.respondToRequest(pkt.entry, addrPort[0])
        elif pkt.command == RIP_COMMAND_RESPONSE:
            self.updateRIB(pkt.entry, addrPort[0])

    def connectionRefused(self):
        pass

    ''' RIP Functions '''
    def loadConfigurationFile(self, filename):
        with open(filename, 'r') as f:
            conf = json.load(f)

        if 'updateTime' in conf:
            self.updateTime = conf['updateTime']
        if 'timeoutTime' in conf:
            self.timeoutTime = conf['timeoutTime']
        if 'garbageTime' in conf:
            self.garbageTime = conf['garbageTime']

        if 'routes' in conf:
            for r in conf['routes']:
                if set(('prefix', 'prefixLen', 'nextHop')) <= set(r.keys()):
                    prefix = IP2Int(r['prefix'])
                    prefixLen = r['prefixLen']
                    nextHop = IP2Int(r['nextHop'])
                    metric = RIP_METRIC_MIN if 'metric' not in r.keys() else r['metric']
                    rtag = 0 if 'routeTag' not in r.keys() else r['routeTag']
                    self.addRouteToRIB(RipRoute(prefix, prefixLen, nextHop, metric, rtag))

    def getUpdateTime(self):
        return self.updateTime + (random.random()*2-1)*self.updateTime*self.jitterScale

    # TODO
    def respondToRequest(self, routes, addr):
        # use unicast to respond
        pass

    def updateRIB(self, routes, addr):
        for r in routes:
            if r.nextHop == 0:
                r.nextHop = IP2Int(addr)
            self.addRouteToRIB(r)
        self.refreshRIB()

    def sendRegularUpdate(self):
        pkt = RipPacket(RIP_COMMAND_RESPONSE, 2)
        for r in self.RIB:
            if r.metric < RIP_METRIC_MAX:
                pkt.addEntry(r.prefix, r.prefixLen, r.nextHop, r.metric)

        if len(pkt.entry) > 0:
            print('s', pkt, pkt.size)
            self.transport.write(pkt.pack(), (RIP_MULTICAST_ADDR, RIP_UDP_PORT))
        else:
            print('s No valid entry to send update packet.')

        reactor.callLater(self.getUpdateTime(), self.sendRegularUpdate)

    # TODO
    def sendRequest(self, route):
        # construct request packet
        pass

    def requestAllRoutes(self):
        pkt = RipPacket(RIP_COMMAND_REQUEST, 2)
        pkt.addEntry(0, 0, 0, 16, 0, 0) # a special entry to request all routing table from neighbors.
        print('s', pkt)
        self.transport.write(pkt.pack(), (RIP_MULTICAST_ADDR, RIP_UDP_PORT))

    '''
        RIB database functions
    '''
    def routeTimerSetup(self, route):
        if route.metric == RIP_METRIC_INFINITY:
            self.garbageRoute(route)
        else:
            route.timeoutTimer = reactor.callLater(self.timeoutTime, self.garbageRoute, route)
            route.garbageTimer = None

    def routeTimerReset(self, route):
        if route.timeoutTimer:
            route.timeoutTimer.reset(self.timeoutTime)
        else:
            route.timeoutTimer = reactor.callLater(self.timeoutTime, self.garbageRoute, route)
            route.garbageTimer.cancel()
            route.garbageTimer = None

    def routeTimerTimeoutGet(self, route):
        if route.timeoutTimer:
            return int(route.timeoutTimer.getTime()-time.time())
        return -1

    def routeTimerGarbageGet(self, route):
        if route.garbageTimer:
            return int(route.garbageTimer.getTime()-time.time())
        return -1

    def garbageRoute(self, route):
        route.timeoutTimer = None
        route.metric = RIP_METRIC_INFINITY
        route.garbageTimer = reactor.callLater(self.garbageTime, self.deleteRouteFromRIB, route)

    def verifyRoute(self, route):
        # check netmask
        # check prefix
        # check next hop

        if route.metric < RIP_METRIC_MIN:
            return False

        if not route.family == RIP_ADDRESS_FAMILY:
            return False

        return True

    def showRIB(self):
        for r in self.RIB:
            print(str(r)+' '+str(self.routeTimerTimeoutGet(r))+' '+str(self.routeTimerGarbageGet(r)))

    def addRouteToRIB(self, route):
        if self.verifyRoute(route) == False:
            return

        for r in self.RIB:
            if r == route:
                return

        self.routeTimerSetup(route)

        for i in range(0,len(self.RIB)):
            if self.RIB[i] >= route:
                self.RIB.insert(i, route)
                return
        self.RIB.append(route)

    def deleteRouteFromRIB(self, route):
        if len(self.RIB) == 0:
            warn('Nothing to delete in RIB.')
            return

        for r in self.RIB:
            if r == route:
                self.RIB.remove(r)

    def refreshRIB(self):
        for r in self.RIB:
            if r.metric >= RIP_METRIC_INFINITY:
                r.metric = RIP_METRIC_INFINITY

'''
    Show available options and details about this program.
'''
def showHelp():
    print('pyrip.py <configFile>')

'''
    Parse input arguments into dictionary.
'''
def inputParser(argv):
    inputDict = {}

    try:
        opts, args = getopt.getopt(argv,'h',['help'])
    except getopt.GetoptError:
        showHelp()
        sys.exit()

    for opt, arg in opts:
        if opt == '-h':
            showHelp()
            sys.exit()

    if len(args) == 0:
        showHelp()
        sys.exit()

    inputDict['configFileName'] = args[0]
    return inputDict

def main(argv):
    reactor.listenMulticast(RIP_UDP_PORT, RIP(inputParser(argv)), listenMultiple=True)
    reactor.run()

if __name__ == '__main__':
    main(sys.argv[1:])


import numpy as np
from typing import List, Tuple

def get_betas_coeff_N_unit_acts_ultimate(delta_theta_connect_idls : List[Tuple[int, int]],
                                         delta_theta_obs_start,
                                         p_or_connect_idls,
                                         p_or_obs_start,
                                         delta_theta_obs_target=None,
                                         p_or_obs_target=None,
                                         idls=None,
                                         verbose=False):

    """
        Compute the coefficients to apply the superposition theorem with each unitary action of any kind,
        using p_or for disconnect or split actions, or delta_thetas for reconnect or merge action

        Parameters
        ----------
        idls: :class:`list`, dtype:pair(int,int)
            List of grid lines or "virtual" lines corresponding to the elements on which each unitary action intervenes.
            The pair is the ids of each extremity node
        delta_theta_connect_idls: :class:`numpy.ndarray`, dtype:float64
            array of dim n_unitary_actions*n_unitary_actions. For each observation of unitary action intervention,
            it records the delta theta elemnts of every grid lines or "virtual lines" on which any considered unitary action could intervene
        delta_theta_obs_start: :class:`numpy.ndarray`, dtype:float64
             array of dim n_unitary_actions. For the base observation without any action,
             it records the delta theta elements of every grid lines or "virtual lines" on which any considered unitary action could intervene
        p_or_connect_idls: :class:`numpy.ndarray`, dtype:float64
            array of dim n_unitary_actions*n_unitary_actions. For each observation of unitary action intervention,
            it records the p_or elements of every grid lines or "virtual lines" on which any considered unitary action could intervene
        p_or_obs_start: :class:`numpy.ndarray`, dtype:float64
            array of dim n_unitary_actions. For the base observation without any action,
            it records the p_or elements of every grid lines or "virtual lines" on which any considered unitary action could intervene
        delta_theta_obs_target: :class:`numpy.ndarray`, dtype:float64
            array of dim n_unitary_actions. If computed for live assert of proper computation, for the target observation with all combined actions,
            it records the delta theta elements of every grid lines or "virtual lines" on which any considered unitary action could intervene
        p_or_obs_target: :class:`numpy.ndarray`, dtype:float64`
            array of dim n_unitary_actions. If computed for live assert of proper computation, for the target observation with all combined actions,
            it records the p_or elements of every grid lines or "virtual lines" on which any considered unitary action could intervene
        verbose: ``bool``
            Whether to print some informations or not

        Returns
        -------
        betas: :class:`numpy.ndarray`, dtype:float64
            the superposition theorem coefficient value to assign to each observation of unitary action intervention

        """

    if (p_or_obs_target is not None) and (delta_theta_obs_target is not None):
        expected_betas =[p_or_obs_target[i ] /p_or_connect_idls[i][i] if
                    ((p_or_connect_idls[i][i] != 0) and (p_or_obs_target[i])) else
                          delta_theta_obs_target[i] / delta_theta_connect_idls[i][i] for i, id_l in enumerate(idls)]
        if verbose:
            print("expected_betas :" + str(expected_betas))

    n_lines_connect = len(idls)

    a = np.array([[1 - p_or_connect_idls[i][j] / p_or_obs_start[j] if (
                (p_or_connect_idls[i][j] != 0) and (p_or_obs_start[j])) else
                   1 - delta_theta_connect_idls[i][j] / delta_theta_obs_start[j] \
                   for i in range(len(idls))] for j in range(len(idls))])
    np.fill_diagonal(a, 1)
    b = np.ones(n_lines_connect)
    if verbose:
        print(a)
        print(b)

    betas = np.linalg.solve(a, b)
    if verbose:
        print("computed betas: " + str(betas))
    return betas

# old functions using LODF matrix, still useful for demonstrations
def get_betas_coeff_N2_reconnect(id_l1, id_l2, delta_theta_line_start, delta_theta_line_unit_acts, env, verbose=False):
    init_obs, *_ = env.simulate(env.action_space())
    obs_disconnected_l1_l2, *_ = env.simulate(env.action_space({"set_line_status": [(id_l1, -1), (id_l2, -1)]}))
    obs_disconnect_l1, *_ = env.simulate(env.action_space({"set_line_status": [(id_l1, -1)]}))
    obs_disconnect_l2, *_ = env.simulate(env.action_space({"set_line_status": [(id_l2, -1)]}))

    expected_betas = [init_obs.p_or[id_l1] / obs_disconnect_l2.p_or[id_l1],
                      init_obs.p_or[id_l2] / obs_disconnect_l1.p_or[id_l2]]
    if verbose:
        print("expected_betas :" + str(expected_betas))

    a = [[1, 1 - delta_theta_line_unit_acts[1][id_l1] / delta_theta_line_start[id_l1]],
         [1 - delta_theta_line_unit_acts[0][id_l2] / delta_theta_line_start[id_l2], 1]
         ]
    b = [1, 1]

    if verbose:
        print(a)
        print(b)

    betas = np.linalg.solve(a, b)
    if verbose:
        print("computed betas: " + str(betas))
    return betas

def get_DeltaVirtual_Flows_NK(il_connect, p_il_connect, A, ilds, verbose=False):
    a = []
    for idl in ilds:
        a_row = np.array([A[il_connect][idl] * A[idlj][il_connect] + A[idlj][idl] for idlj in ilds])
        a.append(a_row)

    b = np.array([-p_il_connect * A[il_connect][idl] for idl in ilds])
    pls_virtual = np.linalg.solve(a, b)
    if verbose:
        print(pls_virtual)

    por_virtual_il_connect = p_il_connect
    for i in range(len(ilds)):
        por_virtual_il_connect += A[ilds[i]][il_connect] * pls_virtual[i]

    return por_virtual_il_connect

def get_Flows_NPlusK(p_init, A, ilds, p_ilds_connect, verbose=False):
    nl_connect = len(p_ilds_connect)
    por_virtual_ilds = []
    for idx, il_connect in enumerate(ilds):
        p_il_connect = p_ilds_connect[idx]
        ilds_il_connect = [idl for idl in ilds if idl != il_connect]
        por_virtual_ilds.append(get_DeltaVirtual_Flows_NK(il_connect, p_il_connect, A, ilds_il_connect))

    if verbose:
        print(por_virtual_ilds)

    por_connected = p_init
    for i in range(len(ilds)):
        por_connected -= A[ilds[i]] * por_virtual_ilds[i]

    return por_connected

def get_Virtual_Flows_N2(por_init, A, idl1, idl2, verbose=False):
    a = np.array([[A[idl1][idl1], A[idl2][idl1]], [A[idl1][idl2], A[idl2][idl2]]])
    b = np.array([-por_init[idl1], -por_init[idl2]])
    [pl1_virtual, pl2_virtual] = np.linalg.solve(a, b)
    if verbose:
        print(pl1_virtual)
        print(pl2_virtual)

    por_virtual = por_init + A[idl1] * pl1_virtual + A[idl2] * pl2_virtual

    return por_virtual

# a generic version for n-K
def get_Virtual_Flows_NK(por_init, A, ilds, verbose=False):
    a = []
    for idl in ilds:
        a_row = np.array([A[idlj][idl] for idlj in ilds])
        a.append(a_row)

    b = np.array([-por_init[idl] for idl in ilds])
    pls_virtual = np.linalg.solve(a, b)
    if verbose:
        print(pls_virtual)

    por_virtual = por_init
    for i in range(len(ilds)):
        por_virtual += A[ilds[i]] * pls_virtual[i]

    return por_virtual


import os
import subprocess
from setuptools import setup, find_packages

version = "master"
filename = "master"

setup(name="xdemo_client",

      version=filename,

      description="A generic and configurable software launcher",

      long_description="A generic and configurable software launcher",

      author="Florian Lier",

      author_email="flier[at]techfak.uni-bielefeld.de",

      url="https://github.com/warp1337/xdemo_client",

      download_url="https://github.com/warp1337/xdemo_client",

      packages=find_packages(exclude=["*.unittests", "*.unittests.*", "unittests.*", "unittests"]),

      scripts=['bin/xdemo_client', 'bin/xdemo_master'],

      include_package_data=True,

      keywords=['Test',
                'System Testing',
                'Component Based Testing',
                'Software Tests',
                'Automated System Execution'
                'Execution Engine',
                'Experiment Execution'],

      license="LGPLv3",

      classifiers=['Development Status :: Beta',
                   'Environment :: Console',
                   'Environment :: Robotics',
                   'Intended Audience :: Developers',
                   'License :: OSI Approved :: GNU Library or ' +
                   'Lesser General Public License (LGPL)',
                   'Operating System :: OS Independent',
                   'Programming Language :: Python',
                   'Topic :: Text Processing :: Markup :: XML'],

      install_requires=['colorlog==2.10.0',
                        'psutil==5.0.0',
                        'paramiko==2.1.2',
                        'sshtunnel==0.1.2'],

      # Workaround for: http://bugs.python.org/issue856103
      zip_safe=False

      )

# Make bin/scripts executable
if os.name.lower() == "linux":
    subprocess.call(["chmod -R ugo+x bin"], shell=True)


# -*- coding: UTF-8 -*-

def iniciala():
    """Vrátí první písmeno v daném souboru."""

    soubor = open('basnicka.txt', encoding='utf-8')
    try:
        obsah = soubor.read()
        return obsah[0]
    finally:
        # Finally blok se provede i po prikazu return!
        print('Zaviram soubor')
        soubor.close()

print(iniciala())


from __future__ import absolute_import

import functools
import logging
import logging.config
import pprint
import traceback

from logging.handlers import SysLogHandler
from logging.handlers import WatchedFileHandler

# A list of things we want to replicate from logging.
# levels
CRITICAL = logging.CRITICAL
FATAL = logging.FATAL
ERROR = logging.ERROR
WARNING = logging.WARNING
WARN = logging.WARN
INFO = logging.INFO
DEBUG = logging.DEBUG
NOTSET = logging.NOTSET

# methods
getLogger = logging.getLogger
debug = logging.debug
info = logging.info
warning = logging.warning
warn = logging.warn
error = logging.error
exception = logging.exception
critical = logging.critical
log = logging.log

# classes
root = logging.root
config = logging.config
Formatter = logging.Formatter

# handlers
StreamHandler = logging.StreamHandler
WatchedFileHandler = WatchedFileHandler
SysLogHandler = SysLogHandler

def log_debug(f):
    @functools.wraps(f)
    def wrapper(*args, **kw):
        logging.debug('%s(%s, %s) ->', f.func_name, str(args), str(kw))
        rv = f(*args, **kw)
        logging.debug(pprint.pformat(rv, indent=2))
        logging.debug('')
        return rv
    return wrapper

def fail_gracefully(f):
    """Logs exceptions and aborts."""
    @functools.wraps(f)
    def wrapper(*args, **kw):
        try:
            return f(*args, **kw)
        except Exception as e:
            # tracebacks are kept in the debug log
            logging.debug(traceback.format_exc(e))

            # exception message is printed to all logs
            logging.critical(e)

            exit(1)
    return wrapper


import os

from utils import parse_xml

def copy_folder_structure(source_dir: str, destination_dir: str) -> None:
    """Создает исходную структуру папок в новой папке."""
    for root, _, files in os.walk(source_dir):
        relative_path = os.path.relpath(root, source_dir)
        new_dir = os.path.join(destination_dir, relative_path)
        os.makedirs(new_dir, exist_ok=True)
        for file in files:
            filename, extension = os.path.splitext(file)
            if extension.lower() == '.xml':
                source_file = os.path.join(root, file)
                destination_file = os.path.join(new_dir, f'{filename}.txt')
                parse_xml(source_file, destination_file)

if __name__ == '__main__':
    source_dir = 'data'
    destination_dir = 'output'
    copy_folder_structure(source_dir, destination_dir)


import sys
import MapReduce

mr = MapReduce.MapReduce()

def mapper(record):
    id = record[0]
    nucleiod = record[1]
    mr.emit_intermediate(nucleiod[:-10], id)

def reducer(unique_nucleiod, ignore):
    mr.emit(unique_nucleiod)

inputdata = open(sys.argv[1])
mr.execute(inputdata, mapper, reducer)


from datetime import datetime
from app import db

# from sqlalchemy import BigInteger

class BaseModel(object):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    @classmethod
    def create(cls, *periods: tuple[dict, dict]):

        try:
            for period in periods:
                obj = cls(**period)
                db.session.add(obj)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            obj = getattr(e, "message", str(e))

        return obj

class Timeshits(BaseModel, db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(120), nullable=False)
    start = db.Column(db.DateTime, default=datetime.now())
    end = db.Column(db.DateTime, default=datetime.now())


import save_file
from bs4 import BeautifulSoup
from ex import p_red, p_green, p_blue

def templ_main():
    return '''+->
| {}
+->
Posted on Wattpad: {}
<--- Description --->
{}
<--- References --->
Wattpad: {}
Wattpad Downloader: https://github.com/Hell13Cat/WPD'''

def templ_ch():
    return '''+->
| {}
+->
{}'''

def m(book):
    p_green("[I] Saving in txt...")
    info_book = templ_main().format(book["title"], book["authors"], book["description"], book["source"])
    save_file.save_txt_info(book["id"] + " - " + book["title"], info_book)
    chapters = book["characters"]
    chapter_count = 0
    for capter in chapters:
        chapter_count += 1
        print('[{c_num}/{c_all}] Saving "{chapter_title}": {chapter_id}'.format(c_num=str(chapter_count), c_all=str(len(chapters)), chapter_title=capter["title"], chapter_id=capter["id"]))
        chapter_text = ""
        title = capter["title"]
        text = capter["text"]
        text = text.replace("<br>", "\n")
        soup = BeautifulSoup(text, 'html5lib')
        tags = soup.find_all('img', src=True)
        for ii in range(len(tags)):
            img_tag_url = (tags[ii])["src"]
            save_file.save_url_name("#" + str(chapter_count) + "~" + str(ii + 1) + ".jpg", book["id"] + " - " + book["title"], img_tag_url)
            img_tag = soup.select_one("img:nth-of-type(1)")
            img_tag.decompose()
        tags = soup.find_all("p")
        for tag in tags:
            chapter_add = "\n\n" + tag.text
            chapter_text += chapter_add
        save_file.save_txt(book["id"] + " - " + book["title"], "#" + str(chapter_count) + " " + title + ".txt", templ_ch().format(capter["title"], chapter_text))
    p_green("[I] Saved to txt")


import pygame as pg

LOADED_FONTS = {}

class _Label(object):
    '''Parent class all labels inherit from. Color arguments can use color names
       or an RGB tuple. rect_attributes should be a dict with keys of
       pygame.Rect attribute names (strings) and the relevant position(s) as values.'''
    def __init__(self, font_path, font_size, text, text_color, rect_attributes,
                         bground_color=None):
        if (font_path, font_size) not in LOADED_FONTS:
            LOADED_FONTS[(font_path, font_size)] = pg.font.Font(font_path,
                                                                                font_size)
        f = LOADED_FONTS[(font_path, font_size)]
        if bground_color is not None:
            self.text = f.render(text, True, pg.Color(text_color),
                                         pg.Color(bground_color))
        else:
            self.text = f.render(text, True, pg.Color(text_color))

        self.rect = self.text.get_rect(**rect_attributes)

    def draw(self, surface):
        surface.blit(self.text, self.rect)

class Label(_Label):
    '''Creates a surface with text blitted to it (self.text) and an associated
       rectangle (self.rect). Label will have a transparent background if
       bground_color is not passed to __init__.'''
    def __init__(self, font_path, font_size, text, text_color, rect_attributes,
                         bground_color=None):
        super(Label, self).__init__(font_path, font_size, text, text_color,
                                                rect_attributes, bground_color)

class GroupLabel(Label):
    '''Creates a Label object which is then appended to group.'''
    def __init__(self, group, font_path, font_size, text, text_color,
                         rect_attributes, bground_color=None):
        super(GroupLabel, self).__init__(font_path, font_size, text, text_color,
                                                        rect_attributes, bground_color)
        group.append(self)

class Button(object):
    def __init__(self, left, top, width, height, label, payload, amount):
        self.rect = pg.Rect(left, top, width, height)
        label.rect.center = self.rect.center
        self.label = label
        self.payload = payload
        self.amount = amount

    def draw(self, surface):
       pg.draw.rect(surface, pg.Color("gray70"), self.rect)
       pg.draw.rect(surface, pg.Color("gray10"), self.rect, 2)
       self.label.draw(surface)

class GroupButton(Button):
    def __init__(self, group, left, top, width, height, label, payload, amount):
        super(GroupButton, self).__init__(left, top, width, height, label, payload, amount)
        group.append(self)


from rest_framework import status
from rest_framework.decorators import api_view
from api.models import Movie, update_movie_view_count, MovieClustering
from api.serializers import MovieSerializer, MovieDetailSerializer
from rest_framework.response import Response
import random
from django.http import JsonResponse

@api_view(['GET', 'POST', 'DELETE'])
def movies(request):
    if request.method == 'GET':
        id = request.GET.get('id', request.GET.get('movie_id', None))
        isUpdateRating = request.GET.get('isUpdateRating', None)
        title = request.GET.get('title', None)
        genres = request.GET.get('genres', None)
        sortBy = request.GET.get('sortBy', None)
        sortingBy = request.GET.get('sortingBy', None)
        agesItems = ["-","0~19", "20~29", "30~39", "40~"]
        genderItems = ["-", "Male", "Female"]

        movies = Movie.objects.all()

        if id and isUpdateRating:
            movies = movies.filter(pk=id)
            serializer = MovieDetailSerializer(movies, many=True)
            return Response(data=serializer.data, status=status.HTTP_200_OK)

        if id:
            movies = movies.filter(pk=id)
            update_movie_view_count(movie_id=id)
            serializer = MovieDetailSerializer(movies, many=True)
            return Response(data=serializer.data, status=status.HTTP_200_OK)

        if title:
            movies = movies.filter(title__icontains=title)
        if genres:
            if genres == "All":
                genres = ""
            movies = movies.filter(genres__icontains=genres)
        if sortBy:
            if sortBy == "조회순":
                movies = movies.order_by('-views_count')
            elif sortBy == "평점순":
                movies = movies.order_by('-average_rating')
        if sortingBy:
            if sortingBy in agesItems:
                if sortingBy =="0~19":
                    movies = sorted(movies, key=lambda i: i.age10/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
                elif sortingBy =="20~29":
                    movies = sorted(movies, key=lambda i: i.age20/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
                elif sortingBy == "30~39":
                    movies = sorted(movies, key=lambda i: i.age30/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
                elif sortingBy == "40~":
                    movies = sorted(movies, key=lambda i: i.age40/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
                else:
                    movies = Movie.objects.all()
            elif sortingBy in genderItems:
                if sortingBy == "Male":
                    movies = sorted(movies, key=lambda i: i.male/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
                elif sortingBy =="Female":
                    movies = sorted(movies, key=lambda i: i.female/(i.total_rater if i.total_rater!=0 else 1), reverse=True)
            else:
                occupation_map = {'other': 0, 'academic/educator': 1, 'artist': 2, 'clerical/admin': 3, 'college/grad student': 4,
                 'customer service': 5, 'doctor/health care': 6, 'executive/managerial': 7, 'farmer': 8, 'homemaker': 9,
                 'K-12 student': 10, 'lawyer': 11, 'programmer': 12, 'retired': 13, 'sales/marketing': 14, 'scientist': 15,
                 'self-employed': 16, 'technician/engineer': 17, 'tradesman/craftsman': 18, 'unemployed': 19, 'writer': 20}
                index = "job" + (str(occupation_map[sortingBy]) if len(str(occupation_map[sortingBy])) != 1 else "0" + str(occupation_map[sortingBy]))
                movies = sorted(movies, key=lambda i: getattr(i,index) / (i.total_rater if i.total_rater != 0 else 1), reverse=True)

        serializer = MovieSerializer(movies, many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    if request.method == 'DELETE':
        movie = Movie.objects.all()
        movie.delete()
        return Response(status=status.HTTP_200_OK)

    if request.method == 'POST':
        movies = request.data.get('movies', None)
        for movie in movies:
            id = movie.get('id', None)
            title = movie.get('title', None)
            genres = movie.get('genres', None)

            if not (id and title and genres):
                continue
            if Movie.objects.filter(id=id).count() > 0 or Movie.objects.filter(title=title).count() > 0:
                continue

            Movie(id=id, title=title, genres='|'.join(genres)).save()

        return Response(status=status.HTTP_200_OK)

@api_view(['GET'])
def get_movie_recommendation(request):
    if request.method == 'GET':
        movie_id = request.GET.get('movie_id', None)
        fit = request.GET.get('fit', None)  # 알고리즘 기준 현재의 경우 k-means
        movie = Movie.objects.get(id=movie_id)
        movie_clustering = movie.movieclustering  # User Clustering object
        label = getattr(movie_clustering, fit)
        # 해당 cluster label 에 속한 다른 user_set 추출하기
        _clusters = MovieClustering.objects.filter(KMeans_labels=label)
        clusters = []
        for _ in range(100):
            a = random.choice(_clusters)
            if a not in clusters and len(clusters) < 10:
                clusters.append(a)
        recommendation_set = []
        for cluster in clusters:
            movie = Movie.objects.get(id=cluster.movie_id)
            recommendation_set.append({
                "id": movie.id,
                "title": movie.title
            })
        return JsonResponse({
            'recommendation_set': recommendation_set
        })


# -*- coding: utf-8 -*-

class Core(object):

    # Base URLs.
    cdn_flightradar_base_url = "https://cdn.flightradar24.com"
    flightradar_base_url = "https://www.flightradar24.com"
    data_live_base_url = "https://data-live.flightradar24.com"

    # Flights data URLs.
    real_time_flight_tracker_data_url = data_live_base_url + "/zones/fcgi/feed.js"
    flight_data_url = data_live_base_url + "/clickhandler/?flight={}"

    # Airports data URLs.
    airport_data_url = flightradar_base_url + "/airports/traffic-stats/?airport={}"
    airports_data_url = flightradar_base_url + "/_json/airports.php"

    # Airlines data URL.
    airlines_data_url = flightradar_base_url + "/_json/airlines.php"

    # Zones data URL.
    zones_data_url = flightradar_base_url + "/js/zones.js.php"

    # Country flag image URL.
    country_flag_url = flightradar_base_url + "/static/images/data/flags-small/{}.gif"

    # Airline logo image URL.
    airline_logo_url = cdn_flightradar_base_url + "/assets/airlines/logotypes/{}_{}.png"
    alternative_airline_logo_url = flightradar_base_url + "/static/images/data/operators/{}_logo0.png"

    headers = {
        "accept-encoding": "gzip, br",
        "accept-language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
        "cache-control": "max-age=0",
        "origin": "https://www.flightradar24.com",
        "referer": "https://www.flightradar24.com/",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }

    json_headers = headers.copy()
    json_headers["accept"] = "application/json"

    image_headers = headers.copy()
    image_headers["accept"] = "image/gif, image/jpg, image/jpeg, image/png"


import sys, getopt
from tkinter import Tk
from src.gui.board import Board
from src.backend.players import Players

def main(argv):
    try:
        opts, args = getopt.getopt(argv,"","two-player")
    except getopt.GetoptError:
        sys.exit(2)
    number_of_players = 4
    for opt, arg in opts:
        if opt in ("--two-player"):
            number_of_players = 2
    window = Tk()
    window.resizable(width=False, height=False)
    window.wm_title("Blokt")
    board = Board(window, Players(number_of_players=number_of_players))
    window.mainloop()

if __name__ == "__main__":
    main(sys.argv[1:])


# Shivani Bhatia
# EC500 C1 Building Software
# API Project - Twitter Image Downloader
# image_download.py

import configparser
import wget
import sys
import os

from tweepy import API, OAuthHandler, Stream

def config_parse(filename):
    cparse = configparser.ConfigParser()
    cparse.read(filename)
    return cparse

def authorization(cparse):
    auth = OAuthHandler(cparse['API']['consumerKey'], cparse['API']['consumerSecret'])
    auth.set_access_token(cparse['API']['accessToken'], cparse['API']['accessTokenSecret'])
    api = API(auth)
    return api

def downloadTweets(username, api, max_img, output):

    media_tweets = set()
    downloaded =  0
    back_count = max_img

    try:
        tweets = api.user_timeline(screen_name=username,
                                   include_rts=False,
                                   exclude_replies=True)

    except Exception as e:
        print(e)
        sys.exit()

    for posts in tweets:
        media = posts.entities.get('media', [])
        if (len(media) > 0):
            if (media[0]['type'] == 'photo'):
                media_tweets.add(media[0]['media_url'])
                downloaded = downloaded + 1
                if (downloaded == max_img):
                    break

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

    last_id = int(tweets[-1].id - 1)

    while (downloaded < max_img):
        temp_tweets = api.user_timeline(screen_name=username,
                                        max_id=last_id,
                                        include_rts = False,
                                        exclude_replies = True)

        if len(temp_tweets) == 0:
            break
        else:
            for posts in temp_tweets:
                media = posts.entities.get('media', [])
                if (len(media) > 0):
                    if (media[0]['type'] == 'photo'):
                        media_tweets.add(media[0]['media_url'])
                        downloaded = downloaded + 1
                        if (downloaded == max_img):
                            break

            last_id = int(temp_tweets[-1].id - 1)

    for files in media_tweets:
        if (back_count != 0):
            wget.download(files,out=output)
            back_count = back_count - 1

def renameImages(output):
    for i, filename in enumerate(os.listdir(output)):
        os.rename(output + "/" + filename, output + "/image" + str(i+1) + ".jpg")


import matplotlib.pyplot as plt
import matplotlib.patches as patches
from .source.directory_source import DirectoryDataSource

class DatasetExplorer():

    def __init__(self, cfg):
        # Case is one of classification, segmentation, detection. Classification by default
        try:
            self.case = cfg['case']
        except KeyError:
            self.case = "classification"
        self.source = DirectoryDataSource(cfg['input_file'], self.case)

    def _count_images(self):
        return len(self.source.list_images())

    def images(self):
        return self.source.list_images()

    def view_image(self, id):
        image = self.source.get_image(id)
        label = self.source.get_label(id)
        fig, ax = plt.subplots()
        ax.imshow(image)
        if self.case == "classification":
            plt.title("Class {}".format(label))
        if self.case == "segmentation":
            plt.imshow(label, alpha=0.3)
        if self.case == "detection":
            for bbox in label:
                bbox = patches.Rectangle((bbox[0],
                                          bbox[1]),
                                         bbox[2] - bbox[0],
                                         bbox[3] - bbox[1],
                                         linewidth=1, edgecolor='r', facecolor='none')
                ax.add_patch(bbox)
        plt.show()


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 07 16:33:52 2017

@author: marco
"""
from typing import Optional

from yaocptool.methods.base.solutionmethodinterface import SolutionMethodInterface
from yaocptool.modelling.ocp import OptimalControlProblem
from yaocptool.modelling.system_model import SystemModel

class Node:
    def __init__(
        self,
        node_id: int = None,
        name: Optional[str] = None,
        model: Optional[SystemModel] = None,
        problem: Optional[OptimalControlProblem] = None,
        solution_method: Optional[SolutionMethodInterface] = None,
        color: float = 0.25,
    ):
        self.name = name
        self.node_id = node_id
        self.problem: OptimalControlProblem = problem  # type: ignore
        self.solution_method = solution_method
        self.color = color

        self.model: SystemModel = problem.model if model is None and problem is not None else model  # type: ignore

        if name is None and self.model is not None:
            self.name = self.model.name

    def __repr__(self) -> str:
        return "<Node name: {}, node_id: {}>".format(self.name, self.node_id)


from odoo import api, fields, models

class StockMove(models.Model):
    _inherit = "stock.move"

    rma_line_id = fields.Many2one(
        "rma.order.line", string="RMA line", ondelete="restrict"
    )

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            if vals.get("group_id"):
                group = self.env["procurement.group"].browse(vals["group_id"])
                if group.rma_line_id:
                    vals["rma_line_id"] = group.rma_line_id.id
        return super().create(vals_list)

    def _action_assign(self, force_qty=False):
        res = super()._action_assign(force_qty=force_qty)
        for move in self:
            if move.rma_line_id:
                move.partner_id = move.rma_line_id.partner_id.id or False
        return res

    @api.model
    def _get_first_usage(self):
        if self.move_orig_ids:
            # We assume here that all origin moves come from the same place
            return self.move_orig_ids[0]._get_first_usage()
        else:
            return self.location_id.usage

    @api.model
    def _get_last_usage(self):
        if self.move_dest_ids:
            # We assume here that all origin moves come from the same place
            return self.move_dest_ids[0]._get_last_usage()
        else:
            return self.location_dest_id.usage

    def _should_bypass_reservation(self, forced_location=False):
        res = super()._should_bypass_reservation(forced_location=forced_location)
        if self.env.context.get("force_no_bypass_reservation"):
            return False
        return res

    def _get_available_quantity(
        self,
        location_id,
        lot_id=None,
        package_id=None,
        owner_id=None,
        strict=False,
        allow_negative=False,
    ):
        if (
            not lot_id
            and self.rma_line_id.lot_id
            and self.location_id.usage == "internal"
        ):
            # In supplier RMA deliveries we can only send the RMA lot/serial.
            lot_id = self.rma_line_id.lot_id
        return super()._get_available_quantity(
            location_id,
            lot_id=lot_id,
            package_id=package_id,
            owner_id=owner_id,
            strict=strict,
            allow_negative=allow_negative,
        )

    def _update_reserved_quantity(
        self,
        need,
        available_quantity,
        location_id,
        lot_id=None,
        package_id=None,
        owner_id=None,
        strict=True,
    ):
        if (
            not lot_id
            and self.rma_line_id.lot_id
            and self.location_id.usage == "internal"
        ):
            # In supplier RMA deliveries we can only send the RMA lot/serial.
            lot_id = self.rma_line_id.lot_id
        return super()._update_reserved_quantity(
            need,
            available_quantity,
            location_id,
            lot_id=lot_id,
            package_id=package_id,
            owner_id=owner_id,
            strict=strict,
        )

    @api.model
    def _prepare_merge_moves_distinct_fields(self):
        res = super()._prepare_merge_moves_distinct_fields()
        return res + ["rma_line_id"]


import MySQLdb

def exdbsql(sql):
    # 连接数据库
    db = MySQLdb.connect('localhost', 'root', '123456', 'dbname')
    # 创建一个游标对象
    cursor = db.cursor()
    # 执行sql语句
    cursor.execute(sql)
    # 得到查询结果
    datas = cursor.fetchall()
    # 关闭连接
    db.close()
    return datas

if __name__ == '__main__':
    datas = exdbsql('select * from student')


import os
import sys
from brownie import network, accounts

def get_is_live():
    return network.show_active() != "development"

def get_deployer_account(is_live):
    if not is_live:
        deployer = accounts[0]
        return deployer
    if "DEPLOYER" not in os.environ:
        raise EnvironmentError(
            "Please set DEPLOYER env variable to the deployer account name"
        )

    return accounts.load(os.environ["DEPLOYER"])

def prompt_bool():
    choice = input().lower()
    if choice in {"yes", "y"}:
        return True
    elif choice in {"no", "n"}:
        return False
    else:
        sys.stdout.write("Please respond with 'yes' or 'no'")

def get_env(name, default=None):
    if name not in os.environ:
        if default is not None:
            return default
        raise EnvironmentError(f"Please set {name} env variable")
    return os.environ[name]


# 413. 等差数列划分

# 如果一个数列 至少有三个元素 ，并且任意两个相邻元素之差相同，则称该数列为等差数列。

# 例如，[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。
# 给你一个整数数组 nums ，返回数组 nums 中所有为等差数组的 子数组 个数。

# 子数组 是数组中的一个连续序列。
from typing import List

class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        res = 0
        n = len(nums)
        diff = []
        for i in range(1,n):
            diff.append(nums[i]-nums[i-1])
        begin = 0
        for i in range(1,len(diff)):
            if diff[i]!=diff[begin]:
                tmp = i-begin
                if tmp>=2:
                    res += (tmp-1)*tmp//2
                begin=i
        else:
            tmp = len(diff)-begin
            if tmp>=2:
                res += (tmp-1)*tmp//2
        return res

sol = Solution()
nums = [1,2,3,4]
print(sol.numberOfArithmeticSlices(nums))


from .pblefdef_pb2 import LibraryExchangeFormat as PbLef
from collections import OrderedDict

class LibraryExchangeFormat:
    def __init__(self, filename) -> None:
        self.lef = PbLef()
        with open(filename, 'rb') as fp:
            self.lef.ParseFromString(fp.read())
        self.macros = OrderedDict()
        for m in self.lef.macros:
            self.macros[m.name] = m


"""
Title: Trial Num Bot Endpoint
Date Started: Feb 8, 2022
Version: 1
Version Start: Feb 8, 2022
Author: David Hyongsik Choi
Legal:  All rights reserved.  This code may not be used, distributed, or copied without the express written consent of David Hyongsik Choi.
Purpose:  To provide API endpoint for Age bot.
"""
# IMPORT TOOLS
#   STANDARD LIBRARY IMPORTS
#   THIRD PARTY IMPORTS
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
from dashappobject import app
import plotly.express as px
import pandas as pd
#   LOCAL APPLICATION IMPORTS
from Modules.stats_profilers import stat_profilerv3
from ..dashinputs import prompt_builder, gen_tablecontents, dash_inputbuilder
from ..botclasses import BotParams
from Modules.referencetools.statresearch.trialnumbot import num_trial_bot, num_trial_bot_report
from ..os_functions import get_currentscript_filename
from formatting import formatting_schema

dccgraph_config = formatting_schema['dccgraph_config']
figure_layout_mastertemplate = formatting_schema['figure_layout_mastertemplate']

bp = BotParams(
    get_currentscript_filename(__file__),
    'Trial Num Bot',
    "The Trial Num Bot returns the number of trials recommended to run based on how much an effect you desire an additional sample should have on the overall mean of the population.",
    num_trial_bot
)
tbodydata = [
    {
        'id': f'randmin_{bp.botid}',
        'placeholdertext': 'min value',
        'prompt': 'Enter the smallest value a random sample can have',
        'inputtype': 'number'
        },
    {
        'id': f'randmax_{bp.botid}',
        'placeholdertext': 'max value',
        'prompt': 'Enter the largest value a random sample can have',
        'inputtype': 'number'
        },
    {
        'placeholdertext': 'tolerance level',
        'id': f'tolerance_{bp.botid}',
        'prompt': "Set the tolerance.  This is the number an additional sample's effect on the overall mean of the population must stay below for that sample's effect to be considered statistically insignificant.",
        'details': 'For example, entering 0.025 means that for an additional sample to have no significant impact on the overall population, it would have to have an impact of less than 2.5%, meaning it may only move the overall population average by an amount less than that.',
        'inputtype': 'number',
        'min': 0
        },
    {
        'placeholdertext': 'window proportion',
        'id': f'win_prop_{bp.botid}',
        'prompt': 'Set the proportion of all samples starting with most recent that you want to include when considering the effect a sample has on the overall mean.',
        'details': 'For example, entering 0.5 means that the bot will use the most recent 50% of all trial samples to determine what is the recent maximum effect an additional sample had on the overall population.',
        'inputtype': 'number',
        'min': 0,
        'max': 1
        }
]

layout = html.Div([
    html.Div([
        html.Table(gen_tablecontents(tbodydata)),
        prompt_builder({
            'id': f'submitbutton_{bp.botid}',
            'inputtype': 'button_submit'
            })
        ], id=f'input_{bp.botid}'),
    html.Div(id=f'output_{bp.botid}')
])

@app.callback(
    Output(f'output_{bp.botid}', 'children'),
    Input(f'submitbutton_{bp.botid}', 'n_clicks'),
    State(f'randmin_{bp.botid}', "value"),
    State(f'randmax_{bp.botid}', "value"),
    State(f'tolerance_{bp.botid}', "value"),
    State(f'win_prop_{bp.botid}', "value"),
    prevent_initial_call=True
    )
def calc_numtrialbot(
        n_clicks,
        randmin,
        randmax,
        tolerance,
        win_prop
        ):
    s = num_trial_bot(randmin, randmax, tolerance, win_prop)
    report = num_trial_bot_report(s)
    df = pd.DataFrame(data=s)
    statdf = pd.DataFrame(data=[stat_profilerv3(s[-1]['data'])])
    return [
        html.Div([html.P(i) for i in report]),
        html.Div([
            html.P('Stat summary of all trial samples:'),
            dash_inputbuilder({
                'data': statdf.to_dict('records'),
                'inputtype': 'table',
                'id': f"statsumtable_{bp.botid}"
                })
            ]),
        dcc.Graph(figure=px.line(
            df.iloc[:, :-2],
            x='trial',
            y=['sample', 'avg_before', 'avg_after', 'sample_effect', 'recent_max_effect', 'tolerance'],
            markers=False,
            template=figure_layout_mastertemplate
            ), config=dccgraph_config)
    ]


# user/models.py
from django.db import models
from django.contrib.auth import get_user_model

class UserProfile(models.Model):
    """User profile model for users"""
    user = models.OneToOneField(
        get_user_model(), related_name='userprofile', on_delete=models.CASCADE)
    image_url = models.CharField(max_length=200, null=True, blank=True)
    company_name = models.CharField(max_length=100, null=True, blank=True)
    is_admin = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.username


import pandas as pd
from scipy.stats import shapiro
from scipy import stats

pd.set_option('display.max_columns', None)
pd.set_option('display.float_format', lambda x: '%.5f' % x)

# Reading Datasets. There are two different datasets to compare.
df_control = pd.read_excel("Week_05/AB_Test_Project/ab_testing_data.xlsx", sheet_name="Control Group")
df_test = pd.read_excel("Week_05/AB_Test_Project/ab_testing_data.xlsx", sheet_name="Test Group")

df_control.head()
df_test.head()

df_control.shape
df_test.shape

df_control.info()
df_test.info()

# Setting outliers, if there are any.
def outlier_thresholds(dataframe, variable, low_quantile=0.05, up_quantile=0.95):
    quantile_one = dataframe[variable].quantile(low_quantile)
    quantile_three = dataframe[variable].quantile(up_quantile)
    interquantile_range = quantile_three - quantile_one
    up_limit = quantile_three + 1.5 * interquantile_range
    low_limit = quantile_one - 1.5 * interquantile_range
    return low_limit, up_limit

low_limit_click, up_limit_click = outlier_thresholds(df_control, "Impression")
low_limit_click, up_limit_click = outlier_thresholds(df_control, "Click")
low_limit_purchase, up_limit_purchase = outlier_thresholds(df_control, "Purchase")
low_limit_earning, up_limit_earning = outlier_thresholds(df_control, "Earning")

def replace_with_thresholds(dataframe, variable):
    low_limit, up_limit = outlier_thresholds(dataframe, variable)
    # dataframe.loc[(dataframe[variable] < low_limit), variable] = low_limit
    dataframe.loc[(dataframe[variable] > up_limit), variable] = up_limit

column_names_to_set = ["Impression", "Click", "Purchase", "Earning"]

for variable in column_names_to_set:
    replace_with_thresholds(df_control, variable)
    replace_with_thresholds(df_test, variable)

# Defining the AB Testing hypothesis
# H0: There is no statistical difference between the control and test groups in terms of the average number of purchases
# H1: There is statistical difference between the control and test groups in terms of the average number of purchases.
df_control["Purchase"].mean()
df_test["Purchase"].mean()

# Target variables here is "Purchase".
control_purchase = df_control["Purchase"]
test_purchase = df_test["Purchase"]

# There are 2 prerequisites for the AB test: Assumption of Normality and Homogeneity of Variance.

# Normality Test
# H0: Assumption of Normality is provided.
# H1: Assumption of Normality is not provided.
test_statistic_control, p_value_control = shapiro(control_purchase)
print('Test Statistic = %.4f, p-value = %.4f' % (test_statistic_control, p_value_control))
p_value_control < 0.05
test_statistic_test, p_value_test = shapiro(test_purchase)
print('Test Statistic = %.4f, p-value = %.4f' % (test_statistic_test, p_value_test))
p_value_test < 0.05
# Both p values are higher than 0.05 so we cant reject H0, assumption of Normality is provided.

# Variance Homogeneity Test - Levene's test
# H0: Homogeneity of Variance is provided.
# H1: Homogeneity of Variance is not provided.
stats.levene(control_purchase, test_purchase)
# p value is higher than 0.05 so we cant reject H0, Homogeneity of Variance is provided.
# In this case, both two conditions are provided.

# AB Testing hypothesis
# H0: There is no statistical difference between the control and test groups in terms of the average number of purchases
# H1: There is statistical difference between the control and test groups in terms of the average number of purchases.
test_statistic, p_value = stats.ttest_ind(control_purchase, test_purchase, equal_var=True)

print('Test Statistic = %.4f, p-value = %.4f' % (test_statistic, p_value))
# p value is higher than 0.05 so we can't reject H0.
# There is no statistical difference between the control and test groups in terms of the average number of purchases.


import datetime
import random
import re
import time
import warnings
from multiprocessing import Process, Queue
import sdbfuncs

import urllib3
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import config
import SBDdata

warnings.filterwarnings("ignore")

class Item():
    name = ''
    def __init__(self, name , color, size,category):
        self.name=name
        self.color=color
        self.category = category
        self.size=size
        self.sizeid=''
        self.shopid = ''
        self.itemid=''
        self.colid=''
        self.active=False
    def __str__(self):
        return  self.name+':  '+str(self.shopid)+':'+str(self.itemid)+':'+str(self.colid)

def findItems(item,q):
    #Найти товар на странице
    http = urllib3.PoolManager()

    ad=False
    r = http.request('GET', 'https://www.supremenewyork.com/shop/all/'+item.category)
    d = r.data
    soup = BeautifulSoup(d)
    brke=False

    for link in soup.find_all('a'):
        if(brke):
            break

        if (sdbfuncs.iseacval (link.contents[0], item.name )):

            for l in soup.find_all('a'):
                if l.get('href') == link.get('href') and (str(l.contents[0]).lower() == str(item.color).lower() or item.color=='any'):

                    page_url = l.get('href')
                    http = urllib3.PoolManager()
                    r = http.request('GET',
                                     "http://www.supremenewyork.com/" + page_url)
                    d = r.data
                    print(d)
                    parsed_html = BeautifulSoup(d)

                    at = parsed_html.body.find('select', attrs={'id': 'size'})
                    at = at.find_all('option')

                    for sz in at:
                        if (sz.text == item.size or item.size == 'any'):
                            item.sizeid=sz['value']
                            break

                    at = str(parsed_html.body.find('input', attrs={'id': 'style'}))
                    a = str(re.search(r'\d{4,6}', at).group())

                    ct = str(parsed_html.body.find('form', attrs={'id': 'cart-addf'}))
                    c = str(re.search(r'\d{5,6}', ct).group())
                    item.shopid = c
                    item.colid = a
                    if(a!=''):
                        item.active=True

                    print('Найден: ',item.name)
                    ad=True

                    brke=True
                    break

    if (ad==False) :
        print('Товар не найден:',item.name)

    q.put(item)

def wait():
    http = urllib3.PoolManager()
    r = http.request('GET',
                     "https://www.supremenewyork.com/shop/new")
    d = r.data
    d1 = d
    while (d == d1):
        r = http.request('GET',
                         "https://www.supremenewyork.com/shop/new")
        d1 = r.data
        time.sleep(2)

def bot(data,items):

    JsBuy = open('buy.js').read()
    JScode = open('additem.js').read()
    options = Options()
    options.add_argument('test-type')
    options.accept_untrusted_certs = True
    options.assume_untrusted_cert_issuer = True
    options.add_argument("user-data-dir=selenium")
    # options.set_headless(headless=True)

    driver = webdriver.Chrome(options=options)
    driver.set_window_size(1050, 650)
    #print('Ходят легенды, если потренеровать капчу заранее, шанс взять выше:')
    #driver.get('https://patrickhlauke.github.io/recaptcha/')

    print('Ожидание дропа.. ничего не трогай!')
    #sdbfuncs.waittime()
    url = "https://www.supremenewyork.com/shop/all/new"
    driver.get(url)
    #sdbfuncs.waitdrop()

    ti=time.clock()
    print("Бот запущен",datetime.datetime.now())

    q = Queue()
    proc =[]
    for item in items:
        proc.append(Process(target=findItems, args=(item, q)))

    for p in proc:#запускаем все
        p.start()

    for p in proc:#ждем окончание всех процессов
        p.join()

    print('all items detected')

    for i in range(len(items)):
        items[i]=q.get()
    #print('Добавлено в список задач', q.get())

    for i in range(len(items)):
        if(items[i].active==False):
            print('Товар не найден или sold out: ',items[i].name)
            continue

        cd = JScode.format(shopid=items[i].shopid, styleid=items[i].colid, sizeid=items[i].sizeid)
        driver.execute_script(cd)
        print("Товар добавлен в корзину", items[i], time.clock()-ti)
        if(i+1!=len(items)):
            time.sleep(0.3)

    JsBuy=JsBuy.format(name=data['fio'],email=data['email'],tel=data['tel']
                       ,adress=data['adress'],sity=data['sity'],cardnom=data['card_nom']
                       ,zip=data['postcode'],cvv=data['card_cvc'],card_type=data['card_type'],
                       card_month=data['card_month'],card_year=data['card_year'])
    driver.execute_script(JsBuy)
    print("Куплено", time.clock()-ti)
    time.sleep(5)
    driver.save_screenshot('scrin.png')
    time.sleep(360)

def main(data, items):
    bot(data, items)

def run():
    import SBDdata
    try:
        data=SBDdata.getdata()
    except :
        sdbfuncs.setdata()
        data = SBDdata.getdata()

    items = []

    name = 'Box Logo Crewneck Sweatshirt'
    print('name: ',name)
    size=input('Item size: ')
    color = input('Item color: ')

    wt=random.uniform(0.9, 1.8)
    while True:
        #name=input('Item name: ')

        if(name)=='-':
            break
        #size=input('Item size: ')
        #color=input('Item color: ')
        category=input('Item category [low letter]: ')
        if (category) == '-':
            break

        itm = Item(name, color, size, category)
        items.append(itm)
        wt += random.uniform(0.2, 0.5)

    print("--------------------------")
    print("Товар добавлен, рассчетное время работы бота",wt,'c')
    main(data,items)

def testrun():
    import SBDdata
    try:
        data = SBDdata.getdata()
    except:
        sdbfuncs.setdata()
        data = SBDdata.getdata()

    items = []

    name = 'Diamond Faux Fur Jacket'
    color="any"
    size="any"
    category="jackets"
    print('name: ', name)

    wt = random.uniform(0.9, 1.8)

    itm = Item(name, color, size, category)
    items.append(itm)
    wt += random.uniform(0.2, 0.5)

    print("--------------------------")
    print("Товар добавлен, рассчетное время работы бота", wt, 'c')
    main(data, items)

if __name__ == "__main__":
    print(config.avaimg)
    if(sdbfuncs.issetting()==False):
        sdbfuncs.setsetting()
    #boxlogorun()
    testrun()
    #run()


import landsatxplore.api
from landsatxplore.earthexplorer import EarthExplorer

api = landsatxplore.api.API('******', '*******')
ee = EarthExplorer('******', '******')

# Request
scenes = api.search(
    dataset='LANDSAT_8_C1',
    latitude=37.01,
    longitude=-5.93,
    start_date=m,
    end_date=t,
    max_cloud_cover=100)

print('{} scenes found.'.format(len(scenes)))

for scene in scenes:

    sc = scene['displayId']
    if scene['displayId'].split('_')[2] == '202034':
        print('downloading:', sc)
        ee.download(scene_id=sc, output_dir='/home/last/Descargas')

ee.logout()
api.logout()


import chj.util.printutil as UP

from typing import Dict, List, Set, Tuple, TYPE_CHECKING

if TYPE_CHECKING:
    from chj.index.AppAccess import AppAccess
    from chj.index.CallgraphDictionary import CallgraphTargetBase

class Recursion():

    def __init__(self, app: "AppAccess"):
        self.app = app
        self.appedges: Dict[int, Set[int]] = self.get_appedges()    # cmsix -> targets

    def get_appedges(self) -> Dict[int, Set[int]]:
        jd = self.app.jd
        appedges: Dict[int, Set[int]] = {}

        def f(caller: int, pc: int, callee: int, tgt: "CallgraphTargetBase") -> None:
            if tgt.has_application_targets():
                if not caller in appedges: appedges[caller] = set([])
                cmstgts = [ jd.get_cmsix(cnix,callee) for cnix in tgt.get_application_targets() ]
                for c in cmstgts:
                    appedges[caller].add(c)

        jd.iter_callgraph_edges(f)
        return appedges

    def get_self_recursive_calls(self) -> List[int]:
        recursivecalls = []

        for caller in self.appedges:
            if caller in self.appedges[caller]:
                recursivecalls.append(caller)

        return recursivecalls

    def get_mutual_recursive_calls(self) -> Set[Tuple[int, int]]:
        mutualrecursivecalls = set([])

        for caller in self.appedges:
            callees = self.appedges[caller]
            for callee in callees:
                if callee in self.appedges and caller in self.appedges[callee]:
                    if callee == caller: continue
                    tuple = (caller,callee) if caller <= callee else (callee,caller)
                    mutualrecursivecalls.add(tuple)

        return mutualrecursivecalls

    def get_recursive_2_cycles(self) -> List[Tuple[int, int, int]]:
        recursive2cycles = []

        for caller in self.appedges:
            callees = self.appedges[caller]
            for callee in callees:
                if callee == caller: continue
                if callee in self.appedges:
                    subcallees = self.appedges[callee]
                    for subcallee in subcallees:
                        if subcallee in self.appedges and caller in self.appedges[subcallee]:
                            recursive2cycles.append((caller,callee,subcallee))

        return recursive2cycles

    def as_dictionary(self) -> Dict[str, List[Tuple[str, ...]]]:
        result: Dict[str, List[Tuple[str, ...]]] = {}
        jd = self.app.jd

        result["recursivecalls"] = [ (str(call), str(jd.get_cms(call))) for call in self.get_self_recursive_calls() ]
        result["mutualrecursivecalls"] = [ (str(caller), str(jd.get_cms(caller)), str(callee), str(jd.get_cms(callee)))
                                            for (caller, callee) in self.get_mutual_recursive_calls() ]
        result["recursive2cycles"] = [ (str(caller), str(jd.get_cms(caller)), str(callee),
                                            str(jd.get_cms(callee)), str(subcallee), str(jd.get_cms(subcallee)))
                                            for (caller, callee, subcallee) in self.get_recursive_2_cycles() ]

        return result

    def to_string(self) -> str:
        jd = self.app.jd

        lines = []

        lines.append('\nDirect recursion: ')
        for c in self.get_self_recursive_calls():
            lines.append(str(jd.get_cms(c)))

        lines.append('\nMutual recursion: ')
        for (caller,callee) in self.get_mutual_recursive_calls():
            caller_cms = jd.get_cms(caller)
            callee_cms = jd.get_cms(callee)
            lines.append('\n' + str(caller_cms) + '\n ==> ' + str(callee_cms))

        lines.append('\nCycle of 2: ')
        for (caller,callee,subcallee) in self.get_recursive_2_cycles():
            caller_cms = jd.get_cms(caller)
            callee_cms = jd.get_cms(callee)
            subcallee_cms = jd.get_cms(subcallee)
            lines.append('\n' + str(caller_cms) + '\n ==> ' + str(callee_cms) + '\n ====> ' + str(subcallee_cms))

        return '\n'.join(lines)


import os
import platform
from avocado import Test
from avocado.utils import cpu, distro, process
from avocado.utils.software_manager.manager import SoftwareManager

class hv_24x7_all_events(Test):

    """
    This tests all hv_24x7 events
    :avocado: tags=perf,24x7,events
    """
    # Initializing fail command list
    fail_cmd = list()

    def setUp(self):
        """
        Setup checks :
        0. Processor should be ppc64.
        1. Perf package
        2. 24x7 is not supported on guest
        3. 24x7 is present
        4. Performance measurement is enabled in LPAR through BMC
        """
        smm = SoftwareManager()
        detected_distro = distro.detect()
        if 'ppc64' not in detected_distro.arch:
            self.cancel("Processor is not PowerPC")
        deps = ['gcc', 'make']
        if 'Ubuntu' in detected_distro.name:
            deps.extend(['linux-tools-common', 'linux-tools-%s'
                         % platform.uname()[2]])
        elif detected_distro.name in ['rhel', 'SuSE', 'fedora', 'centos']:
            deps.extend(['perf', 'numactl'])
        else:
            self.cancel("Install the package for perf supported by %s"
                        % detected_distro.name)
        for package in deps:
            if not smm.check_installed(package) and not smm.install(package):
                self.cancel('%s is needed for the test to be run' % package)

        self.rev = cpu.get_revision()
        perf_args = "perf stat -v -e"
        if self.rev == '004b':
            perf_stat = "%s hv_24x7/HPM_0THRD_NON_IDLE_CCYC" % perf_args
        elif self.rev == '004e':
            perf_stat = "%s hv_24x7/CPM_TLBIE" % perf_args
        elif self.rev == '0080':
            perf_stat = "%s hv_24x7/CPM_TLBIE_FIN" % perf_args
        event_sysfs = "/sys/bus/event_source/devices/hv_24x7"

        # Check if this is a guest
        # 24x7 is not suported on guest
        if "emulated by" in cpu._get_info():
            self.cancel("This test is not supported on guest")

        # Check if 24x7 is present
        if os.path.exists(event_sysfs):
            self.log.info('hv_24x7 present')
        else:
            self.cancel("%s doesn't exist.This test is supported"
                        " only on PowerVM" % event_sysfs)

        # Performance measurement has to be enabled in lpar through BMC
        # Check if its enabled
        result_perf = process.run("%s,domain=2,core=1/ sleep 1"
                                  % perf_stat, ignore_status=True)
        if "operations is limited" in result_perf.stderr.decode("utf-8"):
            self.cancel("Please enable LPAR to allow collecting"
                        " the 24x7 counters info")

        # Getting the number of cores and chips available in the machine
        output = process.run("lscpu")
        for line in output.stdout.decode("utf-8").split('\n'):
            if 'Core(s) per socket:' in line:
                self.cores = int(line.split(':')[1].strip())
            if 'Physical sockets:' in line:
                self.physical_sockets = int(line.split(':')[1].strip())
            if 'Physical chips:' in line:
                self.physical_chips = int(line.split(':')[1].strip())
            # chip = physical socket x physical chip
            self.chip = int(self.physical_sockets*self.physical_chips)

        # Collect all hv_24x7 events
        self.list_of_hv_24x7_events = []
        for lne in process.get_command_output_matching('perf list', 'hv_24x7'):
            lne = lne.split(',')[0].split('/')[1]
            self.list_of_hv_24x7_events.append(lne)

        # Clear the dmesg to capture the delta at the end of the test.
        process.run("dmesg -C", sudo=True)

    def test_all_events(self):
        perf_args = "-v -e"
        for line in self.list_of_hv_24x7_events:
            if line.startswith('HP') or line.startswith('CP'):
                # Running for domain range from 1-6
                for domain in range(2, 7):
                    for core in range(0, self.cores + 1):
                        events = "hv_24x7/%s,domain=%s,core=%s/" % \
                                 (line, domain, core)
                        cmd = 'perf stat %s %s sleep 1' % (perf_args, events)
                        res = process.run(cmd, ignore_status=True)
                        if res.exit_status != 0 or b"not supported" in res.stderr:
                            self.fail_cmd.append(cmd)
            else:
                for chip_item in range(0, self.chip):
                    events = "hv_24x7/%s,chip=%s/" % (line, chip_item)
                    cmd = "perf stat %s %s sleep 1" % (perf_args, events)
                    res = process.run(cmd, ignore_status=True)
                    if res.exit_status != 0 or b"not supported" in res.stderr:
                        self.fail_cmd.append(cmd)

        if len(self.fail_cmd) > 0:
            for cmd in range(len(self.fail_cmd)):
                self.log.info("Failed command: %s" % self.fail_cmd[cmd])
            self.fail("hv_24x7: some of the commands failed, refer to log")

    def tearDown(self):
        # Collect the dmesg
        process.run("dmesg -T")


# coding: utf-8

"""
    Bitbucket API

    Code against the Bitbucket API to automate simple tasks, embed Bitbucket data into your own site, build mobile or desktop apps, or even add custom UI add-ons into Bitbucket itself using the Connect framework.  # noqa: E501

    The version of the OpenAPI document: 2.0
    Contact: support@bitbucket.org
    Generated by: https://openapi-generator.tech
"""

import pprint
import re  # noqa: F401

import six

class IssueAllOfLinks(object):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """

    """
    Attributes:
      openapi_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    openapi_types = {
        'attachments': 'AccountAllOfLinksAvatar',
        'comments': 'AccountAllOfLinksAvatar',
        'html': 'AccountAllOfLinksAvatar',
        '_self': 'AccountAllOfLinksAvatar',
        'vote': 'AccountAllOfLinksAvatar',
        'watch': 'AccountAllOfLinksAvatar'
    }

    attribute_map = {
        'attachments': 'attachments',
        'comments': 'comments',
        'html': 'html',
        '_self': 'self',
        'vote': 'vote',
        'watch': 'watch'
    }

    def __init__(self, attachments=None, comments=None, html=None, _self=None, vote=None, watch=None):  # noqa: E501
        """IssueAllOfLinks - a model defined in OpenAPI"""  # noqa: E501

        self._attachments = None
        self._comments = None
        self._html = None
        self.__self = None
        self._vote = None
        self._watch = None
        self.discriminator = None

        if attachments is not None:
            self.attachments = attachments
        if comments is not None:
            self.comments = comments
        if html is not None:
            self.html = html
        if _self is not None:
            self._self = _self
        if vote is not None:
            self.vote = vote
        if watch is not None:
            self.watch = watch

    @property
    def attachments(self):
        """Gets the attachments of this IssueAllOfLinks.  # noqa: E501

        :return: The attachments of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self._attachments

    @attachments.setter
    def attachments(self, attachments):
        """Sets the attachments of this IssueAllOfLinks.

        :param attachments: The attachments of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self._attachments = attachments

    @property
    def comments(self):
        """Gets the comments of this IssueAllOfLinks.  # noqa: E501

        :return: The comments of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self._comments

    @comments.setter
    def comments(self, comments):
        """Sets the comments of this IssueAllOfLinks.

        :param comments: The comments of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self._comments = comments

    @property
    def html(self):
        """Gets the html of this IssueAllOfLinks.  # noqa: E501

        :return: The html of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self._html

    @html.setter
    def html(self, html):
        """Sets the html of this IssueAllOfLinks.

        :param html: The html of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self._html = html

    @property
    def _self(self):
        """Gets the _self of this IssueAllOfLinks.  # noqa: E501

        :return: The _self of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self.__self

    @_self.setter
    def _self(self, _self):
        """Sets the _self of this IssueAllOfLinks.

        :param _self: The _self of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self.__self = _self

    @property
    def vote(self):
        """Gets the vote of this IssueAllOfLinks.  # noqa: E501

        :return: The vote of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self._vote

    @vote.setter
    def vote(self, vote):
        """Sets the vote of this IssueAllOfLinks.

        :param vote: The vote of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self._vote = vote

    @property
    def watch(self):
        """Gets the watch of this IssueAllOfLinks.  # noqa: E501

        :return: The watch of this IssueAllOfLinks.  # noqa: E501
        :rtype: AccountAllOfLinksAvatar
        """
        return self._watch

    @watch.setter
    def watch(self, watch):
        """Sets the watch of this IssueAllOfLinks.

        :param watch: The watch of this IssueAllOfLinks.  # noqa: E501
        :type: AccountAllOfLinksAvatar
        """

        self._watch = watch

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.openapi_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, IssueAllOfLinks):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


def max2(x):
    """
    返回传入的列表中最大和第二大的元素的值
    :param x: 传入的数列
    :return: 返回最大和第二大的值
    """
    m1, m2 = (x[0], x[1]) if x[0]>x[1] else (x[1], x[0])
    for index in range(2, len(x)):
        if x[index] > m1:
            m2 = m1
            m1 = x[index]
        elif x[index] > m2:
            m2 = x[index]
    return m1, m2

print(max2([2,3,5,23,64,23,42,5]))


from pypet import ComputedLevel, Hierarchy, Dimension, Query, Member
from sqlalchemy.sql import func, extract, cast, select
from sqlalchemy import types

to_char = func.to_char

FORMAT_FUNCTIONS = {
        'year': lambda x: to_char(x, 'YYYY'),
        'month': lambda x: to_char(x, 'YYYY-MM'),
        'day': lambda x: to_char(x, 'YYYY-MM-DD'),
}

class TimeLevel(ComputedLevel):

    def __init__(self, name, column, time_slice=None):
        if time_slice is None:
            time_slice = name

        def partial_trunc(column):
            return func.date_trunc(time_slice, column)

        def partial_extract(column):
            return extract(time_slice, column)
        label_expression = FORMAT_FUNCTIONS.get(time_slice, partial_extract)
        super(TimeLevel, self).__init__(name, column,
                function=partial_trunc, label_expression=label_expression)

    def __getitem__(self, key):
        bind = self.column.table.bind
        values = list(bind.execute(select([
                self.function(cast(key, types.Date)).label('id'),
                self.label_expression(cast(key, types.Date)).label('label')])
                ))[0]
        return Member(self, values.id, values.label)

class TimeDimension(Dimension):

    def __init__(self, name, column, time_levels):
        levels = [TimeLevel(level, column) for level in time_levels]
        super(TimeDimension, self).__init__(name, [Hierarchy('default',
            levels)])