import urllib.parse

import scrapy
from scrapy import Request

class ESMAPIDocsSpider(scrapy.Spider):

    name = "esm_api_docs"
    allowed_domains = []

    def start_requests(self):
        yield Request(
            url=self.url,
            callback=self.parse_help
        )

    def __init__(self, url):

        self.url=url
        self.allowed_domains=[urllib.parse.urlparse(url).hostname]

    def parse_help(self, response):
        """
        Arguments:
        - response: scrapy response object for the help page
        """

        yield dict(
            url=response.url,
            html=response.text
        )

        """ Iterating through the result of get_method_list()"""
        methods = self.get_method_list(response)

        for method in methods:

            yield Request(
                url=method.xpath('@href').get(),
                callback=self.parse_method_or_type
            )

    def get_method_list(self, response):
        """
        Arguments:
        - response: scrapy response object for the help page
        """

        return response.xpath('//div[contains(@class,"commandTitle")]/span/a')

    def parse_method_or_type(self, response):
        """
        Arguments:
        - response: scrapy response object for the method page
        """

        yield dict(
            url=response.url,
            html=response.text
        )

        for esm_type in self.get_type_list(response):
            yield Request(
                url=esm_type.xpath('@href').get(),
                callback=self.parse_method_or_type
            )

    def get_type_list(self, response):
        """
        Arguments:
        - response: scrapy response object for the method page
        """

        return [ t for t in response.xpath('//a') if '/help/types/' in t.xpath('@href').get() ]


import FWCore.ParameterSet.Config as cms

from RecoVertex.V0Producer.generalV0Candidates_cff import *
### CMSSW command line parameter parser
from FWCore.ParameterSet.VarParsing import VarParsing
options = VarParsing('python')

collections_to_keep = cms.untracked.vstring(
    'drop *',
    'keep *_InitialProducer_*_*',
    'keep recoVertexs_offlinePrimaryVertices_*_*',
    'keep recoBeamSpot_offlineBeamSpot_*_*',
    'keep *_genParticles_*_HLT',
    'keep recoVertexCompositeCandidates_generalV0Candidates_*_*',
    'keep recoTracks_lambdaKshortVertexFilter_sParticlesTracks_*',
    'keep recoVertexCompositePtrCandidates_rMassFilter_sVertexCompositePtrCandidate_*',
    'keep recoVertexCompositePtrCandidates_sMassFilter_sVertexCompositePtrCandidate_*',
    'keep *_*_*_SEXAQ',
    "keep *_genParticlesPlusGEANT_*_*",
#    "keep *_g4SimHits_*_*",
    "keep *_simSiPixelDigis_*_*",
#    "keep *_simMuonRPCDigis_*_*",
    "keep *_simSiStripDigis_*_*",
    "keep *_mix_MergedTrackTruth_*",
#    "keep *_siPixelDigis_*_*",
#    "keep *_siStripDigis_*_*",
#    "keep *_siStripDigis_*_*",
    "keep *_siPixelClusters_*_*",
    "keep *_siStripClusters_*_*",
    "keep *_generalTracks_*_*"
  )

## data or MC options
options.register(
	'isData',True,VarParsing.multiplicity.singleton,VarParsing.varType.bool,
	'flag to indicate data or MC')

options.register(
	'maxEvts',-1,VarParsing.multiplicity.singleton,VarParsing.varType.int,
	'flag to indicate max events to process')

process = cms.Process("SEXAQ")

process.load("FWCore.MessageService.MessageLogger_cfi")
process.MessageLogger.cerr.FwkReport.reportEvery = cms.untracked.int32(10000)
process.options = cms.untracked.PSet(wantSummary = cms.untracked.bool(True))

process.load('Configuration.StandardSequences.GeometryRecoDB_cff')
process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")

process.load('Configuration/StandardSequences/MagneticField_38T_cff')
process.load('Configuration/StandardSequences/Reconstruction_cff')
process.load('Configuration/EventContent/EventContent_cff')

from Configuration.AlCa.GlobalTag import GlobalTag

if(options.isData==True):
    process.GlobalTag = GlobalTag(process.GlobalTag, '80X_dataRun2_2016SeptRepro_v7', '') #was using 80X_dataRun2_2016SeptRepro_v7 before trialM, now changed to this one according to: https://twiki.cern.ch/twiki/bin/viewauth/CMS/PdmVAnalysisSummaryTable
else:
    process.GlobalTag = GlobalTag(process.GlobalTag, '80X_mcRun2_asymptotic_2016_miniAODv2_v1', '')

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(options.maxEvts))

process.source = cms.Source("PoolSource",
        fileNames = cms.untracked.vstring(options.inputFiles),
  duplicateCheckMode = cms.untracked.string ("noDuplicateCheck")
)

process.nEvTotal        = cms.EDProducer("EventCountProducer")
process.nEvLambdaKshort = cms.EDProducer("EventCountProducer")
process.nEvLambdaKshortVertex = cms.EDProducer("EventCountProducer")
process.nEvSMass        = cms.EDProducer("EventCountProducer")

process.genParticlePlusGEANT = cms.EDProducer("GenPlusSimParticleProducer",
  src           = cms.InputTag("g4SimHits"),
  setStatus     = cms.int32(8),                # set status = 8 for GEANT GPs
  particleTypes = cms.vstring("Xi-","Xibar+","Lambda0","Lambdabar0","K_S0","K0"),      # also picks pi- (optional)
  filter        = cms.vstring("pt >= 0.0"),     # just for testing
  genParticles  = cms.InputTag("genParticles") # original genParticle list
)

process.load("RecoVertex.V0Producer.generalV0Candidates_cfi")
process.generalV0Candidates.innerHitPosCut = -1
process.generalV0Candidates.cosThetaXYCut = -1
process.generalV0Candidates.kShortMassCut = 0.03
process.generalV0Candidates.lambdaMassCut = 0.015

process.load("SexaQAnalysis.Skimming.LambdaKshortFilter_cfi")
process.lambdaKshortFilter.genCollection = cms.InputTag("genParticlePlusGEANT")
process.lambdaKshortFilter.isData = True
process.lambdaKshortFilter.minPtLambda = 0.
process.lambdaKshortFilter.minPtKshort = 0.
process.lambdaKshortFilter.checkLambdaDaughters = True
process.lambdaKshortFilter.prescaleFalse = 0

process.load("SexaQAnalysis.Skimming.LambdaKshortVertexFilter_cfi")
process.lambdaKshortVertexFilter.lambdaCollection = cms.InputTag("lambdaKshortFilter","lambda")
process.lambdaKshortVertexFilter.kshortCollection = cms.InputTag("lambdaKshortFilter","kshort")
process.lambdaKshortVertexFilter.maxchi2ndofVertexFit = 10.

from SexaQAnalysis.Skimming.MassFilter_cfi import massFilter
massFilter.lambdakshortCollection = cms.InputTag("lambdaKshortVertexFilter","sParticles")
massFilter.minMass = -10000 # effectively no filter
massFilter.maxMass = 10000  # effectively no filter
process.rMassFilter = massFilter.clone()
process.rMassFilter.targetMass = 0
process.sMassFilter = massFilter.clone()
process.sMassFilter.targetMass = 0.939565

process.load("SexaQAnalysis.Skimming.InitialProducer_cfi")

process.load("SexaQAnalysis.TreeProducer.Treeproducer_AOD_cfi")

process.p = cms.Path(
  process.generalV0Candidates*
  process.tree*
  process.nEvTotal *
  process.InitialProducer *
  process.lambdaKshortFilter *
  process.nEvLambdaKshort *
  process.lambdaKshortVertexFilter *
  process.nEvLambdaKshortVertex *
  process.rMassFilter *
  process.sMassFilter *
  process.nEvSMass
)

# Output --> not used in the analyzer
process.TFileService = cms.Service('TFileService',
    fileName = cms.string('preFilterInfo.root'),
)

#Keep edm output file --> used in the analyzer
process.out = cms.OutputModule("PoolOutputModule",
  outputCommands = collections_to_keep,
   fileName = cms.untracked.string("events_skimmed.root"),
    SelectEvents = cms.untracked.PSet(
    SelectEvents = cms.vstring('p')
  )
)

process.output_step = cms.EndPath(process.out)

#iFileName = "configDump_cfg.py"
#file = open(iFileName,'w')
#file.write(str(process.dumpPython()))
#file.close()


import math
t=int(input( ))
while(t>0):
	n=int(input( ))
	count=5

	for i in range(4,n+1):
		if i>1:
			for j in range(2,int(math.sqrt(i))+1):
				if i%j==0:
					break

			else:
				count=count+i

	print(count)
	t-=1


# -*- coding: utf-8 -*-
'''
Do something with related models
'''

from django.contrib.admin.options import ModelAdmin, IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList
from django.core.paginator import InvalidPage

def get_related_flat(queryset, related_field='parent', related_root=None, with_level=False):
    """
    Return iterator over self related models tree as one-dimentional list
    @param related_field: Related field name
    @param related_root: Related field root value
    @param with_level: Returns list of tuple, where 1st var is model instance and 2d - depth level
    """
    def get_related(qs, p, deepth=0):
        for row in qs.filter(**{related_field: p}):
            yield (row, deepth) if with_level else row
            for r in get_related(qs, row, deepth + 1):
                yield r

    return get_related(queryset, related_root)


# -*- cording: utf-8 -*-
# Created by Kim.youngjae at 2018/12/03

import os
import math
import numpy as np
import cv2
import opencv_knn as knn

# module level variables ##########################################################################

kNearest = cv2.ml.KNearest_create()

# constants for checkIfPossibleChar, this checks one possible char only (does not compare to another char)
MIN_PIXEL_WIDTH = 2
MIN_PIXEL_HEIGHT = 8

MIN_ASPECT_RATIO = 0.25
MAX_ASPECT_RATIO = 1.0

MIN_PIXEL_AREA = 80

# constants for comparing two chars
MIN_DIAG_SIZE_MULTIPLE_AWAY = 0.3
MAX_DIAG_SIZE_MULTIPLE_AWAY = 5.0

MAX_CHANGE_IN_AREA = 0.5

MAX_CHANGE_IN_WIDTH = 0.8
MAX_CHANGE_IN_HEIGHT = 0.2

MAX_ANGLE_BETWEEN_CHARS = 12.0

# other constants
MIN_NUMBER_OF_MATCHING_CHARS = 3

RESIZED_CHAR_IMAGE_WIDTH = 20
RESIZED_CHAR_IMAGE_HEIGHT = 30

MIN_CONTOUR_AREA = 100

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

###################################################################################################
class PossibleChar:

    # constructor #################################################################################
    def __init__(self, _contour):
        self.contour = _contour

        self.boundingRect = cv2.boundingRect(self.contour)

        [intX, intY, intWidth, intHeight] = self.boundingRect

        self.intBoundingRectX = intX
        self.intBoundingRectY = intY
        self.intBoundingRectWidth = intWidth
        self.intBoundingRectHeight = intHeight

        self.intBoundingRectArea = self.intBoundingRectWidth * self.intBoundingRectHeight

        self.intCenterX = (self.intBoundingRectX + self.intBoundingRectX + self.intBoundingRectWidth) / 2
        self.intCenterY = (self.intBoundingRectY + self.intBoundingRectY + self.intBoundingRectHeight) / 2

        self.fltDiagonalSize = math.sqrt((self.intBoundingRectWidth ** 2) + (self.intBoundingRectHeight ** 2))

        self.fltAspectRatio = float(self.intBoundingRectWidth) / float(self.intBoundingRectHeight)
    # end constructor

# end class

###################################################################################################
def checkIfPossibleChar(possible_char):
    # this function is a 'first pass' that does a rough check on a contour to see if it could be a char,
    # note that we are not (yet) comparing the char to other chars to look for a group
    if (possible_char.intBoundingRectArea > MIN_PIXEL_AREA and
            possible_char.intBoundingRectWidth > MIN_PIXEL_WIDTH and possible_char.intBoundingRectHeight > MIN_PIXEL_HEIGHT and
            MIN_ASPECT_RATIO < possible_char.fltAspectRatio and possible_char.fltAspectRatio < MAX_ASPECT_RATIO):
        return True
    else:
        return False
    # end if

# end function

def find_possible_chars_in_box(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_hist = cv2.equalizeHist(img_gray)

    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(10, 10))
    img_clahe = clahe.apply(img_gray)

    # Set threshold and maxValue
    thresh = 130  # 175
    maxValue = 255  # 255
    # apply thresholding which helps for finding lines
    th, img_thresh = cv2.threshold(img_clahe, thresh, maxValue, cv2.THRESH_BINARY_INV)

    kernel = np.ones((3, 3), np.uint8)
    img_dilate = cv2.dilate(img_thresh, kernel, 1)
    _, contours, _ = cv2.findContours(img_dilate,
                                      cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE
                                      )
    # sort the contours by their x-axis position, ensuring
    # that we read the numbers from left to right
    contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])

    # test for draw all find contours
    # cv2.drawContours(img, contours, -1, (255, 0, 0), 3)

    list_possible_chars = []
    num = 0
    saveNpz = False
    FNAME = 'digits.npz'
    train, train_labels = knn.loadtraindata(FNAME)
    for i in contours:
        (x, y, w, h) = cv2.boundingRect(i)
        if (w >= 5 and w <= 20) and (h >= 20 and h <= 40):
            list_possible_chars.append(i)
            img_num = img_thresh[y:y + h, x:x + w]

            train, train_labels = knn.loadtraindata(FNAME)
            saveNpz = False
            test = knn.resize20(img_num)
            result = knn.checkDigit(test, train, train_labels)
            result = int(result[0][0])

            print(result)

            num += 1
            # cv2.imwrite('images/num{}.png'.format(num), img_num)
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)

            cv2.putText(img, str(result), (x, y - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)

            # save training
            # k = cv2.waitKey(0)
            # if k > 47 and k < 58:
            #     saveNpz = True
            #     train = np.append(train, test, axis=0)
            #     newLabel = np.array(int(chr(k))).reshape(-1, 1)
            #     train_labels = np.append(train_labels, newLabel, axis=0)
            #
            # if saveNpz:
            #     np.savez(FNAME, train=train, train_labels=train_labels)

    # cv2.destroyAllWindows()
    if saveNpz:
        np.savez(img_num, train=train, train_labels=train_labels)
    return img

# print(x, y, w, h)
# print(cv2.contourArea(i))

# test
# cv2.imwrite('img.jpg', img)
# cv2.imwrite('img_gray.jpg', img_gray)
# cv2.imwrite('img_hist.jpg', img_hist)
# cv2.imwrite('img_clahe.jpg', img_clahe)
# cv2.imwrite('img_thresh.jpg', img_thresh)
# cv2.imwrite('img_img_dilate.jpg', img_dilate)

# contours = []
#
# _, contours, _ = cv2.findContours(img_thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#
# for contour in contours:
#     possible_char = PossibleChar(contour)
#
#     if checkIfPossibleChar(possible_char):
#         list_possible_chars.append(possible_char)
#
# print(list_possible_chars)
# return list_possible_chars, img_gray, img_thresh

if __name__ == '__main__':
    cwd = os.getcwd()
    file = 'img_meterbox.jpg'
    loc_file = os.path.join(cwd, file)
    img = cv2.imread(loc_file)
    find_possible_chars_in_box(img)
    cv2.imshow("frame", img)

    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


import pygame
import random
import math
import physEnv as env
pygame.init()

# random() - returns a random number between 0.0 and 1.0
# uniform(a, b) - returns a random number between a and b
# randint(a, b) - returns a random integer between a and b
# choice(list) - returns a random element from the list
# shuffle(list) - rearranges the order of the list
pygame.mouse.get_pos()

run = True
width = 500
height = 500
gravity = [math.pi, 0.45]
elasticity = 0.75

pygame.display.set_caption("hello")
win = pygame.display.set_mode((width, height))

clock = pygame.time.Clock()
selected_particle = None

particles = env.addParticle(20, 500, 500)

def display(self):
    pygame.draw.circle(win, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)

while run is True:

    clock.tick(30)
    win.fill((0, 0, 0))
    keys= pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            (mouseX, mouseY) = pygame.mouse.get_pos()
            selected_particle = env.find(particles, mouseX, mouseY)
        elif event.type == pygame.MOUSEBUTTONUP:
            selected_particle = None

    if selected_particle:
        (mouseX, mouseY) = pygame.mouse.get_pos()
        selected_particle.colour = (255, 0, 0)
        dx = mouseX - selected_particle.x
        dy = mouseY - selected_particle.y
        selected_particle.angle = 0.5*math.pi + math.atan2(dy, dx)
        selected_particle.speed = math.hypot(dx, dy) * 0.1
    else:
        for i in range(len(particles)):
            particles[i].colour = (255, 255, 255)

    for i in range(len(particles)):
        env.Particle.move(particles[i])
        display(particles[i])
        env.Particle.bounce(particles[i])
        env.collide(particles)
    pygame.display.flip()

pygame.quit


import tensorflow as tf
import numpy as np

TRAIN_SIZE = 45000
VALIDATION_SIZE = 5000

class Cifar:

    def __init__(self, one_hot=True):
        """
        x_train.shape = (45000, 32, 32)
        y_train.shape = (45000, )
        x_valid.shape = (5000, 32, 32)
        y_valid.shape = (5000, )
        x_test.shape  = (10000, 32, 32)
        y_test.shape  = (10000, )
        """
        (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
        x_train, x_test = self.normalized(x_train, x_test)
        rand_indices = np.random.permutation(TRAIN_SIZE + VALIDATION_SIZE)
        train_indices, validation_indices = rand_indices[:TRAIN_SIZE], rand_indices[TRAIN_SIZE:]
        self.x_train = x_train[train_indices]
        self.x_valid = x_train[validation_indices]
        self.x_test = x_test

        # params for batch func
        self._index_in_epoch = 0
        self._epochs_completed = 0
        if one_hot:
            self.y_train = np.reshape(self.one_hot(y_train[train_indices], 10), (-1, 10))
            self.y_valid = np.reshape(self.one_hot(y_train[validation_indices], 10), (-1, 10))
            self.y_test = np.reshape(self.one_hot(y_test, 10), (-1, 10))
        else:
            self.y_train = y_train[train_indices]
            self.y_valid = y_train[validation_indices]
            self.y_test = y_test

    @staticmethod
    def one_hot(targets, nb_classes):
        res = np.eye(nb_classes)[np.array(targets).reshape(-1)]
        return res.reshape(list(targets.shape) + [nb_classes])

    def next_batch(self, batch_size, shuffle=True):
        start = self._index_in_epoch
        # This block is useless since train set is randomly assigned during instantiation
        # if self._epochs_completed == 0 and start == 0 and shuffle:
        #     perm0 = np.arange(TRAIN_SIZE)
        #     np.random.permutation(perm0)
        #     self.x_train = self.x_train[perm0]
        #     self.y_train = self.y_train[perm0]
        # Go to the next epoch
        if start + batch_size > TRAIN_SIZE:
            # Finished epoch
            self._epochs_completed += 1
            # Get the rest examples in this epoch
            rest_num_x = TRAIN_SIZE - start
            x_rest_part = self.x_train[start:TRAIN_SIZE]
            y_rest_part = self.y_train[start:TRAIN_SIZE]
            if shuffle:
                perm = np.arange(TRAIN_SIZE)
                np.random.shuffle(perm)
                self.x_train = self.x_train[perm]
                self.y_train = self.y_train[perm]
            start = 0
            self._index_in_epoch = batch_size - rest_num_x
            end = self._index_in_epoch
            x_new_part = self.x_train[start:end]
            y_new_part = self.y_train[start:end]
            return np.concatenate((x_rest_part, x_new_part), axis=0), np.concatenate(
                (y_rest_part, y_new_part), axis=0)

        else:
            self._index_in_epoch += batch_size
            end = self._index_in_epoch
            return self.x_train[start:end], self.y_train[start:end]

    @staticmethod
    def normalized(train, test):
        norm_train = train.astype('float32') / 255
        norm_test = test.astype('float32') / 255
        return norm_train, norm_test


'''Question:
Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.
Suppose the following input is supplied to the program:
hello world and practice makes perfect and hello world again
Then, the output should be:
again and hello makes perfect practice world'''

palavras = input("Digite as palavras separadas por espaços: ")

lista = palavras.split(" ")

listaResultante = []

for item in lista:
    if not item in listaResultante:
        listaResultante.append(item)

listaResultante.sort()

for item in listaResultante:
    print(item, end=" ")


from asgiref.sync import async_to_sync

from django.dispatch import receiver
from django.db.models.signals import post_save, m2m_changed

from channels.layers import get_channel_layer

from game.models import Session
from game.serializers import SessionSerializer

@receiver(post_save, sender=Session)
def session_changed(sender, instance, *args, **kwargs):
    channel_layer = get_channel_layer()
    session_id = instance.session_id
    group_name = f"session-{session_id}"

    serializer = SessionSerializer(instance)

    event = {
        "type": "session_data",
        "session": serializer.data,
    }

    async_to_sync(channel_layer.group_send)(
        group_name,
        event
    )

@receiver(m2m_changed, sender=Session.players.through)
def session_players_changed(sender, instance, *args, **kwargs):
    if kwargs["action"] == "pre_add":
        print(f"Added into: {instance}")

        players = instance.players.all()
        if len(players) == 0:
            print("no players. adding 1st")
        elif len(players) < 5:
            print("not capacity")
        elif len(players) >= 5:
            print("Room is at capacity")

    if kwargs["action"] == "post_remove":
        players = instance.players.all()

        if len(players) == 0:
            print("no players left. removing")

            try:
                instance.delete()
            except:
                print("Delete failed")

    channel_layer = get_channel_layer()
    session_id = instance.session_id
    group_name = f"session-{session_id}"

    serializer = SessionSerializer(instance)

    event = {
        "type": "session_data",
        "session": serializer.data,
    }

    async_to_sync(channel_layer.group_send)(
        group_name,
        event
    )


from django.conf.urls import url
from django.urls import path

from . import views

urlpatterns = [
    path('', views.Index.as_view(), name='index'),
    # url(r'^categories/', views.CategoryListView.as_view()),
    url(r'^categories/', views.CategoryListView.as_view()),
    url(r'^items/(?P<pk>\d+)/delete$', views.ItemDestroyView.as_view()),
    url(r'^items/(?P<pk>\d+)/update$', views.ItemUpdateView.as_view()),
    url(r'^items/(?P<pk>\d+)$', views.ItemDetailView.as_view()),
    url(r'^items/', views.ItemListView.as_view()),
]


class Node:
    def __init__(self, value=None, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

    def insert_node(self, value):
        if self.value:
            if value < self.value:
                if self.left is None:
                    self.left = Node(value=value)
                else:
                    self.left.insert_node(value=value)
            elif value > self.value:
                if self.right is None:
                    self.right = Node(value=value)
                else:
                    self.right.insert_node(value=value)
        else:
            self.value = value

    def print_tree(self):
        if self.left:
            self.left.print_tree()
        print(self.value, end=" ")
        if self.right:
            self.right.print_tree()

    def is_exist(self, value):
        if self.value == value:
            return True
        elif value < self.value:
            if self.left:
                return False or self.left.is_exist(value)
            else:
                return False
        else:
            if self.right:
                return False or self.right.is_exist(value)
            else:
                return False

import time
root = Node()
for i in range(500,0,-2):
    root.insert_node(i)
for i in range(499,0,-2):
    root.insert_node(i)
root.print_tree()
s = time.perf_counter()
root.is_exist(399)

print(time.perf_counter() - s )


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Nov 18, 2011

@author: 章红兵
"""
from os import path
from scrapy import signals
from scrapy.xlib.pydispatch import dispatcher
import codecs
import time
class FilePipeline(object):
    #   保存数据的文件名
    filename = '360buy_news_%s.txt' % time.strftime('%Y%m%d')
    def __init__(self):
        self.f = None
        dispatcher.connect(self.open, signals.engine_started)
        dispatcher.connect(self.close, signals.engine_stopped)

    def process_item(self,item,spider):
        # 从对象中提取数据
        subject = item['subject'][0]
        url = item['url'][0]
        created = item['created'][0]
        modified = item['modified'][0]
        url = item['url'][0]

        sql = "INSERT INTO think_discountnews (subject, url, created,modified) \
                VALUES ('%s', %s, '%s', '%s', '%s');\n" % (subject, url, created, modified )

        #把数据写入文件
#        self.f.write(sql + '\n')

        with codecs.open(self.filename, 'a+', 'utf-8') as f:
            f.write(sql.replace('[]',''))
        return item

    def open(self):
        # 当文件存在只写
        if path.exists(self.filename):
            self.f = open(self.filename, 'a')
        else:
            #当文件不存在创建后再写
            self.f = open(self.filename, 'w')

    def close(self):
        self.f.close() if self.f is not None else None


# coding: utf-8
#
# Copyright (c) 2010, Logica
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <ORGANIZATION> nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
# Created on 14 Sep 2010
# @author enmarkp
import re
from datetime import datetime
from genshi.filters.transform import Transformer
from genshi.template.loader import TemplateLoader
from pkg_resources import resource_filename
from trac.util.datefmt import to_utimestamp, utc
from trac.config import ListOption
from trac.core import implements, Component
from trac.web.api import IRequestHandler, ITemplateStreamFilter
from trac.env import IEnvironmentSetupParticipant
from trac.db.schema import Table, Column
from trac.db.api import DatabaseManager
from trac.web.chrome import ITemplateProvider, add_javascript, add_stylesheet, Chrome
from trac.util.translation import _
from trac.admin.api import IAdminPanelProvider

class Feedback(Component):
    realms = ListOption('feedback', 'realms', '*',
                        doc="Show feedback option in these realms")
    implements(ITemplateStreamFilter, IRequestHandler, IAdminPanelProvider,
               IEnvironmentSetupParticipant, ITemplateProvider)

    _schema = [Table('project_feedback', key='id')[
                   Column('id', type='int', auto_increment=True),
                   Column('author'),
                   Column('feedback'),
                   Column('path'),
                   Column('created', type='int64'),
                   Column('modified', type='int64')]]

    # IAdminPanelProvider

    def get_admin_panels(self, req):
        if req.perm.has_permission('TRAC_ADMIN'):
            yield ('feedback', _('Feedback'), 'index', _('Feedback'))

    def render_admin_panel(self, req, category, page, path_info):
        feedback = self._get_feedback_list(req)
        return 'feedback.html', dict(feedback=feedback)

    # ITemplateStreamFilter

    def filter_stream(self, req, method, filename, stream, data):
        if req.authname == 'anonymous':
            return stream
        add_stylesheet(req, 'feedback/feedback.css')
        add_javascript(req, 'feedback/feedback.js')
        Chrome(self.env).add_wiki_toolbars(req)
        tmpl = TemplateLoader(self.get_templates_dirs()).load('feedback-box.html')
        feedbackbox = tmpl.generate(req=req)
        stream |= Transformer('//div[@id="footer"]').append(feedbackbox)
        return stream

    # IRequestHandler
    _url_re = re.compile(r'^/ajax/feedback(?:/(\d+))?/?$')
    def match_request(self, req):
        if req.authname == 'anonymous' or not req.method == 'POST':
            return False
        m = self._url_re.search(req.path_info)
        if m is None:
            return False
        id_ = m.group(1)
        req.args['feedback_id'] = id_ and int(id_) or None
        return True

    def process_request(self, req):
        if req.method == 'POST':
            msg = self._update_feedback(req)
            req.send('{"message":"%s"}' % msg, 'text/json')
        else:
            feedback = self._get_feedback_list(req)
            return 'feedback.html', dict(feedback=feedback), None

    def _get_feedback_list(self, req):
        db = self.env.get_read_db()
        cursor = db.cursor()
        if req.perm.has_permission('TRAC_ADMIN'):
            cursor.execute('SELECT * FROM project_feedback ORDER BY created DESC')
        else:
            cursor.execute('SELECT * FROM project_feedback '
                           'WHERE author=%s ORDER BY created DESC', (req.authname,))
        return list(cursor)

    def _update_feedback(self, req):
        author = req.authname
        action = req.args.get('action', 'create')
        msg = None
        if action == 'create':
            feedback = req.args['feedback']
            path = req.args['path']
            created = modified = to_utimestamp(datetime.now(utc))
            @self.env.with_transaction()
            def add_feedback(db):
                cursor = db.cursor()
                self.log.debug('INSERT INTO project_feedback ('
                               'author, feedback, path, created, modified'
                               ") VALUES ('%s', '%s', '%s', '%s', '%s')",
                               author, feedback, path, created, modified)
                cursor.execute('INSERT INTO project_feedback ('
                               'author, feedback, path, created, modified'
                               ') VALUES (%s, %s, %s, %s, %s)',
                               (author, feedback, path, created, modified))
            msg = _("Your feedback has been received, thank you")
        elif action == 'delete':
            id_ = req.args['feedback_id']
            @self.env.with_transaction()
            def del_feedback(db):
                cursor = db.cursor()
                if req.perm.has_permission('TRAC_ADMIN'):
                    cursor.execute('DELETE FROM project_feedback '
                                   'WHERE id=%s', (id_,))
                else:
                    cursor.execute('DELETE FROM project_feedback '
                                   'WHERE id=%s AND author=%s',
                                   (id_, author))
            msg = _("Feedback item %s was deleted") % id_
        return msg

    # IEnvironmentSetupParticipant

    def environment_created(self):
        @self.env.with_transaction()
        def try_upgrade(db=None):
            self.upgrade_environment(db)

    def environment_needs_upgrade(self, db):
        try:
            @self.env.with_transaction()
            def check(db):
                sql = ('SELECT id, author, feedback, path, created, '
                       'modified FROM project_feedback LIMIT 1')
                cursor = db.cursor()
                cursor.execute(sql)
                cursor.fetchone()
        except Exception:
            self.log.debug("Upgrade of schema needed for feedback plugin", exc_info=True)
            return True
        else:
            return False

    def upgrade_environment(self, db):
        self.log.debug("Upgrading schema for feedback plugin")
        db_backend, _ = DatabaseManager(self.env).get_connector()
        cursor = db.cursor()
        for table in self._schema:
            for stmt in db_backend.to_sql(table):
                self.log.debug(stmt)
                cursor.execute(stmt)

    # ITemplateProvider

    def get_htdocs_dirs(self):
        return [('feedback', resource_filename(__name__, 'htdocs'))]

    def get_templates_dirs(self):
        return [resource_filename(__name__, 'templates')]


# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging
import time
from uuid import uuid4

from zuul.driver import Driver, TriggerInterface
from zuul.driver.zuul.zuulmodel import ZuulTriggerEvent
from zuul.driver.zuul import zuultrigger
from zuul.lib.logutil import get_annotated_logger

PARENT_CHANGE_ENQUEUED = 'parent-change-enqueued'
PROJECT_CHANGE_MERGED = 'project-change-merged'

class ZuulDriver(Driver, TriggerInterface):
    name = 'zuul'
    log = logging.getLogger("zuul.ZuulTrigger")

    def __init__(self):
        self.parent_change_enqueued_events = {}
        self.project_change_merged_events = {}

    def registerScheduler(self, scheduler):
        self.sched = scheduler

    def reconfigure(self, tenant):
        for pipeline in tenant.layout.pipelines.values():
            for ef in pipeline.manager.event_filters:
                if not isinstance(ef.trigger, zuultrigger.ZuulTrigger):
                    continue
                if PARENT_CHANGE_ENQUEUED in ef._types:
                    # parent-change-enqueued events need to be filtered by
                    # pipeline
                    for pipeline in ef._pipelines:
                        key = (tenant.name, pipeline)
                        self.parent_change_enqueued_events[key] = True
                elif PROJECT_CHANGE_MERGED in ef._types:
                    self.project_change_merged_events[tenant.name] = True

    def onChangeMerged(self, tenant, change, source):
        # Called each time zuul merges a change
        if self.project_change_merged_events.get(tenant.name):
            try:
                self._createProjectChangeMergedEvents(change, source)
            except Exception:
                self.log.exception(
                    "Unable to create project-change-merged events for "
                    "%s" % (change,))

    def onChangeEnqueued(self, tenant, change, pipeline, event):
        log = get_annotated_logger(self.log, event)

        # Called each time a change is enqueued in a pipeline
        tenant_events = self.parent_change_enqueued_events.get(
            (tenant.name, pipeline.name))
        log.debug("onChangeEnqueued %s", tenant_events)
        if tenant_events:
            try:
                self._createParentChangeEnqueuedEvents(
                    change, pipeline, tenant, event)
            except Exception:
                log.exception(
                    "Unable to create parent-change-enqueued events for "
                    "%s in %s" % (change, pipeline))

    def _createProjectChangeMergedEvents(self, change, source):
        changes = source.getProjectOpenChanges(
            change.project)
        for open_change in changes:
            self._createProjectChangeMergedEvent(open_change)

    def _createProjectChangeMergedEvent(self, change):
        event = ZuulTriggerEvent()
        event.type = PROJECT_CHANGE_MERGED
        event.trigger_name = self.name
        event.project_hostname = change.project.canonical_hostname
        event.project_name = change.project.name
        event.change_number = change.number
        event.branch = change.branch
        event.change_url = change.url
        event.patch_number = change.patchset
        event.ref = change.ref
        event.zuul_event_id = str(uuid4().hex)
        event.timestamp = time.time()
        self.sched.addEvent(event)

    def _createParentChangeEnqueuedEvents(self, change, pipeline, tenant,
                                          event):
        log = get_annotated_logger(self.log, event)

        log.debug("Checking for changes needing %s:" % change)
        if not hasattr(change, 'needed_by_changes'):
            log.debug("  %s does not support dependencies" % type(change))
            return

        # This is very inefficient, especially on systems with large
        # numbers of github installations.  This can be improved later
        # with persistent storage of dependency information.
        needed_by_changes = set(change.needed_by_changes)
        for source in self.sched.connections.getSources():
            log.debug("  Checking source: %s", source)
            needed_by_changes.update(
                source.getChangesDependingOn(change, None, tenant))
        log.debug("  Following changes: %s", needed_by_changes)

        for needs in needed_by_changes:
            self._createParentChangeEnqueuedEvent(needs, pipeline)

    def _createParentChangeEnqueuedEvent(self, change, pipeline):
        event = ZuulTriggerEvent()
        event.type = PARENT_CHANGE_ENQUEUED
        event.trigger_name = self.name
        event.pipeline_name = pipeline.name
        event.project_hostname = change.project.canonical_hostname
        event.project_name = change.project.name
        event.change_number = change.number
        event.branch = change.branch
        event.change_url = change.url
        event.patch_number = change.patchset
        event.ref = change.ref
        event.zuul_event_id = str(uuid4().hex)
        self.sched.addEvent(event)

    def getTrigger(self, connection_name, config=None):
        return zuultrigger.ZuulTrigger(self, config)

    def getTriggerSchema(self):
        return zuultrigger.getSchema()


# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from azure.identity import DefaultAzureCredential
from azure.mgmt.netapp import NetAppManagementClient

"""
# PREREQUISITES
    pip install azure-identity
    pip install azure-mgmt-netapp
# USAGE
    python volume_groups_create.py

    Before run the sample, please set the values of the client ID, tenant ID and client secret
    of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
    AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
    https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""

def main():
    client = NetAppManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id="D633CC2E-722B-4AE1-B636-BBD9E4C60ED9",
    )

    response = client.volume_groups.begin_create(
        resource_group_name="myRG",
        account_name="account1",
        volume_group_name="group1",
        body={
            "location": "westus",
            "properties": {
                "groupMetaData": {
                    "applicationIdentifier": "DEV",
                    "applicationType": "SAP-HANA",
                    "deploymentSpecId": "20542149-bfca-5618-1879-9863dc6767f1",
                    "groupDescription": "Volume group",
                },
                "volumes": [
                    {
                        "name": "test-data-mnt00001",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-data-mnt00001",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "data",
                        },
                    },
                    {
                        "name": "test-log-mnt00001",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-log-mnt00001",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "log",
                        },
                    },
                    {
                        "name": "test-shared",
                        "properties": {
                            "capacityPoolResourceId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRG/providers/Microsoft.NetApp/netAppAccounts/account1/capacityPools/pool1",
                            "creationToken": "test-shared",
                            "proximityPlacementGroup": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/cys_sjain_fcp_rg/providers/Microsoft.Compute/proximityPlacementGroups/svlqa_sjain_multivolume_ppg",
                            "serviceLevel": "Premium",
                            "subnetId": "/subscriptions/d633cc2e-722b-4ae1-b636-bbd9e4c60ed9/resourceGroups/myRP/providers/Microsoft.Network/virtualNetworks/testvnet3/subnets/testsubnet3",
                            "throughputMibps": 10,
                            "usageThreshold": 107374182400,
                            "volumeSpecName": "shared",
                        },
                    },
                ],
            },
        },
    ).result()
    print(response)

# x-ms-original-file: specification/netapp/resource-manager/Microsoft.NetApp/stable/2022-11-01/examples/VolumeGroups_Create.json
if __name__ == "__main__":
    main()


import math

def euclidean(a, b):
    q_list = []
    r0, r1 = b, a
    while r0 != 0:
        q = r1 // r0
        q_list.append(q)
        r1, r0 = r0, r1 - q * r0

    return q_list

def get_kd(q_list):
    kd_list = []
    for i in range(len(q_list)):
        q_new_list = q_list[0:i]
        if (len(q_new_list) == 0):
            # neglect 0/1 since it's impossible to reveal d
            continue

        k = 1
        d = q_new_list[-1]
        for j in range(-2, -len(q_new_list)-1, -1):
            k, d = d, q_new_list[j] * d + k

        kd_list.append((k, d))

    return kd_list

def attack(kd_list, e, n):
    for k, d in kd_list:
        phi = (e * d - 1) / k
        b = - (n - phi + 1)
        c = n
        # solve x^2 + bx + c = 0
        delta = b**2 - 4 * c
        x1 = 1/2 * ((-b) + math.sqrt(delta))
        x2 = 1/2 * ((-b) - math.sqrt(delta))
        if x1.is_integer() and x2.is_integer():
            print("Success!")
            print("<p, q> = <%d, %d>" % (x1, x2))
            print("d = %d" % d)
            return d

    print("failed")
    return 0

if __name__ == "__main__":
    e, n = 77537081, 317940011
    print("Try to decrypt <e, n> = <%d, %d>" % (e, n))

    q_list = euclidean(n, e)
    kd_list = get_kd(q_list)
    print(q_list)
    print(kd_list)
    attack(kd_list, e, n)


"""oj URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import include, url

urlpatterns = [
    url(r"^api/", include("demo.urls.oj")),
    url(r"^teacher/lab/", include("lab.urls.admin")),
    url(r"^teacher/submission/", include("submission.urls.admin")),
    url(r"^api", include("lecture.urls.admin")),
    url(r"^administrator/role/", include("user.urls.admin")),
    url(r"^exam/student", include("exam.urls.admin")),
]


from datetime import datetime

from flask_restplus import Resource, fields
from flask import request, current_app
from app.extensions import api

from app.api.mines.mine.models.mine import Mine
from app.api.mines.permits.permit.models.permit import Permit

from app.api.utils.access_decorators import requires_role_view_all

VERIFY_PERMIT_MINE_MODEL = api.model(
    'VerifyPermitMineModel', {
        'a_Result': fields.String,
        'a_MineInfo': fields.String,
        'a_ResponseMessage': fields.String,
        'a_Timestamp': fields.DateTime
    })

class VerifyPermitMineResource(Resource):
    @api.doc(
        description=
        'Verifies that a permit is valid for a mine based on the type of deemed authorization. NOTE: This exists for integration purposes and does not follow the typical patterns of this API.',
        params={
            'a_PermitNumber': f'The permit number.',
            'a_TypeofDeemedAuth': f'The type of deemed authorization, eg INDUCED.'
        })
    @api.marshal_with(VERIFY_PERMIT_MINE_MODEL, code=200)
    @requires_role_view_all
    def get(self):
        result = ""
        mine_info = ""
        response_message = ""

        try:
            permit_no = request.args.get('a_PermitNumber')
            permit_prefix = permit_no.split("-")[0]
            type_of_deemed_auth = request.args.get('a_TypeofDeemedAuth')

            permits = Permit.find_by_permit_no_all(permit_no)

            for permit in permits:
                for mine in permit._all_mines:

                    # Mine must be operating.
                    if not mine.mine_status or mine.mine_status[
                            0].mine_status_xref.mine_operation_status_code != "OP":
                        break

                    # IP SURVEYS (Induced): Valid MMS mine types: 'CX','ES','EU'
                    # There may be need of a check against mine_tenure_type_code IN ["MIN", "COL"] and mine_disturbance_code IN ["SUR", "UND"]
                    # but this data is inconsistant for now.
                    if type_of_deemed_auth == "INDUCED" and permit_prefix not in ["CX", "MX"]:
                        break

                    # DRILL PROGRAM (Drill): Valid MMS mine types: 'CS','CU','MS','MU','IS','IU'
                    if type_of_deemed_auth != "INDUCED" and permit_prefix not in ["C", "M"]:
                        break

                    mine_info = mine_info + mine.mine_no + ' - ' + mine.mine_name + '\r'

            if mine_info != "":
                result = "Success"
            else:
                result = "Failure"
                response_message = "NoValidMinesForPermit"

        except Exception as e:
            current_app.logger.error(str(e))
            result = "Failure"
            mine_info = ""
            response_message = "Unhandled Exception"

        return {
            "a_Result": result,
            "a_MineInfo": mine_info,
            "a_ResponseMessage": response_message,
            "a_Timestamp": datetime.utcnow()
        }


from django import forms
from manager.models import PanelUser, Story, Task

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['name', 'description', 'hours', 'assigned']
        widgets = {
            'description': forms.Textarea(attrs={'rows': 2})
        }

    def __init__(self, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        self.fields['assigned'].queryset = PanelUser.objects.filter(role=PanelUser.ROLES.DEVELOPER)

    def clean_hours(self):
        value = self.cleaned_data.get('hours')
        if value <= 0:
            self.errors['hours'] = ['This must be a positive integer value']
        return value

class TaskStatusForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['status']


import os
import ntpath
import logging

def make_directory_if_not_exists(dir_path):

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

    return dir_path

def get_index_from_file_path(path):

    try:
        file_parent_path, file_name = ntpath.split(path)
        base_name, extension = os.path.splitext(file_name)
        index = int(base_name[base_name.rfind("_") + 1:])
    except Exception as e:
        file_name = ""
        index = ""
        print(e)

    return file_name, index

def load_text(filename):

    if os.path.isfile(filename):
        file = open(filename, 'r')
        text = file.read()
        file.close()
    else:
        text = ''

    return text

def save_file(content, filename):

    with open(filename, "wb") as f:
        f.write(content)
        f.close()

    return

def log_print(info_str, file_path, only_print=False):

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        datefmt='%d/%b/%Y %H:%M:%S',
                        filename=file_path
                        )
    # print(info_str)

    if not only_print:
        logging.info(info_str)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Mike
# @Contact : 597290963@qq.com
# @Time    : 2021/1/21 23:47
# @File    : LongestCommonPrefix.py
from typing import List

class LongestCommonPrefix(object):

    def __init__(self):
        pass

    def longestCommonPrefix(self, strs: List[str]) -> str:
        """
        暴力解法：纵向查找，用第一个字符串为基准，依次比较其他字符串相同位置是否相等
        :param strs:
        :return:
        """
        if not strs:
            return ""

        length, counter = len(strs[0]), len(strs)

        for i in range(length):
            s = strs[0][i]
            if any(i == len(strs[j]) or strs[j][i] != s for j in range(1, counter)):
                return strs[0][:i]

        return strs[0]

    def longestCommonPrefix1(self, strs: List[str]) -> str:
        """
        二分查找
        :param strs:
        :return:
        """
        # 最长公共前缀的长度不会超过字符串数组中的最短字符串长度
        if not strs:
            return ""

        def isCommonPrefix1(mid):
            return all(strs[0][:mid] == strs[j][:mid] for j in range(1, len(strs)))

        right = min([len(i) for i in strs])
        left = 0
        while left < right:
            mid = (right - left + 1) // 2 + left  # 偏右
            if isCommonPrefix1(mid):
                left = mid
            else:
                right = mid - 1

        return strs[0][:left]

if __name__ == '__main__':
    print(LongestCommonPrefix().longestCommonPrefix1(["f"]))


#!/usr/local/python2.7/bin/python2.7
# -*- coding:utf8 -*-

#Date: 2013-06-26
#Name: budong

'''
说明：
    1.本脚本用于清除nginx缓存文件
    2.查看你的nginx是根据什么作为key来hash的，我的设置是 proxy_cache_key $uri$is_args$args;
    因此nginx会根据$uri$is_args$args作为key进行hash，因此可以模拟nginx对一个key进行再
    hash找到相应的文件路径，删除（具体可随意找个缓存文件 more 一下看看)
    3.缓存设置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
    根据相应的配置，请做相应修改测试
    4.uri格式请按照同级目录下rm_apk_list.txt中填写
'''
import os
import sys
try:
    from hashlib import md5
except:
    from md5 import md5

reload( sys )
sys.setdefaultencoding('utf-8')

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
URI_FILE = ''.join([PROJECT_ROOT,'/rm_apk_list.txt'])

def nginx_purge(uri):
    m = md5()
    m.update("%s" % uri)
    md5uri=m.hexdigest()
    md5uri_len=len(md5uri)
    dir1=md5uri[md5uri_len-1:]
    dir2=md5uri[md5uri_len-3:md5uri_len-1]
    file_path=("/data/mumayi/cache/%s/%s/%s" % (dir1, dir2, md5uri))
    if os.path.exists(file_path):
        os.remove(file_path)

with open("%s" % URI_FILE,'r') as uri_file:
    for line in uri_file:
        line = line.rstrip()
        nginx_purge(line)


#!/usr/bin/env python

import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name="ondelta",
    version="0.1",
    author="Adam Haney",
    author_email="adam.haney@campusbellhops.com",
    description=("A mixin that allows models to register methods that are notified when their values change, or register a method that is notified of all changes. Basically, OnDeltaMixin implements the observer pattern."),
    license="LGPL",
    keywords="Django, observer",
    url="",
    packages=['ondelta'],
    long_description=read('README.md'),
    dependency_links = [],
    install_requires=[
        'django'
        ],
    classifiers=[]
)


from datetime import datetime, timedelta
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator

DAG_ID = "task_1"
defaults = {
    "catchup": False,
    "depends_on_past": False,
    "mutex_keys": None,
    "start_date": datetime.now() - timedelta(minutes=10)
}

def create_dag(dag):
    start = DummyOperator(dag=dag, task_id="start")
    t1 = BashOperator(bash_command="echo 'task_1 sleep'; sleep 1", task_id="task_1", dag=dag)
    t2 = BashOperator(bash_command="echo 'task_2 sleep'; sleep 1", task_id="task_2", dag=dag)
    t3 = BashOperator(bash_command="echo 'task_3 sleep'; sleep 1", task_id="task_3", dag=dag)

    start >> t1
    start >> t2
    start >> t3

# Dag is returned by a factory method
def sub_dag(parent_dag_name, child_dag_name, start_date, schedule_interval):
    dag = DAG(
        '%s.%s' % (parent_dag_name, child_dag_name),
        schedule_interval=schedule_interval,
        start_date=start_date,
    )

    create_dag(dag)
    return dag

dag = DAG(
    dag_id=DAG_ID,
    default_args=defaults,
    schedule_interval=None,
)

create_dag(dag)


from typing import List

from . import BaseObject

class Client(BaseObject):

    def __init__(self,
                 service_name: str,
                 url: str,
                 _id: str = None,
                 users: List[str] = None):
        super().__init__(_id)
        self.service_name = service_name
        self.url = url
        self.users = users if users else list()


import pytest

import threading
from parsl import python_app
from parsl.tests.configs.htex_local import fresh_config
from parsl.executors.errors import SerializationError

local_config = fresh_config()
local_config.retries = 2

@python_app
def fail_pickling(x):
    return True

@pytest.mark.local
def test_serialization_error():

    lock = threading.Lock()
    x = fail_pickling(lock)

    try:
        x.result()
    except Exception as e:
        assert isinstance(e, SerializationError)
    else:
        raise ValueError


from config import PY_ANYWHERE_USERNAME
from github import Github
import pexpect
import time

def create_remote_repo(github_token):
    # accepts the user's github token and uses the pygithub api to create a
    # new remote github repository
    g = Github(github_token)
    user = g.get_user()
    user.create_repo('auto-repo-creation')

def push_code(github_username, github_token):
    # pushes files from the local git repository on the server to the
    # auto-repo-creation remote repository
    my_bash = pexpect.spawn('/bin/bash', cwd='/home/' + PY_ANYWHERE_USERNAME + '/auto-repo-creation')
    my_bash.delaybeforesend = 1
    my_bash.sendline(
        'git remote set-url origin https://github.com/' + github_username +
        '/auto-repo-creation.git')
    my_bash.delaybeforesend = 1
    my_bash.sendline('git add .')
    my_bash.delaybeforesend = 1
    my_bash.sendline("git commit -m 'populate repo'")
    my_bash.delaybeforesend = 1
    my_bash.sendline('git push -u origin master')
    my_bash.expect("Username for 'https://github.com':")
    my_bash.delaybeforesend = 1
    my_bash.sendline(github_username)
    my_bash.expect("Password for 'https://" + github_username + "@github.com':")
    my_bash.delaybeforesend = 1
    my_bash.sendline(github_token)
    time.sleep(10)


"""
Copyright (c) 2004-Present Pivotal Software, Inc.

This program and the accompanying materials are made available under
the terms of the under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time

from mpp.models import MPPTestCase
from mpp.lib.PSQL import PSQL

import pygresql.pg
from struct import *
import os

class CursorTests(MPPTestCase):

    def test_mpp24119(self):
        """
        @description postgres process crashed when running cursor with hold
        @product_version gpdb: [4.2.8.1-4.2.99.99], [4.3.3.0-]
        """
        start_time = time.time()
        conn = pygresql.pg.connect()
        conn.query("drop table if exists mpp24119")
        conn.query("create table mpp24119(i int, j int)")
        conn.query("insert into mpp24119 select i , i from generate_series(1, 10) i")
        conn.query("begin; declare c cursor with hold for select * from mpp24119;")
        conn.query("commit")
        # The crash happens when exec_execute_message is triggered after a tx with cursor
        # with hold is committed. Since there was no way to trigger this, we send a protocol
        # message directly to the socket on which the connection is established
        sockno = conn.fileno()
        msg = pack('!sbi', 'c', 0, 0)
        l = len(msg) + 4

        res = os.write(sockno, pack('!c', 'E'))
        res = os.write(sockno, pack('!i', l))
        res = os.write(sockno, msg)
        format_start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time))
        output = PSQL.run_sql_command("SELECT logmessage FROM gp_toolkit.gp_log_system WHERE " + \
                                      "logtime >= '%s' and logdatabase is null and logseverity = 'PANIC'" %(format_start_time), flags= '-q -t')
        self.assertFalse(output.strip())
        conn.close()


#學號：104213070
#姓名：葉潤安

def printResutl(resultList):
    # 把運算結果list吐出來
    i = 0
    for i in range(len(resultList)):
        print(resultList[i][0],resultList[i][1])

def findDiv(n):
    # 尋找因數
    divList = []
    for i in range(1,n+1):
        if n % i ==0 :
            divList.append(i)
    # print("--",divList)
    return divList

def isPrime(n) :
    # 判斷是否爲質數
    i = 2
    while i<n :
        if n%i==0:
            # return : end this function and return the value as the result of this class
            return False
        i = i+1
    return True

def main():
    # 輸入一個n，求其質因數
    n = int(input())
    # 利用findDiv先求n的所有因數，回傳一個list
    divList = findDiv(n)
    resultList = []
    # print(divList)
    i = 0
    temp = n
    count = 0
    for i in range(len(divList)):
        # n的所有因數中，取出是質數的值，并且非 1
        if isPrime(divList[i]) == True and divList[i] != 1:
            # print("  ",divList[i])
            # 若該質數能整除n，則讓他除一次n，若還能則再除（while loop）
            # 直到該質數無法整除n，跳出while loop，
            # 把該質數和該質數能整除多少次n，寫入2d list
            # 並把count歸零，供下一個質數使用
            while temp % divList[i] == 0 :
                temp = temp / divList[i]
                # print(temp)
                count = count + 1
            resultList.append([divList[i],count])
            count = 0
    # print(resultList)
    printResutl(resultList)

main()


#!/usr/bin/env python
from optparse import OptionParser
import os

###################################################################################
# all_genes_split.py
#
# For each motif, output a tab delimited file corresponding to the number of cds,
# lncrna, pseudogene, 3' utrs, 5' utrs, rrna and small rna hits. The output of the
# file needs to be used as an input to generate a heatmap.
###################################################################################

################################################################################
# main
################################################################################
def main():
    transcripts = []
    usage = 'usage: %prog [options] <all_genes_file>'
    parser = OptionParser(usage)
    #parser.add_option()
    (options,args)=parser.parse_args()

    if len(args) != 1:
        parser.error('Must provide all genes bed results file')
    else:
        all_genes_file = args[0]

    path = all_genes_file.split('/')
    path[-1] = 'transcripts.txt'
    path = '/'.join(path)
    out_file = open(path,'w')
    for line in open(all_genes_file):
        line = line.split()
        transcript = line[32]
        transcript = transcript.split('"')
        transcripts.append(transcript[1])

    transcripts = '\n'.join(transcripts)
    print >> out_file, transcripts,

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


from django import forms
from django.core import validators

class MyForm(forms.Form):

	def check_text_length(value):
		if len(value) > 20:

			raise forms.ValidationError('Text length exceeded')

	name 	= forms.CharField(validators = [validators.MaxLengthValidator(20)])
	email 	= forms.EmailField()
	confirm_email = forms.EmailField(label = 'Confirm Email')
	text 	= forms.CharField(widget = forms.Textarea, validators = [check_text_length])
	botcatcher = forms.CharField(widget = forms.HiddenInput, required = False)

	def clean_botcatcher(self):

		clean_botcatcher = self.cleaned_data['botcatcher']

		if len(clean_botcatcher) > 0:

			raise forms.ValidationError('GOTCHA BOT!!')

		return clean_botcatcher

	def clean(self):

		all_clean_data = super().clean()

		print(all_clean_data)

		if all_clean_data['email'] != all_clean_data['confirm_email']:

			raise forms.ValidationError('Emails do not match')


from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from ..forms import StartupRegisterForm, UserUpdateForm, StartupUpdateForm
from django.apps import apps
import googlemaps

Startup = apps.get_model('users', 'Startup')
Industry = apps.get_model('users', 'Industry')
Sector = apps.get_model('users', 'Sector')
Status = apps.get_model('users', 'Status')
Job = apps.get_model('users', 'Job')

# Create your views here.
def register_startup(request):
    if request.method == 'POST':
        form = StartupRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, 'Din startup-bruker har blitt opprettet! Du kan nå logge inn.  ')
            return redirect('news-home')
        else:
            messages.warning(request, 'Registrering ikke vellykket. Prøv igjen.  ')
    else:
        form = StartupRegisterForm()
    return render(request, 'startups/register_startup.html', {'form': form})

# Requires login for access to profile.
@login_required
def profile(request):
    all_jobs = Job.objects.all()
    return render(request, 'startups/profile.html', {'all_jobs': all_jobs})

# Requires login for access to profile.
# Fetches form for editing in edit.
@login_required
def edit(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = StartupUpdateForm(request.POST,
                                   request.FILES,
                                   instance=request.user.startup_profile)

        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request,'Din profil har blitt endret')
            return redirect('/startups/profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = StartupUpdateForm(instance=request.user.startup_profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'startups/edit.html', context)

def browse(request):
    all_startups = Startup.objects.all()
    all_industry = Industry.objects.all()
    all_sectors = Sector.objects.all()
    all_status = Status.objects.all()
    startup_filter = StartupFilter(request.GET, queryset=all_startups)
    return render(request, 'startups/browse.html', {'filter':startup_filter,'all_startups': all_startups, 'all_industry': all_industry,
                                                    'all_sectors': all_sectors, 'all_status': all_status})

def visit(request, pk):
    context = {
        'startup': Startup.objects.get(pk=pk),
        'all_jobs': Job.objects.all()
    }
    return render(request, 'startups/visit.html', context)


#!/usr/bin/env python3.5

# with daemon.DaemonContext():
# app = hwhome.Main(True)

import sys
import os
import time
import argparse
import logging
import daemon
from daemon import pidfile
import hwhome
import lockfile
import settings

debug_p = True

def do_something(logf):
  ### This does the "work" of the daemon
  logger = logging.getLogger('robot_daemon')
  logger.setLevel(logging.INFO)
  fh = logging.FileHandler(logf)
  fh.setLevel(logging.INFO)
  formatstr = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  formatter = logging.Formatter(formatstr)
  fh.setFormatter(formatter)
  logger.addHandler(fh)
  # logger.debug("this is a DEBUG message")
  # logger.info("this is an INFO message")
  # logger.error("this is an ERROR message")
  settings.logger = logger
  app = hwhome.Main(True, logger)

def start_daemon(pidf, logf):
  ### This launches the daemon in its context
  ### XXX pidfile is a context
  with daemon.DaemonContext(
    working_directory='/home/pi/workspace/python/hwPy',
    # umask=0o002,
    # pidfile=pidfile.TimeoutPIDLockFile(pidf)
    # pidfile=lockfile.FileLock('/var/run/robot_daemon.pid')
    ) as context:
    do_something(logf)

if __name__ == "__main__":
  settings.init()
  parser = argparse.ArgumentParser(description="Robot daemon in Python")
  parser.add_argument('-p', '--pid-file', default='/var/run/robot_daemon.pid')
  parser.add_argument('-l', '--log-file', default='/var/log/robot_daemon.log')
  args = parser.parse_args()

  start_daemon(pidf=args.pid_file, logf=args.log_file) # PRODUCTION

  # do_something(logf=args.log_file) # DEV


def largestIsland(grid):
    res = 0
    i = 2
    islands = []

    def dfs(grid, x, y, i):
        if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] != 1:
            return 0
        else:
            grid[x][y] = i
            return 1+dfs(grid, x-1, y, i)+dfs(grid, x+1, y, i)+dfs(grid, x, y-1, i)+dfs(grid, x, y+1, i)

    for x in range(len(grid)):
        for y in range(len(grid[0])):
            if grid[x][y] == 1:
                islands.append(dfs(grid, x, y, i))
                i += 1
    print(grid)
    print(islands)

    def checkisland(grid, x, y):
        if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
            return 0
        else:
            return grid[x][y]

    key = True
    for x in range(len(grid)):
        for y in range(len(grid[0])):
            if grid[x][y] == 0:
                key = False
                visited = set()
                visited.add(checkisland(grid, x-1, y))
                visited.add(checkisland(grid, x, y-1))
                visited.add(checkisland(grid, x, y+1))
                visited.add(checkisland(grid, x+1, y))
                res = max(res, 1+sum(islands[x-2] for x in visited if x != 0))
    if key:
        res = len(grid)*len(grid[0])
    return res

def test():
    a = [[1, 0], [0, 1]]
    print(largestIsland(a))
    b = [[0]]
    print(largestIsland(b))
    c = [[1, 0, 1], [0, 0, 0], [0, 1, 1]]
    print(largestIsland(c))

test()


from resources.colours import *
import random
import pygame

WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
borderSize = 10

def loadDictionaries(substring):
    indice = [i for i, s in enumerate(dataLocations) if substring in s]

    try:
        with open(dataLocations[indice[0]], 'r') as dictionary:
            result = eval(dictionary.read())
            dictionary.close()
            return result
    except IndexError:
        print('Nothing enumerable, skipping...\nPlease replace empty lines with a #!')

def loadTxt(location):
    with open(location, 'r') as file:
        result = file.read()
        file.close()
        return result

def cRC(limit):  # createRandomCoordinates
    return random.randint(limit / 10, limit - 4 * borderSize)

dataLocations = []
with open('resources/list.txt', 'r') as locationList:
    for line in locationList:
        if '#' not in line:
            dataLocations.append('resources/' + line.strip('\n'))
locationList.close()


import numpy as np
from overrides import overrides
from typing import List
from ..dataset_reader import DatasetReader
from ..compound_dataset_reader import CompoundDatasetReader, CompoundDatasetEpochIterator

class CombinedDatasetReaderIterator(CompoundDatasetEpochIterator):
	def __init__(self, reader):
		self.baseIterators = [CompoundDatasetEpochIterator(baseReader) for baseReader in reader.baseReaders]
		self.ix = -1
		self.Mappings = self.getMappings()
		self.len = len(self)

	def getMappings(self):
		Mappings = np.zeros((len(self), 2), dtype=np.uint32)
		ix = 0
		for i in range(len(self.baseIterators)):
			g = self.baseIterators[i]
			Range = np.arange(len(g))
			Mappings[ix : ix + len(g), 0] = np.repeat([i], len(g))
			Mappings[ix : ix + len(g), 1] = Range
			ix += len(g)
		return Mappings

	@overrides
	def __len__(self) -> bool:
		return sum([len(x) for x in self.baseIterators])

	@overrides
	def __getitem__(self, ix):
		readerIx, readerInnerIx = self.Mappings[ix]
		return self.baseIterators[readerIx].__getitem__(readerInnerIx)

	@overrides
	def __getattr__(self, key):
		X = [getattr(baseIterator, key) for baseIterator in self.baseIterators]
		return X

# @brief A composite dataset reader that has a base reader attribute which it can partially use based on the percent
#  defined in the constructor
class CombinedDatasetReader(CompoundDatasetReader):
	def __init__(self, baseReaders:List[DatasetReader]):
		# super().__init__(baseReader)
		assert len(baseReaders) > 1, "Must provide a list of DatasetReaders!"
		firstReader = baseReaders[0]
		assert isinstance(firstReader, DatasetReader)
		for reader in baseReaders[1 : ]:
			assert isinstance(reader, DatasetReader)
			assert reader.datasetFormat == firstReader.datasetFormat, "All readers must provide same DatasetFormat!"

		DatasetReader.__init__(self, dataBuckets=firstReader.datasetFormat.dataBuckets, \
			dimGetter=firstReader.datasetFormat.dimGetter, dimTransform=firstReader.datasetFormat.dimTransform)
		self.baseReaders = [CompoundDatasetReader(reader) for reader in baseReaders]
		self.baseReader = self.baseReaders

	@overrides
	def iterateOneEpoch(self):
		return CombinedDatasetReaderIterator(self)

	@overrides
	def __len__(self):
		return sum([len(reader) for reader in self.baseReaders])

	@overrides
	def __str__(self) -> str:
		summaryStr = "[CombinedDatasetReader]"
		summaryStr += "\n - Num datasets: %d" % (len(self.baseReaders))
		for i, reader in enumerate(self.baseReaders):
			summaryStr += "\n----------- %d -----------" % (i + 1)
			summaryStr += "\n%s" % str(reader)
		return summaryStr


from dps import cfg
from dps.config import DEFAULT_CONFIG
from dps.env import BatchGymEnv
from dps.utils.tf import MLP, FeedforwardCell
from dps.rl import rl_render_hook, BuildSoftmaxPolicy, BuildMlpController

def build_env():
    return BatchGymEnv(gym_env='MountainCar-v0')

controller = lambda params_dim, name: FeedforwardCell(
    lambda inp, output_size: MLP(
        [cfg.n_controller_units, cfg.n_controller_units])(inp, output_size),
    params_dim, name=name)

config = DEFAULT_CONFIG.copy()

# So far, have not been able to solve this with a policy gradient method, the exploration problem is quite hard.

config.update(
    env_name="mountain_car",

    build_env=build_env,

    build_controller=BuildMlpController(),
    build_policy=BuildSoftmaxPolicy(one_hot=False),
    exploration_schedule="1.0",
    val_exploration_schedule="0.1",

    n_controller_units=64,

    value_weight=10.0,
    reward_scale=200,
    split=False,
    v_lmbda=0.8,
    q_lmbda=0.8,

    T=None,

    n_val=100,
    batch_size=1,
    render_hook=rl_render_hook,
    render_step=10,
    eval_step=10,
    display_step=10,
    stopping_criteria="reward_per_ep,max",
    threshold=0,
)


##
##  Programación en Python
##  ===========================================================================
##
##  Imprima la cantidad de registros por letra para la primera columna del
##  archivo `data.csv`, ordenados alfabeticamente por la letra.
##
##  Rta/
##  A,8
##  B,7
##  C,5
##  D,6
##  E,14
##
##  >>> Escriba su codigo a partir de este punto <<<
##
#cleanfile
import os
lines = ""
with open('data.csv') as f:
    s = f.read();
    s = s.replace('\t',',')
    s = s.replace(' ',',')
    lines+=s;
fo = open('out.csv','w');
fo.write(lines)
fo.close()
data = [ x.split(',')[0] for x in open('out.csv').readlines()];
data.sort()
from itertools import groupby
res = [key+','+str(len(list(group))) for key,group in groupby(data)]
for x in res:
    print(x)
os.remove('out.csv')


from sqlalchemy.exc import SQLAlchemyError, IntegrityError

from convergence.utils import exceptions
from convergence.utils import logger
from convergence.data.repo import Store
from convergence.data.models import User

class UserStore(Store):

    def __init__(self, session=None):
        super().__init__(session)

    def get_user_by_email(self, email):
        """
        Return user for provided email address
        :param email: email address
        :return: User object
        """
        return self.session.query(User).filter_by(email=email).first()

    def get_user_by_id(self, user_id):
        """
        Return user for provided user id
        :param user_id: user id
        :return: User object
        """
        return self.session.query(User).get(user_id)

    def get_users_by_ids(self, user_ids):
        """
        Return users for provided user ids
        :param user_ids: list of user ids
        :return: list of User objects
        """
        return self.session.query(User).filter(User.id.in_(user_ids)).all()

    def get_users_by_emails(self, emails):
        """
        Return users with given set of usernames (emails).
        Resulting list might be equal or less in length
        then requested usernames.
        :param usernames list of user names (emails)
        """
        return self.session.query(User).filter(User.email.in_(emails)).all()

    def add_user(self, user):
        """
        Add user to database
        :param user: User object
        """
        self.session.add(user)
        try:
            self.session.commit()
        except SQLAlchemyError as e:
            self.session.rollback()
            if isinstance(e, IntegrityError):
                raise exceptions.InputError("Email address in use.")
            else:
                logger.log_error(f"Database Error: {str(e)}")
                raise exceptions.ServerError("Error adding user")
        return None

    def delete_user(self, user):
        """
        Delete user from database
        :param user: User object
        """
        self.session.delete(user)
        try:
            self.session.commit()
        except SQLAlchemyError as e:
            self.session.rollback()
            logger.log_error(f"Database Error: {str(e)}")
            raise exceptions.ServerError("Error deleting user")
        return None

    def commit_changes(self):
        """Commit changes in session object to database"""
        try:
            self.session.commit()
        except SQLAlchemyError as e:
            logger.log_error(f"Database Error: {str(e)}")
            raise exceptions.ServerError("Error updating user info")


"""empty message

Revision ID: bb8d5b41a64c
Revises: b8ceddd90187
Create Date: 2018-08-28 16:19:07.518299

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'bb8d5b41a64c'
down_revision = 'b8ceddd90187'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user', sa.Column('validated', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('user', 'validated')
    # ### end Alembic commands ###


import urllib.request
import urllib.parse
from bs4 import BeautifulSoup

defaultURL="https://openapi.naver.com/v1/search/kin.xml"
sort='&sort=sim'
start='&start=1'
display='&display=100'
search=str(input("검색어 : "))
query='?query='+urllib.parse.quote_plus(search)
fullURL = defaultURL + query + display + start + sort
print(fullURL)

file=open("c:\\python34\\crawl\\jisikin\\%s.txt"%search,"w",encoding='utf-8')
headers={
    'Most':'openapi.naver.com',
    'User-Agent':'curl/7.43.0',
    'Accept':'*/*',
    'Content-Type':'application/xml',
    'X-Naver-Client-Id':'BqGt9k2AGN36MSJWqznB',
    'X-Naver-Client-Secret':'_CJLTTvzol'
    }

req=urllib.request.Request(fullURL,headers=headers)
f=urllib.request.urlopen(req)
resultXML=f.read()
xmlsoup=BeautifulSoup(resultXML,'html.parser')
items=xmlsoup.find_all('item')

for item in items:
    file.write('-------------------------------\n')
    file.write(' 시간 : ')
    file.write(' 제목 : '+ item.title.get_text(strip=1)+'\n')
    file.write(' 내용 : '+ item.description.get_text(strip=1)+'\n')
    file.write(' 링크 : '+ item.link.get_text(strip=1)+'\n')#본문의 링크를 가져오기 위해
    file.write('-------------------------------\n')

file.close()


from pyspark.sql import SparkSession
import os

os.environ["SPARK_HOME"] = "/usr/local/spark/"

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL basic example") \
    .config("spark.some.config.option", "some-value")\
    .config('spark.jars.packages',"org.elasticsearch:elasticsearch-spark-20_2.11:5.5.2")\
    .getOrCreate()

es_conf = {"es.read.metadata": "true","es.read.field.as.array.include" : "id, tweet"}

tweets = spark.read.format("org.elasticsearch.spark.sql").options(**es_conf).load("tweets/tweet_table")

tweets.count()
print('a')


import numpy as np
import matplotlib.pyplot as plt
import mltools as ml

iris = np.genfromtxt("data/iris.txt", delimiter = None)

Y = iris[: , -1]
X = iris[: ,0: -1]

X,Y = ml.shuffleData(X, Y)

Xtr, Xte, Ytr, Yte = ml.splitData(X, Y, 0.75)

for k in [1, 5, 10, 50]:
	knn = ml.knn.knnClassify(Xtr[:,0:2], Ytr, k)
	ml.plot.plotClassify2D(knn, Xtr[:,0:2], Ytr)
	plt.title('k = %i'%k)
	plt.show()


#------------------------------------------------------------------------------
# Mach-8: The Virtual Machinery Playpen
#
# blackchip.org, Inspired by the Vintage Computer Club.
# All rites reversed (K) 2011, Reprint what you like.
#
# $Id: mach.py 73 2011-10-27 12:44:53Z mcgann $
#------------------------------------------------------------------------------
"""
The Mach-8 virtual computer and supporting classes.
"""
from mach8 import aliases, io, memmap, memory, rom, tools, x6502, yapc

BLOCK_ZERO_PAGE = 'zero_page'
"""
Name for the zero page memory block: ``$0000 - $00FF``
"""

BLOCK_STACK = 'stack'
"""
Name for the stack block: ``$0100 - $01FF``
"""

BLOCK_SCRATCH = 'scratch'
"""
Name for the block reserved for scratch space: ``$0200 - $05FF``
"""

BLOCK_ROM = 'rom'
"""
Name for the kernel and runtime block: ``$0600 - $1FFF``
"""

BLOCK_PROGRAM = 'program'
"""
Name for the block of memory for user programs: ``$2000 - $FFEF``
"""

BLOCK_VECTOR = 'vector'
"""
Name for the block containg vectors and jump tables: ``$FFF0 - $FFFF``
"""

class JiffyTimer(object):
    """
    A series of *actions* that are executed at a certain *interval*. Each
    time :meth:`cycle` is invoked, a counter is incremented and if it equals
    the *interval* count, all *actions* in the sequence are called in iteration
    order. Actions registered should be callable objects.
    """

    actions = None
    """
    List of actions to be called.
    """

    interval = 0
    """
    The number of times :meth:`cycle` needs to be called before actions are
    executed.
    """

    count = 0
    """
    Current numer of times :meth:`cycle` has been called -- resets to zero
    when the actions are called.
    """

    def __init__(self, actions, interval=100):
        self.actions = list(actions)
        self.interval = interval
        self.count = 0

    def service(self):
        """
        Service all actions immediately and reset the internal counter to
        zero.
        """
        [action() for action in self.actions]
        self.count = 0

    def cycle(self):
        """
        Increment the internal counter and if it is equal to the *interval*
        parameter, execute all actions.
        """
        self.count += 1
        if self.count >= self.interval:
            self.service()

class Computer(object):

    reset_vector = None

    step = False

    breakpoints = None

    cycles = 0

    limit = None

    running = False

    def __init__(self):
        bank = memory.Bank()
        bank.map(memory.Block(pages=0x01), 0x0000, BLOCK_ZERO_PAGE)
        bank.map(memory.Block(pages=0x01), 0x0100, BLOCK_STACK)
        bank.map(memory.Block(pages=0x04), 0x0200, BLOCK_SCRATCH)
        bank.map(memory.Block(pages=0x1a), 0x0600, BLOCK_ROM)
        bank.map(memory.Block(pages=0xdf), 0x2000, BLOCK_PROGRAM)
        bank.map(memory.Block(pages=0x01), 0xff00, BLOCK_VECTOR)

        self.mem = bank
        self.meta = tools.MetaSource()
        self.cpu = x6502.CPU(self.mem)

        self.terminal_output = io.TerminalOutput(self.mem)
        self.terminal_input = io.TerminalInput(self.mem)
        self.fpu = io.FPU(self.mem)
        actions = [self.terminal_output, self.terminal_input, self.fpu]
        self.timer = JiffyTimer(actions)

        self.breakpoints = set()
        self.reset_vector = memmap.ROM_START - 1
        self.cpu.run_listeners += [self._idle_listener, self._monitor_listener]
        yapc.compiler_factory = lambda: yapc.Compiler(self)

        self._reset()

    def _reset(self):
        self.mem.clear()
        self.meta.reset()

        self.mem.find(BLOCK_ROM).read_only = False
        self._fill_system_symbols()
        self._fill_aliases()
        self._assemble_rom()
        self.mem.find(BLOCK_ROM).read_only = True

        self.breakpoints = set()
        self.cpu.pc = self.reset_vector

    def reset(self):
        self._reset()
        self.run()

    def run(self):
        self.cpu.exit = None
        self.running = True
        self.cycles = 0
        self.terminal_input.start()
        try:
            while self.cpu.exit is None:
                if not self.cpu.i:
                    self.timer.cycle()
                self.cpu.next()
                if self.step:
                    self.cpu.exit = x6502.EXIT_STEP
                # Break if the PC is about to be address
                elif self.cpu.pc + 1 in self.breakpoints:
                    self.cpu.exit = x6502.EXIT_BREAKPOINT
                self.cycles += 1
                if self.limit is not None and self.cycles >= self.limit:
                    raise LimitExceededError('Execution limit reached at ' +
                                             '{:d} cycles'.format(self.cycles))
        except:
            self.cpu.exit = x6502.EXIT_TRAP
            raise
        finally:
            self.terminal_input.stop()
            self.step = False
            self.running = False
            self.timer.service()

    def _idle_listener(self, address, instr):
        if address == self.meta['IDLE']:
            self.timer.service()

    def _monitor_listener(self, address, instr):
        if address == self.meta['MONITOR']:
            self.cpu.exit = x6502.EXIT_MONITOR

    #--------------------------------------------------------------------------
    # Initializing methods
    #--------------------------------------------------------------------------
    def _fill_system_symbols(self):
        """
        Add all elements in the memmap module to the symbol table.
        """
        for symbol in dir(memmap):
            if symbol[0] != '_' and symbol[-1] != '_':
                value = getattr(memmap, '__dict__')[symbol]
                self.meta.define_label(symbol, value, reserved=True)

    def _fill_aliases(self):
        """
        Add all elements in the alias module to the symbol table.
        """
        for alias in dir(aliases):
            if alias[0] != '_' and alias[-1] != '_':
                value = getattr(aliases, '__dict__')[alias]
                if isinstance(value, int):
                    self.meta.define_alias(alias, value, reserved=True)

    def _assemble_rom(self):
        """
        Assemble all routines in the rom module.
        """
        asm = tools.Assembler(self.mem, origin=memmap.ROM_START,
                              meta=self.meta)

        for routine_name in rom.__all__:
            routine = getattr(rom, routine_name)
            routine(asm)
        asm.verify()

#------------------------------------------------------------------------------
# Exceptions
#------------------------------------------------------------------------------
class LimitExceededError(Exception):
    """
    CPU has executed more instructions than allowed.
    """


import networkx as nx
import matplotlib.pyplot as plt
import json
from math import sqrt
from collections import defaultdict

from secrets import SystemRandom
sr = SystemRandom()

class RouteBuilder:
    def __init__(self, mysid):

        self.mysid = mysid

        # sid -> [stationinrange,]
        self.routeables = defaultdict(list)
        # sid -> [possibleroutes,]
        self.builtRoutes = {}

        # Stores all known stations
        self.known = set()

        # sid -> sessionID
        self.sessions = {} # This isn't used right now, but once encryption is implemented session data will be important

    def rebuildRoutes(self):
        routes = self.routeables.copy()
        mysid = self.mysid
        g = nx.empty_graph()

        for sid, othernodes in routes.items():
            for othernode in othernodes:
                g.add_edge(sid, othernode)

        self.builtRoutes.clear()
        for sid in routes.keys():
            if sid != mysid:
                #paths = list(nx.all_simple_paths(g, mysid, sid))
                paths = list(nx.all_shortest_paths(g, mysid, sid))

                for path in paths:
                    path.remove(mysid)
                self.builtRoutes[sid] = paths
        self.G = g

        if mysid == 0:
            plt.cla()
            nx.draw(g, with_labels=True)
            plt.show()
            plt.pause(0.1)

    def updateRouteable(self, sid, updated, clearPrevious=False):
        if not isinstance(updated, list):
            raise TypeError

        change = self.routeables[sid].copy()

        if clearPrevious == True:
            self.routeables[sid].clear()

        self.routeables[sid] = list(set(self.routeables[sid]+updated))

        if self.routeables[sid] != change:
            self.rebuildRoutes()

    def countRecieve(self, sid):
        if sid != None:
            self.updateRouteable(self.mysid, [sid])
            self.known.add(sid)

    def newSession(self, sid, sessionID):
        self.sessions[sid] = sessionID

    def hasSession(self, sid):
        if sid == self.mysid:
            return True
        if self.sessions.get(sid, -1) >= 0:
            return True
        else:
            return False

    def getMissingSessions(self, stations):
        return [ sid for sid in stations if not self.hasSession(sid) ]

    def getRoute(self, destination, onlyReturnActiveSessions=False):
        if destination in self.routeables[self.mysid]:
            return [destination]

        if self.builtRoutes.get(destination):
            routes = self.builtRoutes[destination].copy()
            if not onlyReturnActiveSessions:
                return sr.choice(routes)
            else:
                routes = [ route for route in routes if not self.getMissingSessions(route) ]
                if len(routes) > 0:
                    return sr.choice(routes)
        return []

    def displayRoutes(self):
        self.rebuildRoutes()
        plt.cla()
        nx.draw(self.G, with_labels=True)
        plt.show()

class AirspaceRouteBuilder:

    def __init__(self, positionDict, transmitRange):
        self.positionDict = positionDict
        self.transmitRange = transmitRange
        self.ranges = {}
        self.G = None

    @staticmethod
    def distance(sourcexy, destxy):
        (sx,sy), (dx,dy) = sourcexy, destxy
        d = sqrt(((sx-dx)**2) + ((sy-dy)**2) )
        return d

    def updateRanges(self):
        g = nx.empty_graph()
        self.ranges.clear()
        for sid, sxy in self.positionDict.items():
            self.ranges[sid] = []
            for did, dxy in self.positionDict.items():
                if did != sid:
                    if self.distance(sxy, dxy) < self.transmitRange:
                        self.ranges[sid].append(did)
                        g.add_edge(sid, did)
        self.G = g

    def inRange(self, sid):
        return self.ranges.get(sid, [])

    def displayRanges(self):
        plt.cla()
        nx.draw(self.G, with_labels=True)
        plt.show()
        plt.pause(0.1)


import datetime

import requests

from nemo.helper import *

numList = []
moneyList = []
orderList = []

header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) Appl\
eWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}

def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today - oneday
    return yesterday

def project_name(pro_id):
    url = 'https://wds.modian.com/api/project/detail'
    form = {
        'pro_id': pro_id
    }
    sign = getSign(form)
    form['sign'] = sign
    response = requests.post(url, form, headers=header).json()
    project_name = response['data'][0]['pro_name']
    return project_name

def getOrders(pro_id):
    page = 1
    date = str(getYesterday())
    url = 'https://wds.modian.com/api/project/orders'
    while True:
        form = {
            'page': page,
            'pro_id': pro_id
        }
        sign = getSign(form)
        form['sign'] = sign
        response = requests.post(url, form, headers=header).json()
        page += 1
        datas = response['data']
        if datas is None:
            break
        for data in datas:
            if date in data['pay_time']:
                orderList.append(data)
                moneyList.append(data['backer_money'])
                numList.append(data['user_id'])

    moneyNum = '%.2f' % sum(moneyList)
    peopleNum = len(list(set(numList)))
    orderNum = len(orderList)
    save_to_database(date, moneyNum, peopleNum, orderNum)

def save_to_database(date, moneyNum, peopleNum, orderNum):
    db = connect_database()
    cursor = db.cursor()
    cursor.execute("INSERT INTO jzdaily VALUES (%s,%s,%s,%s)", (date, moneyNum, peopleNum, orderNum))
    db.commit()
    db.close()

if __name__ == '__main__':
    pro_id = 12767
    getOrders(pro_id)


"""
Author: Nguyễn Manh Trung
Date: 24/09/2021

Problem:Write a script named dif.py. This script should prompt the user for the names
of two text files and compare the contents of the two files to see if they are the
same. If they are, the script should simply output "Yes". If they are not, the script
should output "No", followed by the first lines of each file that differ from each
other. The input loop should read and compare lines from each file. The loop
should break as soon as a pair of different lines is found

Solution:

    ....
"""
file1 = input("Nhập file 1: ")
file2 = input("Nhập file 2: ")

same = True  # check whether if files are not same

with open(file1, "r") as fp, open(file2, "r") as fp1:
    for line1, line2 in zip(fp, fp1):
        if line1 != line2:
            same = False
            print("No")
            print(line1)
            print(line2)
            break

if same:
    print("yes")


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 11:40:07 2019

@author: arena
"""
"""
這題先指定一個數，之後採用左右指針方式進行，惟須注意重複的list
Runtime: 832 ms, faster than 74.85% of Python3 online submissions for 3Sum.
Memory Usage: 16.8 MB, less than 64.31% of Python3 online submissions for 3Sum.
"""
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        ans=[]
        nums.sort()
        lenN = len(nums)
        for i in range(lenN):
            if nums[i] > 0:
                break
            if i > 0 and nums[i] == nums[i-1]:
                continue

            left = i+1
            right = lenN-1
            while left < right:
                value = nums[i] + nums[left] + nums[right]
                if value == 0:
                    ans.append([nums[i],nums[left],nums[right]])
                    while left < right and nums[left] == nums[left+1]:
                        left += 1
                    while left < right and nums[right] == nums[right-1]:
                        right -= 1
                    left, right = left+1, right-1
                elif value < 0:
                    left += 1
                else:
                    right -= 1
        return ans


import os
import numpy as np
from cv2 import cv2
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from numpy import array

TRAIN_DIR = str(os.getcwd())+r'/MNIST/trainingSet'
train_folder_list = array(os.listdir(TRAIN_DIR))

train_input = []
train_label = []

label_encoder = LabelEncoder()  # LabelEncoder Class 호출
integer_encoded = label_encoder.fit_transform(train_folder_list)
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)

for index in range(len(train_folder_list)):
    path = os.path.join(TRAIN_DIR, train_folder_list[index])
    path = path + '/'
    img_list = os.listdir(path)
    for img in img_list:
        img_path = os.path.join(path, img)
        print(img_path, ": ", img) # Debugging
        img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        train_input.append([np.array(img)])
        train_label.append([np.array(onehot_encoded[index])])

print("[ 1 ]", train_input)
train_input = np.reshape(train_input, (-1, 784))
train_label = np.reshape(train_label, (-1, 4))
print("[ 2 ]", train_input)

train_input = np.array(train_input).astype(np.float32)
train_label = np.array(train_label).astype(np.float32)

np.save("train_data.npy", train_input)
np.save("train_label.npy", train_label)