# This file contains feature extraction methods and harness
# code for data classification

import naiveBayes
import knnClassifier
import knnClassifier_Digits
import knnClassifier_Faces
import perceptron
import minicontest
import samples
import sys
import util
import time
import numpy as np
import random

TEST_SET_SIZE = 100
DIGIT_DATUM_WIDTH=28
DIGIT_DATUM_HEIGHT=28
FACE_DATUM_WIDTH=60
FACE_DATUM_HEIGHT=70

def basicFeatureExtractorDigit(datum):
  """
  Returns a set of pixel features indicating whether
  each pixel in the provided datum is white (0) or gray/black (1)
  """
  a = datum.getPixels()

  features = util.Counter()
  for x in range(DIGIT_DATUM_WIDTH):
    for y in range(DIGIT_DATUM_HEIGHT):
      if datum.getPixel(x, y) > 0:
        features[(x,y)] = 1
      else:
        features[(x,y)] = 0
  return features

def basicFeatureExtractorFace(datum):
  """
  Returns a set of pixel features indicating whether
  each pixel in the provided datum is an edge (1) or no edge (0)
  """
  a = datum.getPixels()

  features = util.Counter()
  for x in range(FACE_DATUM_WIDTH):
    for y in range(FACE_DATUM_HEIGHT):
      if datum.getPixel(x, y) > 0:
        features[(x,y)] = 1
      else:
        features[(x,y)] = 0
  return features

def enhancedFeatureExtractorDigit(datum):
  """
  Your feature extraction playground.

  You should return a util.counter() of features
  for this datum (datum is of type samples.Datum).

  ## DESCRIBE YOUR ENHANCED FEATURES HERE...

  ##
  """
  block_size = 4
  features = util.Counter()
  for x in range(0, DIGIT_DATUM_WIDTH, block_size):
    for y in range(0, DIGIT_DATUM_HEIGHT, block_size):
      for x1 in range(block_size):
        for y1 in range(block_size):
          if datum.getPixel(x+x1, y+y1) > 0:
            features[(x,y)] += 1 # counting the number of pixels in 7x7 grid
          else:
            features[(x,y)] += 0
  return features

  "*** YOUR CODE HERE ***"

  return features

def contestFeatureExtractorDigit(datum):
  """
  Specify features to use for the minicontest
  """
  features =  basicFeatureExtractorDigit(datum)
  return features

def enhancedFeatureExtractorFace(datum):
  """
  Your feature extraction playground for faces.
  It is your choice to modify this.
  """
  block_size = 5
  features = util.Counter()
  for x in range(0, FACE_DATUM_WIDTH, block_size):
    for y in range(0, FACE_DATUM_HEIGHT, block_size):
      for x1 in range(block_size):
        for y1 in range(block_size):
          if datum.getPixel(x+x1, y+y1) > 0:
            features[(x,y)] += 1 # counting the number of pixels in 5x5 grid
          else:
            features[(x,y)] += 0
  return features

def analysis(classifier, guesses, testLabels, testData, rawTestData, printImage):
  """
  This function is called after learning.
  Include any code that you want here to help you analyze your results.

  Use the printImage(<list of pixels>) function to visualize features.

  An example of use has been given to you.

  - classifier is the trained classifier
  - guesses is the list of labels predicted by your classifier on the test set
  - testLabels is the list of true labels
  - testData is the list of training datapoints (as util.Counter of features)
  - rawTestData is the list of training datapoints (as samples.Datum)
  - printImage is a method to visualize the features
  (see its use in the odds ratio part in runClassifier method)

  This code won't be evaluated. It is for your own optional use
  (and you can modify the signature if you want).
  """

  # Put any code here...
  # Example of use:
  for i in range(len(guesses)):
      prediction = guesses[i]
      truth = testLabels[i]
      if (prediction != truth):
          print("===================================")
          print("Mistake on example %d" % i)
          print("Predicted %d; truth is %d" % (prediction, truth))
          print("Image: ")
          #print(rawTestData[i])
          break

## =====================
## You don't have to modify any code below.
## =====================

class ImagePrinter:
    def __init__(self, width, height):
      self.width = width
      self.height = height

    def printImage(self, pixels):
      """
      Prints a Datum object that contains all pixels in the
      provided list of pixels.  This will serve as a helper function
      to the analysis function you write.

      Pixels should take the form
      [(2,2), (2, 3), ...]
      where each tuple represents a pixel.
      """
      image = samples.Datum(None,self.width,self.height)
      for pix in pixels:
        try:
            # This is so that new features that you could define which
            # which are not of the form of (x,y) will not break
            # this image printer...
            x,y = pix
            image.pixels[x][y] = 2
        except:
            print("new features:", pix)
            continue
      print(image)

def default(str):
  return str + ' [Default: %default]'

def readCommand( argv ):
  "Processes the command used to run from the command line."
  from optparse import OptionParser
  parser = OptionParser(USAGE_STRING)

  parser.add_option('-c', '--classifier', help=default('The type of classifier'), choices=['nb', 'naiveBayes', 'perceptron', 'minicontest', 'knnClassifier'], default='naiveBayes')
  parser.add_option('-d', '--data', help=default('Dataset to use'), choices=['digits', 'faces'], default='digits')
  parser.add_option('-t', '--training', help=default('The size of the training set'), default=100, type="int")
  parser.add_option('-f', '--features', help=default('Whether to use enhanced features'), default=False, action="store_true")
  parser.add_option('-o', '--odds', help=default('Whether to compute odds ratios'), default=False, action="store_true")
  parser.add_option('-1', '--label1', help=default("First label in an odds ratio comparison"), default=0, type="int")
  parser.add_option('-2', '--label2', help=default("Second label in an odds ratio comparison"), default=1, type="int")
  parser.add_option('-k', '--smoothing', help=default("Smoothing parameter (ignored when using --autotune)"), type="float", default=2.0)
  parser.add_option('-a', '--autotune', help=default("Whether to automatically tune hyperparameters"), default=False, action="store_true")
  parser.add_option('-i', '--iterations', help=default("Maximum iterations to run training"), default=3, type="int")
  parser.add_option('-r', '--randomizedselection', help=default("Use randomized data points"), default=False, action="store_true")

  options, otherjunk = parser.parse_args(argv)
  if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + str(otherjunk))
  args = {}

  # Set up variables according to the command line input.
  print("Doing classification")
  print("--------------------")
  print("data:\t\t" + options.data)
  print("classifier:\t\t" + options.classifier)
  if not options.classifier == 'minicontest':
    print("using enhanced features?:\t" + str(options.features))
  else:
    print("using minicontest feature extractor")
  print("training set size:\t" + str(options.training))
  if(options.data=="digits"):
    printImage = ImagePrinter(DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT).printImage
    if (options.features):
      featureFunction = enhancedFeatureExtractorDigit
    else:
      featureFunction = basicFeatureExtractorDigit
    if (options.classifier == 'minicontest'):
      featureFunction = contestFeatureExtractorDigit
  elif(options.data=="faces"):
    printImage = ImagePrinter(FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT).printImage
    if (options.features):
      featureFunction = enhancedFeatureExtractorFace
    else:
      featureFunction = basicFeatureExtractorFace
  else:
    print("Unknown dataset", options.data)
    print(USAGE_STRING)
    sys.exit(2)

  if(options.data=="digits"):
    legalLabels = list(range(10))
  else:
    legalLabels = list(range(2))

  if options.training <= 0:
    print("Training set size should be a positive integer (you provided: %d)" % options.training)
    print(USAGE_STRING)
    sys.exit(2)

  if options.smoothing <= 0:
    print("Please provide a positive number for smoothing (you provided: %f)" % options.smoothing)
    print(USAGE_STRING)
    sys.exit(2)

  if options.odds:
    if options.label1 not in legalLabels or options.label2 not in legalLabels:
      print("Didn't provide a legal labels for the odds ratio: (%d,%d)" % (options.label1, options.label2))
      print(USAGE_STRING)
      sys.exit(2)

  if(options.classifier == "naiveBayes" or options.classifier == "nb"):
    classifier = naiveBayes.NaiveBayesClassifier(legalLabels)
    classifier.setSmoothing(options.smoothing)
    if (options.autotune):
        print("using automatic tuning for naivebayes")
        classifier.automaticTuning = True
    else:
        print("using smoothing parameter k=%f for naivebayes" %  options.smoothing)
  elif(options.classifier == "perceptron"):
    classifier = perceptron.PerceptronClassifier(legalLabels,options.iterations)
  elif(options.classifier == "knnClassifier"):
    if(options.data == "faces"):
      classifier = knnClassifier_Faces.kNearestNeighborsClassifier()
    else:
      classifier = knnClassifier_Digits.kNearestNeighborsClassifier()
  elif(options.classifier == 'minicontest'):
    classifier = minicontest.contestClassifier(legalLabels)
  else:
    print("Unknown classifier:", options.classifier)
    print(USAGE_STRING)

    sys.exit(2)

  args['classifier'] = classifier
  args['featureFunction'] = featureFunction
  args['printImage'] = printImage

  return args, options

USAGE_STRING = """
  USAGE:      python dataClassifier.py <options>
  EXAMPLES:   (1) python dataClassifier.py
                  - trains the default mostFrequent classifier on the digit dataset
                  using the default 100 training examples and
                  then test the classifier on test data
              (2) python dataClassifier.py -c naiveBayes -d digits -t 1000 -f -o -1 3 -2 6 -k 2.5
                  - would run the naive Bayes classifier on 1000 training examples
                  using the enhancedFeatureExtractorDigits function to get the features
                  on the faces dataset, would use the smoothing parameter equals to 2.5, would
                  test the classifier on the test data and performs an odd ratio analysis
                  with label1=3 vs. label2=6
                 """

# Main harness code

def runClassifier(args, options):

  featureFunction = args['featureFunction']
  classifier = args['classifier']
  printImage = args['printImage']

  # Load data
  numTraining = options.training

  if(options.randomizedselection and options.data=='faces'):
     numTraining = 451
  if(options.randomizedselection and options.data=='digits'):
     numTraining = 5000

  if(options.data=="faces"):
    rawTrainingData = samples.loadDataFile("facedata/facedatatrain", numTraining,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT)
    trainingLabels = samples.loadLabelsFile("facedata/facedatatrainlabels", numTraining)
    rawValidationData = samples.loadDataFile("facedata/facedatatrain", TEST_SET_SIZE,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT)
    validationLabels = samples.loadLabelsFile("facedata/facedatatrainlabels", TEST_SET_SIZE)
    rawTestData = samples.loadDataFile("facedata/facedatatest", TEST_SET_SIZE,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT)
    testLabels = samples.loadLabelsFile("facedata/facedatatestlabels", TEST_SET_SIZE)
  else:
    rawTrainingData = samples.loadDataFile("digitdata/trainingimages", numTraining,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT)
    trainingLabels = samples.loadLabelsFile("digitdata/traininglabels", numTraining)
    rawValidationData = samples.loadDataFile("digitdata/validationimages", TEST_SET_SIZE,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT)
    validationLabels = samples.loadLabelsFile("digitdata/validationlabels", TEST_SET_SIZE)
    rawTestData = samples.loadDataFile("digitdata/testimages", TEST_SET_SIZE,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT)
    testLabels = samples.loadLabelsFile("digitdata/testlabels", TEST_SET_SIZE)

  # Extract features
  print("Extracting features...")
  trainingData = list(map(featureFunction, rawTrainingData))
  validationData = list(map(featureFunction, rawValidationData))
  testData = list(map(featureFunction, rawTestData))

  if(options.randomizedselection):
    print("Using Randomized selection: selecting {} random data points from {}".format(options.training,numTraining))
    merged=list(map(lambda x, y:(x,y), trainingData, trainingLabels))
    random.shuffle(merged)
    trainingData,trainingLabels=[],[]
    for i in range(options.training):
      trainingData.append(merged[i][0])
      trainingLabels.append(merged[i][1])

  f = open("testdata_faces_simple.txt", "a")
  f.write("\n,".join(str(item) for item in testData))
  f.close()
  f = open("traindata_faces_simple.txt", "a")
  f.write('\n,'.join(str(item) for item in trainingData))
  f.close()
  # if classifier == KNN

  if(options.classifier == "knnClassifier"):
    accuracy, K = classifier.preprocessData(options.training)
    return accuracy, K
  else:
    # Conduct training and testing
    print("Training...")
    classifier.train(trainingData, trainingLabels, validationData, validationLabels)

    print("Validating...")
    guesses = classifier.classify(validationData)
    correct1 = [guesses[i] == validationLabels[i] for i in range(len(validationLabels))].count(True)
    print(str(correct1), ("correct out of " + str(len(validationLabels)) + ": %.1f%%") % (100.0 * correct1 / len(validationLabels)))

    print("Testing...")
    guesses = classifier.classify(testData)
    correct2 = [guesses[i] == testLabels[i] for i in range(len(testLabels))].count(True)
    print(str(correct2), ("correct out of " + str(len(testLabels)) + ": %.1f%%") % (100.0 * correct2 / len(testLabels)))
    analysis(classifier, guesses, testLabels, testData, rawTestData, printImage)

  return correct1, correct2

if __name__ == '__main__':
  # Read inputt
  # Run classifier
  # t=0
  # for i in range(10):
  #   t+=500
  #   st=time.process_time()
  #   args, options = readCommand(['-d','digits','-c','perceptron','-t', str(t),'-k','1','-f'])
  #   # MAIN RUN : args, options = readCommand( sys.argv[1:] )

  #   correct1, correct2 = runClassifier(args, options)
  #   print("correcttt")
  #   print(correct1)
  #   print(correct2)

  #   et=time.process_time()
  #   print("Time: ",et-st)

  t=0
  validating_mean_acc = []
  validating_std_acc = []
  test_mean_acc = []
  test_std_acc = []

  # currently checking only for 10% data
  for i in range(2):
    t += 1000
    # MAIN RUN : args, options = readCommand( sys.argv[1:] )
    validating_acc = []
    test_acc = []
    bestK = []

    for j in range(2):
      st=time.process_time()
      args, options = readCommand(['-d','digits','-c','perceptron','-t', str(t),'-k','1','-f','-r'])
      #print(args)
      #print(options)
      correct1, correct2 = runClassifier(args, options)
      et=time.process_time()
      print("Time: ",et-st)
      validating_acc.append(correct1)
      test_acc.append(correct2)

    print(validating_acc)
    print(test_acc)
    print(bestK)
    isKnn = False
    if(test_acc[0] == 0):
      # knn
      test_mean_acc.append(np.mean(validating_acc))
      test_std_acc.append(np.std(validating_acc))
      isKnn = True
    else:
      validating_mean_acc.append(np.mean(validating_acc))
      validating_std_acc.append(np.std(validating_acc))
      test_mean_acc.append(np.mean(test_acc))
      test_std_acc.append(np.std(test_acc))

  if(isKnn):
    print("Testing : MEAN, STD")
    print(test_mean_acc)
    print(test_std_acc)
  else:
    print("Validation : MEAN, STD")
    print(validating_mean_acc)
    print(validating_std_acc)
    print("Testing : MEAN, STD")
    print(test_mean_acc)
    print(test_std_acc)

#python dataClassifier.py -c naiveBayes -d digits -t 1000 -f -o -1 3 -2 6 -k 2.5
# runClassifier(args, options)


import os

import PyPDF2
import sys

def pdf_merger(pdf_list):
    merger = PyPDF2.PdfMerger()
    for pdf in pdf_list:
        pdf_name = os.path.basename(pdf)
        print(f"Successfully merged {pdf_name}")
        merger.append(pdf)
    print("------------COMPLETED------------")
    merger.write('merged.pdf')

# takes in command line arguments of the pdf file names that you want to merge
def main():
    inputs = sys.argv[1:]
    pdf_merger(inputs)

if __name__ == "__main__":
    main()


class Solution:
    def maxLevelSum(self, root: TreeNode):
        # start from root node?
        max, level, maxLevel = -float('inf'), 0, 0
        q = collections.deque()
        q.append(root)
        while q:
            level += 1
            sum = 0
            for _ in range(len(q)):
                node = q.popleft()
                sum += node.val
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
            # Check if the sum of nodes is greater than the max of what we have
            if max < sum:
                max, maxLevel = sum, level
        return maxLevel


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        """buy=0
        sell=1
        max_profit=0
        while sell<len(prices):
            current=prices[sell]-prices[buy]
            if prices[buy]<prices[sell]:
                max_profit +=(current)
            buy +=1
            sell +=1
        return max_profit"""
        profit=0
        for i in range(len(prices)-1):
            if prices[i]<prices[i+1]:
                profit += prices[i+1]-prices[i]
        return profit


'''
APPROACH:
1 Find an empty square to use
2 Start putting numbers on it
3 check if that number works
	- check row (left/right)
	- check column (up/down)
	- check grid (3x3 square)
4 Continue proccess for every empty square
5 Move back if number doesn't work (backtracking)
 - check if board is solved
 - loop through 1-9
 - check if they work
 - set empty square to it
 - if it doesn't work, go back change the old square
 - continue with entire board until there are no more empty
   squares, meaning the board is solved.
6 Show the start and end through a board visualizer
7 Possibly add a GUI using pygame to show the board being solved.

'''

#zeroes indicate a blank space

b1 = [
    [5,3,0,0,7,0,0,0,0],
    [6,0,0,1,9,5,0,0,0],
    [0,9,8,0,0,0,0,6,0],
    [8,0,0,0,6,0,0,0,3],
    [4,0,0,8,0,3,0,0,1],
    [7,0,0,0,2,0,0,0,6],
    [0,6,0,0,0,0,2,8,0],
    [0,0,0,4,1,9,0,0,5],
    [0,0,0,0,8,0,0,7,9]
]

b2 = [
    [7,8,0,4,0,0,1,2,0],
    [6,0,0,0,7,5,0,0,9],
    [0,0,0,6,0,1,0,7,8],
    [0,0,7,0,4,0,2,6,0],
    [0,0,1,0,5,0,9,3,0],
    [9,0,4,0,6,0,0,0,5],
    [0,7,0,3,0,0,0,1,2],
    [1,2,0,0,0,7,4,0,0],
    [0,4,9,2,0,6,0,0,7]
]

#Choose a board

while True:
	sudoku_puzzle = input("Which board would you like to have solved, board 1 or 2? Enter an integer for the board of your choice.")
	if int(sudoku_puzzle) == 1:
		board = b1
		break
	elif int(sudoku_puzzle) == 2:
		board = b2
		break
	else:
		print("Enter an integer only! Please try again.")
		continue

#prints the sudoku board
def printSudoku(board):
	for x in range(len(board)):
		if x % 3 == 0 and x != 8 and x != 0:
			print("-------------------------------")

		for y in range(len(board[1])):

			if y % 3 == 0 and y != 8 and y != 0:
				print(" | ", end = "")

			if y != 8:
				print(str(board[x][y]), " ", end="")
			else:
				print(str(board[x][y]))

#find an empty square on the board, along with its index location and returns it as a tuple.
def findSquare(board):
	for row in range(9):
		for column in range(9):
			if board[row][column] == 0:
				return (row,column) # y,x
	return False

#goes throught numbers 1-9 and checks which ones are valid in the position

def checkNumberValid(board, number, position):
	#CHECK COLUMN:
	for row in board:
		if row[position[1]] == number and row != board[position[0]]:
			return False

	#CHECK GRID:
	#set up x variable

	if 0 <= (position[1]) /3 < 1:
		x_coor = 0

	elif 1 <= position[1]/3 < 2:
		x_coor = 1

	elif 2 <= position[1]/3 < 3:
		x_coor = 2

	#set up y variable
	if 0 <= position[0]/3 < 1:
		y_coor = 0

	elif 1 <= position[0]/3 < 2:
		y_coor = 1

	elif 2 <= position[0]/3 < 3:
		y_coor = 2

	#Go through each grid and check for repeated numbers
	for y in range(y_coor*3, y_coor*3+3):
		for x in range(x_coor*3, x_coor*3+3):
			if board[y][x] == number and board[position[0]][position[1]] != board[y][x]:
				return False

	#CHECK ROW:
	for column in range(9):
		if board[position[0]][column] == number and column != position[1]:
			return False

	return True

def solvedBoard(board):
	#printSudoku(board)
	#print('------------------------ \n ------------------------ \n ------------------------')
	empty_square = findSquare(board)
	if not empty_square:
		return True
	else:
		row, column = empty_square

	for ans in range(1,10):
		if checkNumberValid(board, ans, (row, column)):
			board[row][column] = ans

			if solvedBoard(board):
				return True

			board[row][column] = 0

	return False

#Final script
print('\n\n\nHere is the starting board: ')
printSudoku(board)
solvedBoard(board)
print('\n\n\nHere is the solved board!')
printSudoku(board)
print("\n\nNOTE: If you are seeing an unsolved board, it means that your sudoku board was invalid. Please change it to a valid puzzle and try again.\n")


from gtts import gTTS
import os

# Simple sentence
textContent = 'Hello, I am alias of Phu, who is currently PhD student at INRIA'

# Complex one, a paragraph of an article
with open('article.txt', 'r') as myFile:
    data=myFile.read()

# Complex Vietnamese text
with open('vietnamese.txt', 'rb') as myVietnameseText:
    vietnamese=myVietnameseText.read().decode('utf8', 'ignore')

tts = gTTS(text=vietnamese,lang='vi')
tts.save("sample.mp3")
os.system("mpg123 sample.mp3")
os.system("rm sample.mp3")


#  Write a function that takes in a string and returns a 'True'
#  if the parentheses are balances or 'False' if they are not

# Input: s = "()[]{}"
# Output: True

def validParens(str):
    dict = {"[": "]", "{": "}", "(": ")"}
    stack = []

    for char in str:
        if char in dict:
            stack.append(char)
        elif stack and char == dict[stack[-1]]:
            stack.pop()
        else:
            return False

    return len(stack) == 0

print(validParens("[{[()]}]()"))


import os.path as osp
import os
import pickle as pkl

if not osp.exists("bbox_labels"):
    os.mkdir("bbox_labels")

annotations_dir = "GroceryDataset_part2/BrandImagesFromShelves/"
save_dir = "bbox_labels"

classes = [str(x) for x in range(1,11)]

bbox_labels = {}

for cls in classes:

    cls_dir = osp.join(annotations_dir, cls)

    #Get the list of images
    class_ims = os.listdir(cls_dir)

    im_names = ["{}.JPG".format(name.split(".")[0]) for name in class_ims]

    im_names = [x for x in im_names if not(x[:6] == "C1_P01" or x[:6] == "C2_P01")]

    box_cords = [(name.split(".")[1].split("_")[-4:]) for name in class_ims]

    for i, name in enumerate(im_names):

        try:
            bbox_labels[name]
        except KeyError:
            bbox_labels[name] = []

        try:
            box_cord = [int(a) for a in box_cords[i]]
        except ValueError:
            continue

        box_cord[2], box_cord[3] = box_cord[0] + box_cord[2], box_cord[1] + box_cord[3]

        bbox_labels[name].append((int(cls), box_cord))

pkl.dump(bbox_labels, open("bboxes.pkl", "wb"))


# Using numpy to order glyphs by black pixel count

import py5
import numpy as np

glyphs = '#1234567890.XZ*'
W = 60

def setup():
    py5.size(900, 120)
    f = py5.create_font('Inconsolata Black', 60)
    pg = py5.create_graphics(W, W)  # an offscreen buffer
    pg.no_smooth()  # to disable antialiasing, it must be done before begin_draw()
    pg.begin_draw()
    pg.text_size(W)
    pg.fill(0)
    pg.text_font(f)
    pg.text_align(py5.CENTER, py5.CENTER)
    pg.end_draw()
    counts = []
    for glyph in glyphs:
        pg.begin_draw()
        pg.background(255)
        pg.text(glyph, W / 2, W / 2 - 2)
        pg.load_np_pixels()
        pxs = pg.np_pixels
        black = [255, 0, 0, 0]  # Alpha, R, G, B
        black_count = np.count_nonzero(np.all(pxs==black, axis=2))
        pg.end_draw()
        py5.image(pg, 0, 0)
        py5.translate(W, 0)
        counts.append((black_count, glyph, pg.copy()))
    counts.sort()
    py5.reset_matrix()
    py5.tint(200, 200, 0)
    for count, glyph, img in counts:
        py5.image(img, 0, W)
        py5.translate(W, 0)

py5.run_sketch()


from fastai.vision.all import *

url = "https://github.com/deep-diver/fastai-course/raw/master/data/oxford-iiit-pet-tiny.tar.gz"
path = untar_data(url)/"images"
files = get_image_files(path)

dls = ImageDataLoaders.from_name_func(path, files, lambda f: f[0].isupper(),
                                      bs=4, item_tfms=Resize(224))

learner = cnn_learner(dls, models.resnet18)

model = learner.model

# ResNet의 splitter 함수의 구현체
# - https://github.com/fastai/fastai/blob/6e44b354f4d12bdfa2c9530f38f851c54a05764d/fastai/vision/learner.py#L105
# - def  _resnet_split(m):
#       return L(m[0][:6], m[0][6:], m[1:]).map(params)

layers = list(model.children())
print(f'모델의 길이: {len(layers)}')

layers_body = list(layers[0].children())
layers_head = list(layers[1].children())
print(f'부분 모델(몸통)의 길이: {len(layers_body)}')
print(f'부분 모델(머리)의 길이: {len(layers_head)}')


#!/usr/bin/env python

import socketio
import eventlet
import eventlet.wsgi
import time
eventlet.monkey_patch(socket=True, select=True, time=True)
from flask import Flask, render_template

from bridge import Bridge
from conf import conf

#sio = socketio.Server()
sio = socketio.Server(async_mode='eventlet')
app = Flask(__name__)
msgs = []

dbw_enable = False

@sio.on('connect')
def connect(sid, environ):
    print("connect ", sid)

def send(topic, data):
    s = 1
    msgs.append((topic, data))
    #sio.emit(topic, data=json.dumps(data), skip_sid=True)

bridge = Bridge(conf, send)

@sio.on('telemetry')
def telemetry(sid, data):
    global dbw_enable
    if data["dbw_enable"] != dbw_enable:
        dbw_enable = data["dbw_enable"]
        bridge.publish_dbw_status(dbw_enable)
    bridge.publish_odometry(data)
    for i in range(len(msgs)):
        topic, data = msgs.pop(0)
        sio.emit(topic, data=data, skip_sid=True)

@sio.on('control')
def control(sid, data):
    bridge.publish_controls(data)

@sio.on('obstacle')
def obstacle(sid, data):
    bridge.publish_obstacles(data)

@sio.on('lidar')
def obstacle(sid, data):
    bridge.publish_lidar(data)

@sio.on('trafficlights')
def trafficlights(sid, data):
    bridge.publish_traffic(data)

@sio.on('image')
def image(sid, data):
    # Limit the frequency of images notification to lighten the load
    now = time.time()
    if now - image.last_image_time >= .2:  # No less than this time interval between two images, in seconds
        image.last_image_time = now
        bridge.publish_camera(data)
image.last_image_time = time.time()

if __name__ == '__main__':

    # wrap Flask application with engineio's middleware
    app = socketio.Middleware(sio, app)

    # deploy as an eventlet WSGI server
    eventlet.wsgi.server(eventlet.listen(('', 4567)), app)


import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime
import os

matplotlib.rcParams.update(
    {
        "font.family": "sans-serif",
        "font.size": 8,
        "mathtext.fontset": "stix",
        "font.sans-serif": ["Arial"],
        "axes.unicode_minus": False,
    }
)

def vis_waves_preds_targets(
    waveforms: np.ndarray,
    preds: np.ndarray,
    targets: np.ndarray,
    sampling_rate=None,
    save_dir="./",
    format="png",
):
    fig = plt.figure()
    num_row = waveforms.shape[0] + preds.shape[0] + targets.shape[0]
    for idx, wave in enumerate(waveforms):
        plt.subplot(num_row, 1, idx + 1)
        if sampling_rate is None:
            plt.plot(wave, "-", color="k", linewidth=0.15, alpha=0.8)
        else:
            x = [i / sampling_rate for i in range(len(wave))]
            plt.plot(x, wave, "-", color="k", linewidth=0.15, alpha=0.8)
        plt.text(
            0.001,
            0.95,
            f"Channel-{idx}",
            horizontalalignment="left",
            verticalalignment="top",
            transform=plt.gca().transAxes,
            fontsize="small",
            fontweight="normal",
        )
        plt.ylim(-1, 1)
        plt.yticks([])

    for idx, data in enumerate(preds):
        plt.subplot(num_row, 1, waveforms.shape[0] + idx + 1)
        if sampling_rate is None:
            plt.plot(data, "-", color="k", linewidth=0.15, alpha=0.8)
        else:
            x = [i / sampling_rate for i in range(len(data))]
            plt.plot(x, data, "-", color="k", linewidth=0.15, alpha=0.8)
        plt.text(
            0.001,
            0.95,
            f"Pred-{idx}",
            horizontalalignment="left",
            verticalalignment="top",
            transform=plt.gca().transAxes,
            fontsize="small",
            fontweight="normal",
        )
        plt.yticks([])

    for idx, data in enumerate(targets):
        plt.subplot(num_row, 1, waveforms.shape[0] + preds.shape[0] + idx + 1)
        if sampling_rate is None:
            plt.plot(data, "-", color="k", linewidth=0.15, alpha=0.8)
        else:
            x = [i / sampling_rate for i in range(len(data))]
            plt.plot(x, data, "-", color="k", linewidth=0.15, alpha=0.8)
        plt.text(
            0.001,
            0.95,
            f"Target-{idx}",
            horizontalalignment="left",
            verticalalignment="top",
            transform=plt.gca().transAxes,
            fontsize="small",
            fontweight="normal",
        )
        plt.yticks([])

    if sampling_rate is None:
        plt.xlabel("Sample points")
    else:
        plt.xlabel("Time (s)")
    plt.tight_layout()
    plt.subplots_adjust(wspace=0.1, hspace=0.0)

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

    plt.savefig(
        f'{os.path.join(save_dir,datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))}.{format}',
        dpi=400,
    )
    plt.close()

def vis_phase_picking(
    waveforms: np.ndarray,
    waveforms_labels:list,
    preds: np.ndarray,
    true_phase_idxs: list,
    true_phase_labels:list,
    pred_phase_labels:list,
    sampling_rate:int=None,
    save_name="",
    save_dir="./",
    formats=["png"],
):
    fig = plt.figure(figsize=(10/2.54,10/2.54))
    if sampling_rate is None:
        x = list(range(len(waveforms[0])))
    else:
        x = [i / sampling_rate for i in range(len(waveforms[0]))]
    num_row = waveforms.shape[0] + 1
    tmp_min = np.min(waveforms)
    tmp_max = np.max(waveforms)
    number_map = {0:"(a)",1:"(b)",2:"(c)",3:"(d)"}
    for idx, wave in enumerate(waveforms):
        plt.subplot(num_row, 1, idx + 1)

        plt.plot(x, wave, "-", color="k", linewidth=1, alpha=0.8,label=waveforms_labels[idx])
        if idx==0 and true_phase_idxs:
            plt.vlines(x=[true_phase_idxs[0]],ymin=tmp_min*1.1,ymax=tmp_max*1.1,colors=["C1"],linestyles="solid",label=true_phase_labels[0])
            plt.vlines(x=[true_phase_idxs[1]],ymin=tmp_min*1.1,ymax=tmp_max*1.1,colors=["C5"],linestyles="solid",label=true_phase_labels[1])
        plt.ylim(tmp_min*1.2, tmp_max*1.2)
        plt.ylabel('Amplitude')
        plt.yticks([])
        plt.xticks([])
        plt.text(0.05, 0.78, number_map[idx], horizontalalignment='center',
            transform=plt.gca().transAxes, fontsize=8, fontweight="normal", bbox=None)
        legend=plt.legend(loc='upper right', fontsize=8, ncol=1)
        legend.get_frame().set_linewidth(0.75)

    plt.subplot(num_row, 1, num_row)
    plt.text(0.05, 0.78, number_map[3], horizontalalignment='center',
            transform=plt.gca().transAxes, fontsize=8, fontweight="normal", bbox=None)
    plt.plot(x,preds[0], f'-.C0', linewidth=1, alpha=0.8,label=pred_phase_labels[0])
    plt.ylabel('Probability')
    plt.plot(x,preds[1], f'--C1', linewidth=1, alpha=0.8,label=pred_phase_labels[1])
    plt.ylabel('Probability')
    plt.plot(x,preds[2], f'--C5', linewidth=1, alpha=0.8,label=pred_phase_labels[2])
    plt.ylabel('Probability')

    if sampling_rate is None:
        plt.xlabel("Samples")
    else:
        plt.xlabel("Time (s)")

    legend=plt.legend(loc='upper right', fontsize=8, ncol=1)

    _width = 0.75
    legend.get_frame().set_linewidth(_width)

    ax=plt.gca()
    ax.spines['top'].set_linewidth(_width)
    ax.spines['right'].set_linewidth(_width)
    ax.spines['bottom'].set_linewidth(_width)
    ax.spines['left'].set_linewidth(_width)

    plt.tight_layout()
    plt.gcf().align_labels()
    plt.subplots_adjust(wspace=0.1, hspace=0.05)

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

    if not isinstance(formats,list):
        formats=[formats]

    for fmt in formats:
        plt.savefig(
            f'{os.path.join(save_dir,datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+save_name)}.{fmt}',
            dpi=400,
        )
    plt.close()


import math
import collections
import torch
import torch.nn as nn

class PositionalEmbedding(nn.Module):
    '''
    Modified from Annotated Transformer
    http://nlp.seas.harvard.edu/2018/04/03/attention.html
    '''
    def __init__(self, d_model, max_len=1024):
        super(PositionalEmbedding, self).__init__()
        pe = torch.zeros((max_len, d_model), requires_grad=False).float()
        position = torch.arange(0, max_len).float().unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0)
        self.register_buffer('pe', pe)

    def forward(self, x):
        return self.pe[:, :x.size(1)]

class InputPositionEmbedding(nn.Module):
    def __init__(self, vocab_size=None, embed_dim=None, dropout=0.1,
                init_weight=None, seq_len=None):
        super(InputPositionEmbedding, self).__init__()
        self.embed = nn.Embedding(vocab_size, embed_dim)
        self.dropout = nn.Dropout(dropout)
        self.position_embed = PositionalEmbedding(embed_dim, max_len=seq_len)
        self.reproject = nn.Identity()
        if init_weight is not None:
            self.embed = nn.Embedding.from_pretrained(init_weight)
            self.reproject = nn.Linear(init_weight.size(1), embed_dim)

    def forward(self, inputs):
        x = self.embed(inputs)
        x = x + self.position_embed(inputs)
        x = self.reproject(x)
        x = self.dropout(x)
        return x

class AggregateLayer(nn.Module):
    def __init__(self, d_model=None, dropout=0.1):
        super(AggregateLayer, self).__init__()
        self.attn = nn.Sequential(collections.OrderedDict([
            ('layernorm', nn.LayerNorm(d_model)),
            ('fc', nn.Linear(d_model, 1, bias=False)),
            ('dropout', nn.Dropout(dropout)),
            ('softmax', nn.Softmax(dim=1))
        ]))

    def forward(self, context):
        '''
        Parameters
        ----------
        context: token embedding from encoder (Transformer/LSTM)
                (batch_size, seq_len, embed_dim)
        '''

        weight = self.attn(context)
        # (batch_size, seq_len, embed_dim).T * (batch_size, seq_len, 1) *  ->
        # (batch_size, embed_dim, 1)
        output = torch.bmm(context.transpose(1, 2), weight)
        output = output.squeeze(2)
        return output

class GlobalPredictor(nn.Module):
    def __init__(self, d_model=None, d_h=None, d_out=None, dropout=0.5):
        super(GlobalPredictor, self).__init__()
        self.predict_layer = nn.Sequential(collections.OrderedDict([
            ('batchnorm', nn.BatchNorm1d(d_model)),
            ('fc1', nn.Linear(d_model, d_h)),
            ('tanh', nn.Tanh()),
            ('dropout', nn.Dropout(dropout)),
            ('fc2', nn.Linear(d_h, d_out))
        ]))

    def forward(self, x):
        x = self.predict_layer(x)
        return x

class SequenceLSTM(nn.Module):
    """Container module with an encoder, a recurrent module, and a decoder."""

    def __init__(self, d_input=None, d_embed=20, d_model=128,
                vocab_size=None, seq_len=None,
                dropout=0.1, lstm_dropout=0,
                nlayers=1, bidirectional=False,
                proj_loc_config=None):
        super(SequenceLSTM, self).__init__()

        self.embed = InputPositionEmbedding(vocab_size=vocab_size,
                    seq_len=seq_len, embed_dim=d_embed)

        self.lstm = nn.LSTM(input_size=d_input,
                            hidden_size=d_model//2 if bidirectional else d_model,
                            num_layers=nlayers, dropout=lstm_dropout,
                            bidirectional=bidirectional)
        self.drop = nn.Dropout(dropout)
        self.proj_loc_layer = proj_loc_config['layer'](
                    proj_loc_config['d_in'], proj_loc_config['d_out']
                )

    def forward(self, x, loc_feat=None):
        x = self.embed(x)
        if loc_feat is not None:
            loc_feat = self.proj_loc_layer(loc_feat)
            x = torch.cat([x, loc_feat], dim=2)
        x = x.transpose(0, 1).contiguous()
        x, _ = self.lstm(x)
        x = x.transpose(0, 1).contiguous()
        x = self.drop(x)
        return x

class LSTMPredictor(nn.Module):
    def __init__(self, d_embed=20, d_model=128, d_h=128, d_out=1,
                vocab_size=None, seq_len=None,
                dropout=0.1, lstm_dropout=0, nlayers=1, bidirectional=False,
                use_loc_feat=True, use_glob_feat=True,
                proj_loc_config=None, proj_glob_config=None):
        super(LSTMPredictor, self).__init__()
        self.seq_lstm = SequenceLSTM(
            d_input=d_embed + (proj_loc_config['d_out'] if use_loc_feat else 0),
            d_embed=d_embed, d_model=d_model,
            vocab_size=vocab_size, seq_len=seq_len,
            dropout=dropout, lstm_dropout=lstm_dropout,
            nlayers=nlayers, bidirectional=bidirectional,
            proj_loc_config=proj_loc_config)
        self.proj_glob_layer = proj_glob_config['layer'](
            proj_glob_config['d_in'], proj_glob_config['d_out']
        )
        self.aggragator = AggregateLayer(
            d_model = d_model + (proj_glob_config['d_out'] if use_glob_feat else 0))
        self.predictor = GlobalPredictor(
            d_model = d_model + (proj_glob_config['d_out'] if use_glob_feat else 0),
            d_h=d_h, d_out=d_out)

    def forward(self, x, glob_feat=None, loc_feat=None):
        x = self.seq_lstm(x, loc_feat=loc_feat)
        if glob_feat is not None:
            glob_feat = self.proj_glob_layer(glob_feat)
            x = torch.cat([x, glob_feat], dim=2)
        x = self.aggragator(x)
        output = self.predictor(x)
        return output

if __name__ == "__main__":
    model = LSTMPredictor(
        d_model=128, d_h=128, nlayers=1,
        vocab_size=21, seq_len=500,
        proj_glob_config = {'layer':nn.Linear, 'd_in':768, 'd_out':128},
        proj_loc_config = {'layer':nn.Linear, 'd_in':500, 'd_out':128},
        )
    x = torch.randint(0, 21, (128, 500))
    glob_feat = torch.rand((128, 500, 768))
    loc_feat = torch.rand((128, 500, 500))
    y = model(x, glob_feat=glob_feat, loc_feat=loc_feat)
    print(y.size())


firstWord = input("please enter the first word to compare: ")
firstWord = firstWord.lower()
secondWord = input("please enter the second word to compare: ")
secondWord = secondWord.lower()

firstWord = list(firstWord)
secondWord = list(secondWord)
firstWord.insert(0, '')
secondWord.insert(0, '')

calculationMatrix = []
n = len(firstWord)
m = len(secondWord)

for x in range(0, m):
    calculationMatrix.append([])
    for y in range(0, n):
        calculationMatrix[x].append(0)

for i in range(0, m):
    calculationMatrix[i][0] = i

for j in range(0, n):
    calculationMatrix[0][j] = j

for j in range(1, n):
    for i in range(1, m):
        if firstWord[j] == secondWord[i]:
            substitutionCost = 0
        else:
            substitutionCost = 1
        calculationMatrix[i][j] = min(calculationMatrix[i-1][j] + 1, calculationMatrix[i][j-1] + 1, calculationMatrix[i-1][j-1] + substitutionCost)

print(calculationMatrix)


# -*- coding: utf8 -*-
from timeit import default_timer
import time
import functools
from flask import request, jsonify, Blueprint
from . import storage
from pprint import pprint as pp

CONF = {}
collection = None

class Measurement(object):
    """represents an endpoint measurement"""
    DECIMAL_PLACES = 6

    def __init__(self, name, args, kwargs, method, context=None):
        super(Measurement, self).__init__()
        self.context = context
        self.name = name
        self.method = method
        self.args = args
        self.kwargs = kwargs
        self.startedAt = 0
        self.endedAt = 0
        self.elapsed = 0

    def __json__(self):
        return {
            "name": self.name,
            "args": self.args,
            "kwargs": self.kwargs,
            "method": self.method,
            "startedAt": self.startedAt,
            "endedAt": self.endedAt,
            "elapsed": self.elapsed,
            "context": self.context
        }

    def __str__(self):
        return str(self.__json__())

    def start(self):
        # we use default_timer to get the best clock available.
        # see: http://stackoverflow.com/a/25823885/672798
        self.startedAt = time.time()

    def stop(self):
        self.endedAt = time.time()
        self.elapsed = round(
            self.endedAt - self.startedAt, self.DECIMAL_PLACES)

def measure(f, name, method, context=None):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        if not collection:
            raise Exception(
                "before measuring anything, you need to call init_app()")

        measurement = Measurement(name, args, kwargs, method, context)
        measurement.start()

        try:
            returnVal = f(*args, **kwargs)
        except Exception as e:
            raise e
        finally:
            measurement.stop()
            if CONF.get("verbose", False):
                pp(measurement.__json__())
            collection.insert(measurement.__json__())

        return returnVal

    return wrapper

def wrapHttpEndpoint(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        context = {
            "url": request.base_url,
            "args": dict(request.args.items()),
            "form": dict(request.form.items()),
            "body": request.data.decode("utf-8", "strict"),
            "headers": dict(request.headers.items()),
            "func": request.endpoint}
        endpoint_name = str(request.url_rule)
        wrapped = measure(f, endpoint_name, request.method, context)
        return wrapped(*args, **kwargs)
    return wrapper

def wrapAppEndpoints(app):
    """
    wraps all endpoints defined in the given flask app to measure how long time
    each endpoints takes while being executed. This wrapping process is
    supposed not to change endpoint behaviour.
    """
    for endpoint, func in app.view_functions.items():
        app.view_functions[endpoint] = wrapHttpEndpoint(func)

def profile(*args, **kwargs):
    """
    http endpoint decorator
    """
    def wrapper(f):
        return wrapHttpEndpoint(f)
    return wrapper

def registerInternalRouters(app):
    """
    These are the endponts which are used to display measurements in the
    flask-profiler dashboard.

    Note: these should be defined after wrapping user defined endpoints
    via wrapAppEndpoints()
    """
    urlPath = CONF.get("endpointRoot", "flask-profiler")

    fp = Blueprint(
        'flask-profiler', __name__,
        url_prefix="/" + urlPath,
        static_folder="static/dist/", static_url_path='/static/dist')

    @fp.route("/".format(urlPath))
    def index():
        return fp.send_static_file("index.html")

    @fp.route("/api/measurements/".format(urlPath))
    def filtermeasurements():
        args = dict(request.args.items())
        measurements = collection.filter(args)
        return jsonify({"measurements": list(measurements)})

    @fp.route("/api/measurements/grouped/".format(urlPath))
    def getmeasurementsSummary():
        args = dict(request.args.items())
        measurements = collection.getSummary(args)
        return jsonify({"measurements": list(measurements)})

    @fp.route("/api/measurements/<measurementId>".format(urlPath))
    def getContext(measurementId):
        return jsonify(collection.get(measurementId))

    @fp.route("/api/measurements/timeseries/".format(urlPath))
    def getReqiestsTimeseries():
        args = dict(request.args.items())
        return jsonify({"series": collection.getTimeseries(args)})

    @fp.route("/api/measurements/methodDistribution/".format(urlPath))
    def getMethodDistribution():
        args = dict(request.args.items())
        return jsonify({
            "distribution": collection.getMethodDistribution(args)})

    app.register_blueprint(fp)

def init_app(app):
    global collection, CONF

    try:
        CONF = app.config["flask_profiler"]
    except Exception as e:
        raise Exception(
            "to init flask-profiler, provide "
            "required config through flask app's config. please refer: "
            "https://github.com/muatik/flask-profiler")

    if not CONF.get("enabled", False):
        return

    collection = storage.getCollection(CONF.get("storage", {}))

    wrapAppEndpoints(app)
    registerInternalRouters(app)

    print(" * flask-profiler is enabled.")


import os
import torch
import model_training
from load_model import load_model

d = []

def main():
    ans = input('Choose:\n1) Обучить модель на основе данных /data/fa-tg/\n2) Оценить модель\n0) Выход')
    if ans == '1':
        model_training.train_model()
    elif ans == '2':
        load_model()
    elif ans == '3':
        raise SystemExit()
    else:
        main()

if __name__ == '__main__':
    main()


class Solution:
    def diagonalSum(self, mat: List[List[int]]) -> int:
        s = 0
        n = len(mat)
        for i in range(n):
            s += mat[i][i] + mat[i][n - i -1]
            i += 1

        center = mat[n // 2][n // 2]
        return s if len(mat) % 2 == 0 else s - center


class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        if not s:
            return 0
        res = 0
        a = 0
        for i in range(len(s)-1,-1,-1):
            num = ord(s[i]) - ord('A') + 1
            res += num * pow(26,a)
            a += 1
        return res
s = Solution()
print(s.titleToNumber('AB'))


# 119A - epic game
def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x
    return gcd(y, x % y)

a, b, n = map(int, input().split())
i = 0
while n >= 0:
    n = n - gcd(a, n)
    i += 1
    if n < 0:
        print(i % 2)
        break
    n = n - gcd(b, n)
    i += 1
    if n < 0:
        print(i % 2)
        break


import os
import time
import yaml
from yolov5 import load_model, parse_result
from visulize import visulize

with open("coco.yaml", 'r') as f:
    name_dict = yaml.safe_load(f)

def detect(image_file, display=True):
    model = load_model()
    if os.path.isfile(image_file):
        result = model(image_file)
        result = parse_result(result)
        if visulize:
            labels = ["{}:{:.2f}".format(name_dict[name], conf) for name, conf in zip(result[:, -1], result[:, -2])]
            visulize(img=image_file, boxes=result[:, :4], labels=labels)

    elif os.path.isdir(image_file):
        file_list = os.listdir(image_file)
        for frame_index, file_name in enumerate(file_list):
            img_file = os.path.join(image_file, file_name)
            time_start = time.time()
            result = model(img_file)
            time_end = time.time()
            print(f"Process {frame_index}/{len(file_list)}, FPS: {1 / (time_end - time_start)}, file name:{file_name} ")
            result = parse_result(result)
            if visulize:
                labels = ["{}:{:.2f}".format(name_dict[name], conf) for name, conf in zip(result[:, -1], result[:, -2])]
                visulize(img=img_file, boxes=result[:, :4], labels=labels)
            print(f"Process {frame_index}/{len(file_list)}, FPS: {1 / (time_end - time_start)}, file name:{file_name} ")

if __name__ == "__main__":
    img_dir = "E:\\dataset\\HT21\\train\\HT21-02\\img1\\"
    detect(img_dir)


# 2018-08-20 10:21:09 #
from pathlib import Path
from datetime import datetime
from json import dump
import re
import sqlite3 as sqlite

root = Path('/Users/HD6904/Erik/Procedure Status/outlines')
db_file = Path('/Users/HD6904/Erik/Procedure Status/twdb.sqlite3')
raw_file = Path('/Users/HD6904/Erik/Procedure Status/twdb.json')
rpdw_re = re.compile(r'^([A-Z]{2,6}\d{4}\S*).*\n?(?:\n\S*\d{6})+', re.I|re.M)
draw_re = re.compile(r'^(?:\d-)?\d{6}\S*', re.M)
date_re = re.compile(r'DUE.+?(\d\d)[-/ ]?(\w+)[-/ ]?(\d\d)', re.I)

def main():
    # REMOVE SUDS FROM OUTLINES
    # REFORMAT PROCS WHEN ADDING TO DB
    # CREATE 2 TABLES, ONE FOR RAW, AND THE OTHER FOR FORMATTED DATA
    # force formatting (add system: and id: to all)
    docs = []
    for i, ftxt in enumerate(root.glob('*.txt')):
        print(f'Processing File {i + 1}: {ftxt.name}')
        txt = ftxt.read_text().upper()
        meta_data = txt.split('\n\n')[0].splitlines()
        docs.append({
            'contents': [proc for proc in scrape_doc(txt)]
        }.extend(dict(tuple(ln.split(':', 1)) for ln in meta_data)))
    dump_data(docs)
    with open(raw_file, 'w') as f:
        dump(docs, f, indent=2)

def scrape_doc(contents):
    for match in re.finditer(rpdw_re, contents):
        proc = ''.join(c for c in match[1] if c.isalnum())
        draws = ', '.join(sorted(draw_re.findall(match[0])))
        yield [proc] + draws

def dump_data(data):
    # raw_app('\t'.join(doc_num, proc, str(doc_date), draws))
    # rpdw = (proc, draws)
    # if not rpdw in procs or procs[rpdw][0] < doc_date:
    #     procs[rpdw] = (doc_date, doc_num)
    conn = sqlite3.connect(db_file)
    c = conn.cursor()

    c.execute('CREATE TABLE manuals'
                 '(num int, vol int, rev int, sys text, date text, cust text, proj text, rig text)'
    '')
    for doc in data:
        procs = doc.pop('contents')
        c.execute('''CREATE TABLE manuals
                     num int, vol int, rev int, sys text, date text, cust text, proj text, rig text)

        ''')
    c.execute('''CREATE TABLE stocks
              (date text, trans text, symbol text, qty real, price real)''')
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    conn.commit()
    conn.close()

if __name__ == '__main__':
    main()


"""
Unit and regression test for the measure module.
"""

# Import package, test suite, and other packages as needed
import ising_model
import numpy as np

def test_spinconfig():
    """Test that spinconfig function calculates what we expect."""

    test_spin = ising_model.SpinConfig(8)

    expected_N = 8
    calculated_N = test_spin.N

    expected_iMax = 256
    calculated_iMax = test_spin.iMax

    expected_input_decimal = [0, 0, 0, 0, 1, 0, 1, 0]
    calculated_input_decimal = test_spin.input_decimal(10)

    expected_magnetization = -4
    calculated_magnetization = test_spin.magnetization()

    # translate '+' '-' intro list
    test_spin2 = ising_model.SpinConfig()
    expected_input_p_m = [1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1]
    calculated_input_p_m = test_spin2.input_p_m("++-+---+--+")

    assert expected_N == calculated_N
    assert expected_iMax == calculated_iMax
    assert expected_input_decimal == calculated_input_decimal
    assert expected_input_p_m == calculated_input_p_m
    assert expected_magnetization == calculated_magnetization


from pathlib import Path
import xml.etree.ElementTree as ET
from xml.dom import minidom

def obj_to_urdf(obj_path, urdf_path):
    obj_path = Path(obj_path)
    urdf_path = Path(urdf_path)
    assert urdf_path.parent == obj_path.parent

    geometry = ET.Element('geometry')
    mesh = ET.SubElement(geometry, 'mesh')
    mesh.set('filename', obj_path.name)
    mesh.set('scale', '1.0 1.0 1.0')

    material = ET.Element('material')
    material.set('name', 'mat_part0')
    color = ET.SubElement(material, 'color')
    color.set('rgba', '1.0 1.0 1.0 1.0')

    inertial = ET.Element('inertial')
    origin = ET.SubElement(inertial, 'origin')
    origin.set('rpy', '0 0 0')
    origin.set('xyz', '0.0 0.0 0.0')

    mass = ET.SubElement(inertial, 'mass')
    mass.set('value', '0.1')

    inertia = ET.SubElement(inertial, 'inertia')
    inertia.set('ixx', '1')
    inertia.set('ixy', '0')
    inertia.set('ixz', '0')
    inertia.set('iyy', '1')
    inertia.set('iyz', '0')
    inertia.set('izz', '1')

    robot = ET.Element('robot')
    robot.set('name', obj_path.with_suffix('').name)

    link = ET.SubElement(robot, 'link')
    link.set('name', 'base_link')

    visual = ET.SubElement(link, 'visual')
    visual.append(geometry)
    visual.append(material)

    collision = ET.SubElement(link, 'collision')
    collision.append(geometry)

    link.append(inertial)

    xmlstr = minidom.parseString(ET.tostring(robot)).toprettyxml(indent="   ")
    Path(urdf_path).write_text(xmlstr)  # Write xml file
    return


"""
Database models for monitoring researchers who have used the facility and their
outputs
"""
import os.path
from sqlalchemy import orm
from app import db, PKG_DIR
from app.constants import NIF_ASSOC
from app.exceptions import NifReportingException

class Researcher(db.Model):
    """
    A researcher who has used the facility
    """

    __tablename__ = 'researchers'

    id = db.Column(db.Integer, primary_key=True)
    given_name = db.Column(db.String(100))
    surname = db.Column(db.String(200))
    initials = db.Column(db.String(50))
    orcid = db.Column(db.String(200))
    title = db.Column(db.String(20))

    _scopus_authors = db.relationship('ScopusAuthor', backref='researcher')

    def __init__(self, given_name, surname, initials=None, scopus_authors=(),
                 orcid=None, title=None):
        self.given_name = given_name
        self.surname = surname
        self.initials = initials
        self.scopus_authors = list(scopus_authors)
        self.orcid = orcid
        self.title = title

    @property
    def name(self):
        name = '{} {}'.format(self.given_name, self.surname)
        if self.title is not None:
            name = '{} ' + name
        return name

    @property
    def scopus_ids(self):
        return [i.scopus_id for i in self.scopus_authors]

    @property
    def scopus_authors(self):
        return self._scopus_authors

    @scopus_authors.setter
    def scopus_authors(self, authors):
        for author in authors:
            if author.researcher is None:
                author.researcher = self
            elif author.researcher is not self:
                raise NifReportingException(
                    "Mismatching researcher in scopus authors {}: {}".format(
                        self, author.researcher))
        self._scopus_authors = authors

    def __str__(self):
        return self.name

class Publication(db.Model):

    __tablename__ = 'publications'

    CONTENT_DIR = os.path.join(PKG_DIR, 'publication-content')

    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date)
    scopus_id = db.Column(db.String(100), unique=True)
    doi = db.Column(db.String(200), unique=True)
    pii = db.Column(db.String(100), unique=True)
    title = db.Column(db.String(500))
    pubmed_id = db.Column(db.String(100), unique=True)
    volume = db.Column(db.String(100))
    pub_name = db.Column(db.String(200))
    openaccess = db.Column(db.Boolean)
    issue_id = db.Column(db.String(100))
    issn = db.Column(db.String(100))
    nif_assoc = db.Column(db.Integer)
    access_status = db.Column(db.Integer)
    abstract = orm.deferred(db.Column(db.Text))
    #content = orm.deferred(db.Column(db.Text))

    scopus_authors = db.relationship(
        'ScopusAuthor', secondary='scopusauthor_publication_assoc')

    def __init__(self, doi, title, scopus_id=None, pii=None, date=None,
                 pubmed_id=None, volume=None, pub_name=None, openaccess=None,
                 issue_id=None, issn=None, nif_funded=None, access_status=None,
                 nif_assoc=None, abstract=None, content=None):  #, author_ids=()):
        self.date = date
        self.doi = doi
        self.scopus_id = scopus_id
        self.pii = pii
        self.title = title
        self.pubmed_id = pubmed_id
        self.volume = volume
        self.pub_name = pub_name
        self.openaccess = openaccess
        self.issue_id = issue_id
        self.issn = issn
        self.nif_funded = nif_funded
        self.nif_assoc = nif_assoc
        self.abstract = abstract
        self.content = content
        self.access_status = access_status
        # self.author_ids = author_ids

    @property
    def content(self):
        if not self.has_content:
            return None
        with open(self.content_path) as f:
            content = f.read()
        return content

    @content.setter
    def content(self, content):
        if content is not None:
            with open(self.content_path, 'w') as f:
                f.write(str(content))

    @property
    def content_path(self):
        path = os.path.join(self.CONTENT_DIR, str(self.scopus_id))
        path += ('.txt' if self.pii else '.html')
        return path

    @property
    def has_content(self):
        return os.path.exists(self.content_path)

    @property
    def nif_assoc_str(self):
        try:
            return NIF_ASSOC[self.nif_assoc][0]
        except KeyError:
            return "Not determined"

    @property
    def researchers_involved(self):
        return (a.researcher for a in self.scopus_authors)

class Affiliation(db.Model):

    __tablename__ = 'affiliations'

    id = db.Column(db.Integer, primary_key=True)
    scopus_id = db.Column(db.Integer, unique=True)
    name = db.Column(db.String(500))
    city = db.Column(db.String(100))
    country = db.Column(db.String(100))

    scopus_authors = db.relationship('ScopusAuthor', backref='affiliation')

    def __init__(self, scopus_id, name, city, country):
        self.scopus_id = scopus_id
        self.name = name
        self.city = city
        self.country = country

class ScopusAuthor(db.Model):

    __tablename__ = 'scopusauthors'

    id = db.Column(db.Integer, primary_key=True)
    scopus_id = db.Column(db.Integer, unique=True)
    researcher_id = db.Column(db.Integer,
                              db.ForeignKey(
                                  'researchers.id',
                                  name='fk_scopusauthors_researchers'))
    affiliation_id = db.Column(db.Integer,
                               db.ForeignKey(
                                   'affiliations.id',
                                   name='fk_scopusauthors_affiliations'))
    givenname = db.Column(db.String(200))
    surname = db.Column(db.String(200))
    areas = db.Column(db.String(250))

    publications = db.relationship(
        'Publication', secondary='scopusauthor_publication_assoc')

    def __init__(self, scopus_id, researcher=None, affiliation=None,
                 areas=None, givenname=None, surname=None):
        self.scopus_id = scopus_id
        self.researcher = researcher
        # self.publication = publication
        self.affiliation = affiliation
        self.areas = areas
        self.givenname = givenname
        self.surname = surname

    @property
    def name(self):
        return '{} {}'.format(self.givenname, self.surname)

scopusauthor_publication_assoc = db.Table(
    'scopusauthor_publication_assoc', db.Model.metadata,
    db.Column('id', db.Integer, primary_key=True),
    db.Column(
        'scopusauthor_id', db.String(20), db.ForeignKey(
            'scopusauthors.id',
            name='fk_scopusauthorpublicationassoc_scopusauthor')),
    db.Column(
        'publication_id', db.Integer, db.ForeignKey(
            'publications.id',
            name='fk_scopusauthorpublicationassoc_publication')))


#!/usr/bin/env python
# coding=utf-

# 时间/价格/单价/面积/小区/区域/核验编码/房产公司/住宅/楼层/抵押/

class NingboHouse(object):
    def __init__(self, date, price, price_per, average, community, district, guid, agency_name, residence_type, floor, mortage_state,exclusive_or_not,agent):
        self.date = date.replace(',','/')
        self.price = price.replace(',','/')
        self.price_per = price_per.replace(',','/')
        self.average = average.replace(',','/')
        self.community = community.replace(',','/')
        self.district = district.replace(',','/')
        self.guid = guid.replace(',','/')
        self.agency_name = agency_name.replace(',','/')
        self.residence_type = residence_type.replace(',','/')
        self.floor = floor.replace(',','/')
        self.mortage_state = mortage_state.replace(',','/')
        self.exclusive_or_not = exclusive_or_not.replace(',','/')
        self.agent = agent.replace(',','/')

    def text(self):
        return self.date + "," + \
            self.price + "," + \
            self.price_per + "," + \
            self.average + "," + \
            self.community + "," + \
            self.district + "," + \
            self.guid + "," + \
            self.agency_name + "," + \
            self.residence_type + "," + \
            self.floor + "," + \
            self.mortage_state + "," + \
            self.exclusive_or_not+ "," + \
            self.agent


budget = float(input())
broi_vga = int(input())
broi_cpu = int(input())
broi_ram = int(input())

price_vga = 250 * broi_vga
price_cpu = (0.35 * price_vga) * broi_cpu
price_ram = (0.10 * price_vga) * broi_ram

sum = price_vga + price_cpu + price_ram

if broi_vga > broi_cpu:
    sum = sum - (sum * 0.15)

if budget >= sum:
    over = budget - sum
    print(f"You have {over:.2f} leva left!")
elif sum >= budget:
    nedeed = sum - budget
    print(f"Not enough money! You need {nedeed:.2f} leva more!")

Conditional Statements - Exercise/07. Shopping.py


"""
双链表有以下几个区别:
- 每一个node变为了two way node.
Node: data|pointer
TwoWayNode: pointer|data|pointer
- 可以指向previous node, traverse to the left;
- 多了一个tail node, 直接指向last node;
"""

class Node:
    def __init__(self,data =None,next = None):
        self.data = data
        self.next = next

class TwoWayNode(Node):
    def __init__(self,data,previous=None,next=None):
        Node.__init__(self,data,next)
        self.previous = previous

class DLinkedList:
    """
    Do one without dummy header node
    """
    def __init__(self):
        # Construct an empty doubly linked list
        self.head = None
        self.tail = None

        # self.head = TwoWayNode(None)
        # self.tail = TwoWayNode(None)
        # self.head.next = self.tail
        # self.tail.previous = self.head
    def insert_at_end(self,newItem):

        """
        Insert at the end of doubly linked list (DLL); 双链表的好处是，设计了
        一个tail pointer, 指向end of the DLL, 所以可以直接insert value, 时间复杂度
        为O(1)
        Args:
            newItem (_type_): data you wish to insert
        """
        # if the linked list is empty
        if self.head is None:
            node = TwoWayNode(newItem)
            self.head = node
            self.tail = self.head
        else:
            # 如果链表为非空
            # construct a new two way node, points to self.tail
            # points self.tail to the "new" tail now
            self.tail.next = TwoWayNode(newItem,self.tail)
            self.tail = self.tail.next

    def insert_at_start(self,newItem):

        if self.head is None:
            # if empty, it's the first node, |null|data|null|
            node = TwoWayNode(newItem)
            self.head = node
            self.tail = self.head
        else:
            # DLL is not empty, then
            # 1. 创建一个新node, next pointer 指向self.head, previous pointer指向null
            # 2. set previous pointer of the self.head to the newly created node
            # 3. point the self.head to the new node
            node = TwoWayNode(data = newItem,
                              previous = None,
                              next = self.head)
            self.head.previous = node
            self.head = node

    def remove_at_beginning(self):
        """
        remove node at the start of the linked list, O(1)
        Returns:
            _type_: data stored in the removed node (start node)
        """
        if self.head != None:
            removedItem = self.head.data
            self.head = self.head.next
            return removedItem

    def remove_at_end(self):
        """
        remove node at the end of the linked list, O(1)
        Returns:
            _type_: data stored in the removed node (end node)
        """
        if self.head != None:
            # 1.将last node的数据存在removedItem中
            # 2.将tail node指向second last node
            # 3.将tail node的next pointer指向null, 切断和removed node的connection, 交给garbage collector
            removedItem = self.tail.data
            self.tail = self.tail.previous
            self.tail.next = None
            return removedItem

    def print_forward(self):
        """
        print the graphical representation of the linked list

        None<->5<->9<->3<->2<->None
        """
        llstr = ""
        # 判断是否为null
        if self.head is None:
            llstr = "the linked list is empty"
        else:
            # 如果不为null
            llstr += "None" + "<->"
            probe = self.head
            while probe != None:
                llstr += str(probe.data) + "<->"
                probe = probe.next

            llstr+="None"

        print(llstr)

    def print_backward(self):
        """
        prin the linked list from tail, in reverse order
        """
        llstr = ""
        if self.head is None:
            print("the linked list is empty")
        else:
            # it's not empty
            llstr += "None" + "<->"
            probe = self.tail
            while probe != None:
                llstr += str(probe.data) + "<->"
                probe = probe.previous

            llstr += "None"
            print(llstr)

    def remove_at(self,index):
        """
        remove data at certain index, O(n) on average since needs to traverse the linked list
        Args:
            index (_type_): _description_

        优化思路: 实际上根据index更靠近start index or end index, 我们可以判定一下
        traverse from start or reverse, 这样的话就能改善average时间复杂度;
        """
        if index < 0 or index >= self.get_length():
            raise Exception("invalid index")
        elif index == 0:
            # remove from the beginning
            self.head = self.head.next
        else:

            # traverse from the top
            probe = self.head
            counter = 0

            while probe != None:
                if counter == index - 1:
                    break
                counter += 1
                probe = probe.next

            # 如果要removed index为i, 现在在i-1
            probe.next = probe.next.next
            probe.next.next.prev = probe

    def insert_at(self,index,newItem):
        # 看一下index是否valid
        if index < 0 or index >= self.get_length():
            raise Exception("index out of range")
        elif index == 0:
            self.insert_at_start(newItem)
        else:
            probe = self.head
            counter = 0
            while probe.next != None:
                if counter + 1 == index:
                    break

                counter += 1
                probe = probe.next

            # 现在在i-1, 想插入的i
            node = TwoWayNode(data = newItem,
                              previous=probe,
                              next = probe.next)
            probe.next.previous = node
            probe.next = node

    def insert_values(self,dataList):
        pass

    def get_length(self):
        """
        return length of the linked list
        Returns:
            _type_: _description_
        """
        # set a counter to count each node in linked list
        counter = 0
        # scan from the top
        probe = self.head
        while probe != None:
            counter += 1
            probe = probe.next

        return counter

if __name__ == "__main__":
    # test insert at start
    ll = DLinkedList()
    ll.insert_at_start(2)
    ll.insert_at_start(3)
    ll.insert_at_start(9)
    ll.insert_at_start(5)
    ll.print_forward()
    print("")

    # test remove at beginning
    ll.print_forward()
    print("removed item: " + str(ll.remove_at_beginning()))
    ll.print_forward()
    print("")

    # test remove at end
    ll.print_forward()
    print("removed item: " + str(ll.remove_at_end()))
    ll.print_forward()
    print("")
    # test remove at
    ll.insert_at_start(1)
    ll.insert_at_start(2)
    ll.insert_at_start(7)
    ll.insert_at_start(8)
    ll.print_forward()
    ll.remove_at(0)
    ll.print_forward()
    # test insert_at
    ll.print_forward()
    ll.insert_at(0,100)
    ll.print_forward()


# SW Expert Academy 0220_Queue - 11650

import sys
sys.stdin = open('input.txt', 'r')

for t in range(1, 11):
    T = int(input())
    arr = list(map(int, input().split()))
    result = 1
    while result:
        for i in range(1, 6):
            num = arr.pop(0)
            num -= i
            if num <= 0:
                result = 0
                arr.append(0)
                break
            arr.append(num)
    print(f'#{t}', *arr)


"""
Author: Peter Elliott       Created:    19/02/2020

            Task
Taking the following EDIFACT message text,
write some code to parse out the all the LOC segments
and populate an array with the 2nd and 3rd element of each segment.

Note:  the ‘+’ is an element delimiter
each + symbol seperates different words

        Notes to self
1. Read in EDIFACT file, question as edifact or text as above say EDIFACT message text
2. find all LOC segments and seperate them
3. Fill an array with each LOC entry 2nd and 3rd elements
4. Display
"""

file = open("Exercise1.txt", "r")  # task stated EDIFACT message text didn't mention file format so I defaulted it to text file
entered_data = file.readlines()  # open file and read each line

LOC_data = []  # array for LOC segments 2nd and 3rd element

for line in entered_data: # this cycles through each line of the file
    if "LOC" in line:  # if line contains the word LOC
        LOC = line.split("+")  # split the
        LOC_data.append({"Element 2": LOC[1], "Element 3": LOC[2].rstrip()})  # rstrip() removes \n at the end each string
        # above the 2nd and 3rd elements are placed in a dictionary and then added to a list

print(len(LOC_data), " items where found")  # number of tags found
if len(LOC_data) > 0:  # if tags were found display them
    for entry in LOC_data:  # print each element of  array
        print("2nd Element : ", entry["Element 2"], "    and    3rd Element :", entry["Element 3"])

input('Press enter to continue')


# Задание №1
# Напишите программу, которая добавляет ‘ing’ к словам (к каждому слову) в тексте
orig_list = """Etiam tincidunt neque erat, quis molestie enim imperdiet vel. Integer urna nisl,
facilisis vitae semper at, dignissim vitae libero""".split()
# и после этого выводит получившийся текст на экран.
# Знаки препинания не должны оказаться внутри слова.
# Если после слова идет запятая или точка, этот знак препинания должен идти после того же слова,
# но уже преобразованного.

updated_list = []
for word in orig_list:
    if word.endswith(',') or word.endswith('.'):
        updated_list.append(f"{word[:-1] + 'ing' + word[-1:]}")
    else:
        updated_list.append(word + "ing")

print(' '.join(updated_list))


# -*- coding: utf-8 -*-
"""
Analyse data from EP305capture23.py, calculate mean, median, mode and
standard deviation of the data and make a graph.
@author: Shane
"""
import numpy as np #needed for many math functions
import statistics as stats #used for stat functions
import matplotlib.pyplot as plt #needed for plotting

#-----------------------------------------------------------------------------#
def file_read(file_name):
    """Function takes in file_name of type string
       and returns arrays containing data"""

    #open specified file in read mode
    in_file = open(file_name, "r")

    #create data lists
    a = []
    b = []

    #save header to string and split into list
    header_string = in_file.readline()
    header = header_string.split()

    #save revelent data to respective lists
    for line in in_file:
        values = line.split()
        a.append(float(values[0]))
        b.append(float(values[1]))

    #close the file
    in_file.close()

    #return 3D lists of lists containing data
    ans = [a, b, header]

    return ans
#-----------------------------------------------------------------------------#
def getStats(array):
    """This function will return the mean, meadian, mode and standard
    deviation of an array of data."""

    #calculate mean, meadian and stdDev using numpy
    mean = np.mean(array)
    #calculate median using statistics library
    median = np.median(array)
    #calculate mode using lower_mode for redundency
    modes = lower_mode(array)
    #use numpy to get standard deviation
    stdDev = np.std(array)

    #return all stats calculated
    return mean, median, modes, stdDev

#-----------------------------------------------------------------------------#
def percentile(data):
    """Function to calculate the percentiles of a passed array"""

    #number of percentiles
    groups = np.empty(25)
    data.sort() #sort array
    max = 4 #initial max value
    index = 0 #initial index
    count = 0 #initial count
    for i in range (len(groups)):
        for j in range (len(data)):
            if (data[j] <= max and data[j] >= index):
                #if value in current percentile increase count
                count += 1
        max += 4 #increase roof by 4
        index += 4 #increase floor by 4
        groups[i] = count #add the number of values to percentile
        count = 0 #reset count

    #return the array of percentiles
    return groups
#-----------------------------------------------------------------------------#
def lower_mode(mode_array):
    """Function to catch no unique mode error, take lower mode and return this
    value insted, due to the number of values used this is common"""
    try:
        return stats.mode(mode_array)
    except stats.StatisticsError as e:
        return sorted(mode_array)[len(mode_array)//2 - 1]
#-----------------------------------------------------------------------------#
def printPercent(array, maxTime, minTime):
    """Function to print the percintiles, manx and min time. With the
    percintiles being an array of ints and max and min taking ints"""

    header1 = "Percentile"
    header2 = "Number of Sensor Readings"
    print("{0:<10}".format(header1) ,\
          "{0:>30}".format(header2))

    index = 0
    for i in range (25):
        percentRange = ""
        percentRange += str(index)
        percentRange += " - "
        percentRange += str(index+4)
        percentRange += "%"
        print("{0:<10}".format(percentRange) ,\
              "{0:>30.0f}".format(array[i]))
        index += 4

    header1 = "Time of max value in seconds"
    header2 = "Time of min value in seconds"
    print("\n{0:<10}".format(header1) ,\
          "{0:>30}".format(header2))
    print("{0:<6}".format(maxTime) ,\
          "{0:>30}".format(minTime))

#-----------------------------------------------------------------------------#
def plot(time, percent, binsNo, mean, median, mode, stdDev):
    """"Function will take array of data called y and number of bins
    to produce a histogram plot"""

    #make histogram plot using data from user
    plt.hist(percent, bins = binsNo)
    #title the graph
    plt.title("Percentile Histogram", fontsize = 20, color="b")
    #title the axis
    plt.xlabel("Percentile", fontsize = 10, color="r")
    plt.ylabel("Number sensor readings", fontsize = 10, color="r")

    textstr = '\n'.join((
    r'$\mu=%.2f$' % (mean, ),
    r'$\mathrm{median}=%.2f$' % (median, ),
    r'$\mathrm{mode}=%.2f$' % (mode, ),
    r'$\sigma=%.2f$' % (stdDev, )))

    # these are matplotlib.patch.Patch properties
    props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

    plt.text(110, 15, textstr, bbox=props)

    #finally print grpah
    plt.show()

    plt.plot(time ,percent)
    #title the graph
    title = "Plot of max potentiometer reading over time"
    plt.title(title, fontsize = 15, color="b")
    #title the axis
    plt.xlabel("Time is seconds", fontsize = 10, color="r")
    plt.ylabel("Percentage of potentiometer", fontsize = 10, color="r")
    #show the graph
    plt.show()

#-----------------------------------------------------------------------------#
def maxMinTime(time, data):
    """Function to calculate the occurance of max and min potentiometer
    reading, the result is returned in seconds as a tuple."""

    #obtain max and min values from array
    max_value = max(data)
    min_value = min(data)

    #find the index of max and min values
    max_index = data.index(max_value)
    min_index = data.index(min_value)

    #return the timestamp of max and min values
    return time[max_index], time[min_index]

#-------------------------------Main Method-----------------------------------#
#inform the user what is happening
print("This program will read EP305data23.txt, preform various calculations")
print("then a histogram of the data along with the statistics will be printed")

#read the data from file using file_read function
data = file_read("EP305data23.txt")

#obtain arrays returned from file_read
timeStamp = data[0]
POTpercent = data[1]
header = data[2]

#obtain the mean, median, mode and stdDev using getStats
mean, median, mode, stdDev = getStats(POTpercent)

#calculate the percintiles using percentile function
percentiles = percentile(POTpercent)

#plot the potentiometer readings and pass stats to be displayes on graph
plot(timeStamp, POTpercent, 25, mean, median, mode, stdDev)

#calculate occurance of max and min position using function
maxTime, minTime = maxMinTime(timeStamp, POTpercent)

#print the percentile, maxTime and minTime in formatted output
printPercent(percentiles, minTime, maxTime)


# -*- coding: utf-8 -*-
from datetime import timedelta
from odoo import models, fields, api, exceptions, _
#import pyperclip
#import copy

AVAILABLE_QUALITY = [
    ('0', 'Poor'),
    ('1', 'Normal'),
    ('2', 'Good'),
    ('3', 'Very Good'),
    ('4', 'Excellent')
]

class OvHrEmployeeExt(models.Model):
    _inherit = 'hr.employee'

    m_words = fields.Selection([('nil', 'Nil'),('basic', 'Basic'),('intermediate', 'Intermediate')], "Microsoft Words", store=True)
    m_excel = fields.Selection([('nil', 'Nil'),('basic', 'Basic'),('intermediate', 'Intermediate')], "Microsoft Excel", store=True)
    m_powerpoint = fields.Selection([('nil', 'Nil'),('basic', 'Basic'),('intermediate', 'Intermediate')], "Microsoft Power Point", store=True)
    m_outlook = fields.Selection([('nil', 'Nil'),('basic', 'Basic'),('intermediate', 'Intermediate')], "Microsoft Outlook", store=True)
    #For Stage
    applicant_image = fields.Binary('Image', attachment=True)
    enquiry = fields.Boolean("Contact", default=True)
    application = fields.Boolean("Application", default=False)
    iv = fields.Boolean("Interview", default=False)
    contract_proposal = fields.Boolean("Contract Proposal", default=False)
    convert_to_emp = fields.Boolean("Converted to Employee", default=False)
    blacklist = fields.Boolean("Blacklist", default=False)
    blacklist_note = fields.Text()
    #SubStage for Contact
    # contact_stage = fields.Many2one('contact.substage','Contact Stage',default=_default_contact_stage_id)
    # application_stage = fields.Many2one('application.substage','Application Stage',default=_default_application_stage_id)
    # interview_stage = fields.Many2one('interview.substage','Interview Stage',default=_default_interview_stage_id)
    school_ids = fields.One2many('edu.back', 'applicant_id', string="School")
    alevel_ids = fields.One2many('alevel.result', 'applicant_id', string="GCE 'A' Level Result")
    nolevel_ids = fields.One2many('nolevel.result', 'applicant_id', string="GCE 'N'/'O' Level Result")
    lang_ids = fields.One2many('language.spokenwritten', 'applicant_id', string="Language(s)")
    dialect_ids = fields.One2many('language.dialect', 'applicant_id', string="Dialect")
    careerhistory_ids = fields.One2many('career.history', 'applicant_id', string="Career History")
    days_avail_ids = fields.Many2many('days.avail','days_avail_hr_applicant_rel', 'hr_applicant_id', 'days_avail_id', string="Working Days Preference")
    #nationalservice_ids = fields.One2many('national.service', 'applicant_id', string="National Service")
    nationalservice = fields.Selection([
        ('saf', 'SAF'),
        ('spf', 'SPF'),
        ('scdf', 'SCDF'),
        ('nil', 'Not Served')
        ], "National Service", store=True)
    reason = fields.Text('Reason Not Served')
    rank = fields.Char('Rank')
    pes_grading = fields.Char('PES Grading')
    enlistment_date = fields.Date(string='Enlistment Date')
    operation_ready_date = fields.Date(string='Operation Ready Date')
    applicant_id = fields.Many2one('hr.applicant', string="Applicant", ondelete='cascade', index=True, copy=False, readonly=True)
    #other information

    #Details Information Needed
    appointment_reference = fields.Char("Our Ref.")
    description = fields.Text()
    letter_date = fields.Date(string='Date',default=lambda self : fields.Date.today())
    commencement_date = fields.Date(string='Commencement Date')
    partner_name = fields.Char("Name of Candidate")
    salutation = fields.Selection([('mr', 'Mr'),('ms', 'Ms'),('mrs', 'Mrs')], "Salutation", store=True)
    address = fields.Text('Address')
    address_status = fields.Selection([
        ('owned', 'Owned'),
        ('rented', 'Rented'),
        ('relative', '*Relative / Friend Place')
        ], "Address Type", store=True)
    country_id = fields.Many2one("res.country", "Nationality")
    nationality = fields.Selection([
        ('singaporean', 'Singaporean'),
        ('pr', 'Singapore PR'),
        ('malaysian', 'Malaysian'),
        ('others', 'Others')
        ], "Nationality", store=True)
    nationality_other = fields.Char('Others')
    country_of_birth = fields.Many2one("res.country", "Country of Birth")
    races = fields.Many2one("applicant.races", "Races")
    religion = fields.Char("Religion")
    postal_code = fields.Char("Postal Code",default='')
    country_image = fields.Binary(readonly=True, related='country_id.image')
    project_site = fields.Many2one('project.site', "Project Site Name")
    preferred_location1 = fields.Many2one('preferred.location', "Preferred Location")
    preferred_location2 = fields.Many2one('preferred.location', "Preferred Location 2")
    preferred_location3 = fields.Many2one('preferred.location', "Preferred Location 3")
    quality_of_contact = fields.Selection(AVAILABLE_QUALITY, "Quality of Contact", default='0')
    nric = fields.Char("NRIC No.")
    mobile_no1 = fields.Char("Mobile No.", size=32)
    mobile_no2 = fields.Char("Mobile No. 2", size=32)
    email = fields.Char("Email Address", size=128)
    # source_ids = fields.Many2one('job.source','Source')
    post_source = fields.Selection([
        ('newspaper_advertisement','Newspaper Advertisement'),
        ('word_of_mouth','Word of Mouth'),
        ('website','Website'),
        ('friends_relative','Website'),
        ('osp','One Stop Security Platform'),
        ('others','Others')
        ], "How do you learn of this post?",store=True)
    post_other = fields.Text('Other')
    wss_applied = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you ever applied for employment before with William Security Services / William Secure Solution?",store=True)
    wss_applied_other = fields.Text('Please state when and where')
    gender = fields.Selection([
        ('male','Male'),
        ('female','Female')
        ], "Gender", store=True)
    marital_status = fields.Selection([
        ('single', 'Single'),
        ('married', 'Married'),
        ('divorced', 'Divorced'),
        ('widowed', 'Widowed')
        ], "Marital Status", store=True)
    driving_license = fields.Selection([
        ('class3','Class 3'),
        ('class2','Class 2 / 2A / 2B'),
        ('no','Nil'),
        ('others','Others')
        ], "License", store=True)
    smoke = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Do you smoke?", store=True)
    friend_relative = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Do you have any friends / relatives working with William Security Services?", store=True)
    relative_ids = fields.One2many('friend.relative', 'applicant_id', string="Friends / Relatives")
    six_shift = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Are you able to work 6 shifts per week?", store=True)
    six_shift_no = fields.Char('Please state the number of shifts you can work per week')
    working_weekend = fields.Selection([
        ('yes','Yes'),
        ('no','No'),
        ('one_per_week','I can work at least 1 per week')
        ], "Are you able to work on weekends?", store=True)
    driving_license_other = fields.Char('Others')
    #Emergency Contact
    emergency_name = fields.Char('Name')
    emergency_relation = fields.Char('Relationship')
    emergency_mobile_no = fields.Char('Mobile')
    emergency_phone_no = fields.Char('Phone')
    emergency_address = fields.Text('Address')
    #Educational
    # school1 = fields.Char('School')
    parent_id = fields.Many2one('hr.employee', 'Manager')
    created_by = fields.Many2one('res.users','Created By', default=lambda self: self.env.user)
    owner = fields.Many2one('res.users','Owner', default=lambda self: self.env.user)
    recruiter = fields.Many2one('res.users','Recruiter', default=lambda self: self.env.user)
    job_id = fields.Many2one('hr.job', "Applied Job",required=True)
    day_of_source = fields.Selection([
        ('monday', 'Monday'),
        ('tuesday', 'Tuesday'),
        ('wednesday', 'Wednesday'),
        ('thursday', 'Thursday'),
        ('friday', 'Friday'),
        ('saturday', 'Saturday'),
        ('sunday', 'Sunday')
        ], "Day of Source", store=True)
    date_of_source = fields.Date(string='Date of Source',default=lambda self : fields.Date.today())
    date_of_birth = fields.Date(string='Date of Birth')
    age = fields.Integer('Age')
    age_range = fields.Selection([
        ('18-19','18-19'),
        ('20-29','20-29'),
        ('30-39','30-39'),
        ('40-49','40-49'),
        ('50-55','50-55'),
        ('56-60','56-60'),
        ('61-65','61-65'),
        ('66-69','66-69'),
        ('70-75','70-75'),
        ('others','Others'),
        ], "Age range", store=True)
    employment_type = fields.Selection([
        ('permanent','Permanent'),
        ('non_permanent','Non Permanent'),
        ('outsourced','Outsourced'),
        ('package','Package'),
        ('others','Others'),
        ],"Deployment Type Preferred", store=True)
    shift_preferences = fields.Selection([
        ('day','Day'),
        ('night','Night'),
        ('none','No Preference')
        ],"Shift Preference", store=True)
    preferred_shift = fields.Selection([
        ('day','Day'),
        ('night','Night'),
        ('none','No Preference')
        ],"Do you have a preferred shift", store=True)
    preferred_site = fields.Selection([
        ('condominium','Condominium'),
        ('factory','Factory'),
        ('light_industrial_bulding','Light Industrial Building'),
        ('office_buidling','Office Building'),
        ('heavy_industrial_building','Heavy Industrial Building'),
        ('shopping_centre','Shopping Centre'),
        ('showroom','Showroom'),
        ('school','School'),
        ('others','Others')
        ],"What type of work site do you prefer?", store=True)
    preferred_site_other = fields.Char('Others')
    #no_days_avail = fields.Integer("No. of Days Available (Full Week)",default='')
    #days_avail = fields.Many2many('days.available', string="Do you have specific days you can work?")
    # days_avail = fields.Selection([
    #     ('monday', 'Monday'),
    #     ('tuesday', 'Tuesday'),
    #     ('wednesday', 'Wednesday'),
    #     ('thursday', 'Thursday'),
    #     ('friday', 'Friday'),
    #     ('saturday', 'Saturday'),
    #     ('sunday', 'Sunday'),
    #     ('no_pref', 'I do not have specific days I can work')
    #     ], "Do you have specific days you can work?", store=True)
    no_weekends_avail = fields.Integer("No. of Weekends Available",default='')
    #preferred_site = fields.Many2many('preferred.site', string="Preferred Sites")
    remarks = fields.Text()
    previous_company = fields.Char("Where did you work before this? (Company)")
    previous_working_site = fields.Char("Where did you work before this? (Project Sites)")
    reason_leave = fields.Text()
    pay_like = fields.Char("What was the pay like?")
    remarks_on_prev_comp = fields.Text()
    shift_id = fields.Many2one('salary.package','Salary Code', domain="[('time', '=', shift)]")
    shift_time = fields.Char('Shift Time')
    bs = fields.Boolean('Basic Salary')
    ot = fields.Boolean('Overtime')
    mvc = fields.Boolean('Monthly Variable Component')
    pis = fields.Boolean('Productivity Incentive')
    awbr = fields.Boolean('Attendance & Bonus Reward')
    shift = fields.Selection([
       ('730am730pm','7.30 am – 7.30 pm'),
       ('800am800pm','8.00 am – 8.00 pm'),
       ('730pm730am','7.30 pm – 7.30 am'),
       ('800pm800am','8.00 pm – 8.00 am'),
       ], "Shift")
    contact_comment = fields.Text('Comment')
    interview_comment = fields.Text('Comment')
    link_url = fields.Char(string='Link')
    #declaration q and a
    illness = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you ever suffered, or are suffering from any medical condition, illness, disease, +mental illness or physical impairment? +Includes all forms of nervous breakdown and anxiety disorder", store=True)
    illness_yes = fields.Text('Details')
    criminal_record = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Do you have a criminal record in Singapore?", store=True)
    cr_yes = fields.Text('Details')
    charged_law = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you been charged with any offence in a court of law in Singapore or in any other country for which the outcome is pending?", store=True)
    cl_yes = fields.Text('Details')
    investigation_awareness = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Are you aware of being under any current police investigations in Singapore or in any other country following allegations made against you?", store=True)
    ia_yes = fields.Text('Details')
    financial_embarrassment = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you been or are you under any financial embarrassment i.e. (a) an undischarged bankrupt, (b) a judgement debtor, (c) have unsecured debts and liabilities of more than 3 months of last-drawn pay, (d) have signed a promissory note or an acknowledgement of indebtedness?", store=True)
    fe_yes = fields.Text('Details')
    broken_service_period = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you ever broken any bond or failed to fulfil any minimum service period?", store=True)
    bsp_yes = fields.Text('Details')
    currently_serving_bond = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Are you currently serving bond or any obligatory moral service with any organisation?", store=True)
    csb_yes = fields.Text('Details')
    dismissed_from_service = fields.Selection([
        ('yes','Yes'),
        ('no','No')
        ], "Have you ever been dismissed or discharged from the service of any employer?", store=True)
    dfs_yes = fields.Text('Details')
    #Declaration Signature and Date
    signature = fields.Binary(string="Signature", store=True)
    declaration_date = fields.Date(string='Date',default=lambda self : fields.Date.today())
    def _warning(self, title, message):
        return {
          'warning': {
            'title': title,
            'message': message,
          },
        }

    # @api.one
    # def copy_link(self):
    #     base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
    #     copied_url = base_url+'/web#id=%s&action=173&model=%s&view_type=form&menu_id=132' % (self.id, self._name)
    #     test = pyperclip.copy(copied_url)
    #     pyperclip.copy(copied_url)
    #     pyperclip.paste(copied_url)
    #     print (test)

    # @api.multi
    # @api.onchange('shift_id')
    # def onchange_shift_id(self):
    #     for shift in self.shift_id:
    #         if shift:
    #             self.shift_time = shift.time

    # @api.multi
    # @api.onchange('shift')
    # def onchange_shift(self):
    #     for t in self.shift:
    #         if t:
    #             self.shift_time = shift.time

    # @api.multi
    # def print_contract_document(self):
    #     for obj in self:
    #         if not obj.shift:
    #             raise exceptions.ValidationError("Please set Shift for this employee.")
    #         if not obj.shift_id:
    #             raise exceptions.ValidationError("Please set Salary Code for this employee.")
    #         elif ((obj.shift_time == '730am730pm')&(obj.shift_id.type_of_package=='daily')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_daily').report_action(self)
    #         elif ((obj.shift_time == '800am800pm')&(obj.shift_id.type_of_package=='daily')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_daily').report_action(self)
    #         elif ((obj.shift_time == '730pm730am')&(obj.shift_id.type_of_package=='daily')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_daily').report_action(self)
    #         elif ((obj.shift_time == '800pm800am')&(obj.shift_id.type_of_package=='daily')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_daily').report_action(self)
    #         elif ((obj.shift_time == '730am730pm')&(obj.shift_id.type_of_package=='monthly')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_monthly').report_action(self)
    #         elif ((obj.shift_time == '800am800pm')&(obj.shift_id.type_of_package=='monthly')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_monthly').report_action(self)
    #         elif ((obj.shift_time == '730pm730am')&(obj.shift_id.type_of_package=='monthly')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_monthly').report_action(self)
    #         elif ((obj.shift_time == '800pm800am')&(obj.shift_id.type_of_package=='monthly')):
    #             contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_monthly').report_action(self)
    #         else:
    #             #contract_report = self.env.ref('ov_hr_recruitment_ext.emp_contract_daily').report_action(self)
    #             return False
    #     return contract_report


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
xmind_name="kafka"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("Kafka高可用：分布式存储架构")
r2=s2.getRootTopic()
r2.setTitle("Kafka高可用：分布式存储架构")

content={
'分区':[
    '一个topic数据集合拆分为多个数据分区',
    '每个Partition可以在不同机器上，储存部分数据'
],
'副本冗余':[
    '每个Partition都可以放一个副本放在别的机器上',
    '某台机器宕机，只不过是Partition其中一个副本丢失',
    {'Partition有多副本':[
        '选举其中一个Parititon副本作为Leader，然后其他的Partition副本是Follower',
        '只Leader Partition对外提供读写操作，Follower Partition从Leader Partition同步数据',
        'Leader Partition宕机，选举其他的Follower Partition作为新的Leader Partition对外提供读写服务'
    ]}
],
'ISR机制':[
    'In-Sync Replicas:保持同步的副本',
    '每个Partition维护一个ISR列表，列表里有Leader，还有跟Leader保持同步的Follower',
    '如Follower因自身发生一些问题，不能及时从Leader同步数据，Follower被认为“out-of-sync”，会从ISR列表里踢出',
    {'多副本间数据如何同步':[
        'Leader副本接收到数据后',
        'Follower副本会不停的给他发送请求尝试去拉取最新的数据，拉取到自己本地后，写入磁盘',
    ]}
],
'ack参数':[
    '在KafkaProducer，即生产者客户端里设置',
    '0:KafkaProducer在客户端，只要把消息发送出去，不管数据是否落盘，直接认为消息发送成功',
    '1:(默认)Partition Leader收到消息且写入磁盘，不管其他Follower有无同步消息，认为消息发送成功',
    {'all(-1)':[
        'Partition Leader收到消息后，还要求ISR列表里跟Leader保持同步的Follower要把消息同步过去，才认为消息发送成功',
        {'配合min.insync.replicas=2使用':[
            '确保ISR中一定要有两个副本写入成功才能算消息重发成功',
            '不然ISR只有leader一个副本，acks就退化回1'
        ]}
    ]}
]

}

#构建xmind
xmind.build(content,r2)
#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")

12.Kafka高可用：分布式存储架构.py


# how to in data a argument

def fungsi(nama, tinggi, berat):
    print(f"{nama} punya tinggi {tinggi} dan berat {berat}")
fungsi("ucup", 170, 40)

def fungsi(data_list):
    data = data_list.copy()
    nama = data[0]
    tinggi = data[1]
    berat = data[2]
    print(f"{nama} punya tinggi {tinggi} dan berat {berat}")
fungsi(["yusril mahendri", 100, 200])

# WITH *args  adalah teknik mengambil dari banyak input
# def fungsi(*args):
#      nama = args[0]
#      tinggi = args[1]
#      berat = args[2]
#          print(f"{nama} punya tinggi {tinggi} dan berat {berat}")
# fungsi("dudung", 120, 120)

def tambah(*data):
    output = 0
    for angka in data:
        output += angka
    return output
hasil = tambah(1,2,3,4,5,6,7,8,9)
print(f"hasil = {hasil} ")

hasil = tambah(1,2,10)
print(f"hasil = {hasil} ")