#! /usr/bin/python

import os
import time
import smtplib
from datetime import datetime
from distutils import util
from MappingCommon import MappingCommon

#
# Main code begins here.
#
fqaqcIssueCount = 0
nqaqcIssueCount = 0

mapc = MappingCommon()

logFilePath = mapc.projectRoot + "/log"
k = open(logFilePath + "/createHit.log", "a+")

now = str(datetime.today())
k.write("\ncreateHit: Daemon starting up at %s\n" % now)
k.close()

# Execute loop based on polling interval
while True:
    hitPollingInterval = int(mapc.getConfiguration('Hit_PollingInterval'))
    hitStandAlone = mapc.getConfiguration('Hit_StandAlone')
    # Boolean config parameters are returned as string and need to be converted.
    hitStandAlone = bool(util.strtobool(hitStandAlone))
    Hit_AvailTarget = int(mapc.getConfiguration('Hit_AvailTarget'))
    hitReplacementThresholdF = float(mapc.getConfiguration('Hit_ReplacementThreshold_F'))
    hitReplacementThresholdN = float(mapc.getConfiguration('Hit_ReplacementThreshold_N'))
    hitAlertFrequency = int(mapc.getConfiguration('Hit_AlertFrequency')) * 60
    hitNeedFKmlsAlert = mapc.getConfiguration('Hit_NeedFKmlsAlert')
    # Boolean config parameters are returned as string and need to be converted.
    hitNeedFKmlsAlert = bool(util.strtobool(hitNeedFKmlsAlert))
    hitNeedNKmlsAlert = mapc.getConfiguration('Hit_NeedNKmlsAlert')
    # Boolean config parameters are returned as string and need to be converted.
    hitNeedNKmlsAlert = bool(util.strtobool(hitNeedNKmlsAlert))

    k = open(logFilePath + "/createHit.log", "a+")
    now = str(datetime.today())

    # Get serialization lock.
    mapc.getSerializationLock()

    # Get all Assignable HITs  and calculate our needs.
    numQaqcHits = 0
    numFqaqcHits = 0
    numNonQaqcHits = 0
    for hitId, row in mapc.getAssignableHitInfo().iteritems():
        # Calculate the number of assignable QAQC, FQAQC, and non-QAQC HITs
        # currently available. For HITs with multiple assignments, only count HITs
        # where the number of assignments created is less than the configured threshold.
        kmlType = row['kmlType']
        maxAssignments = row['maxAssignments']
        createdAssignments = maxAssignments - row['assignmentsRemaining']
        if kmlType == MappingCommon.KmlQAQC:
            numQaqcHits = numQaqcHits + 1
        elif kmlType == MappingCommon.KmlFQAQC:
            # Must have created less than the threshold number of assignments.
            threshold = max(int(round(hitReplacementThresholdF * maxAssignments)), 1)
            if createdAssignments < threshold:
                numFqaqcHits = numFqaqcHits + 1
        elif kmlType == MappingCommon.KmlNormal:
            # Must have created less than the threshold number of assignments.
            threshold = max(int(round(hitReplacementThresholdN * maxAssignments)), 1)
            if createdAssignments < threshold:
                numNonQaqcHits = numNonQaqcHits + 1

    # Create any needed QAQC HITs.
    kmlType = MappingCommon.KmlQAQC
    numReqdQaqcHits = max(Hit_AvailTarget - numQaqcHits, 0)
    if numReqdQaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s Q HITs, and needs to create %s HITs\n" %
            (numQaqcHits, numReqdQaqcHits))

    for i in xrange(numReqdQaqcHits):
        # Retrieve the last kml gid used to create a QAQC HIT.
        curQaqcGid = mapc.getSystemData('CurQaqcGid')

        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose gid is greater than the last kml chosen.
        # Exclude any kmls that are currently associated with an active HIT.
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, 1, curQaqcGid)
        # If we have no kmls left, loop back to the beginning of the table.
        if nextKml is None:
            curQaqcGid = 0
            (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, 1, curQaqcGid)
            # If we still have no kmls left, all kmls are in use as HITs.
            # Try again later.
            if nextKml is None:
                break
        # Save the last kml gid used to create a QAQC HIT.
        mapc.setSystemData('CurQaqcGid', gid)

        # Create the QAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts)
        k.write("createHit: Created HIT ID %s for Q KML %s\n" % (hitId, nextKml))

    # Create any needed FQAQC HITs.
    kmlType = MappingCommon.KmlFQAQC
    numReqdFqaqcHits = max(Hit_AvailTarget - numFqaqcHits, 0)
    if numReqdFqaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s F HITs, and needs to create %s HITs\n" %
            (numFqaqcHits, numReqdFqaqcHits))

    for i in xrange(numReqdFqaqcHits):
        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose mapped count by a trusted worker is less than
        # the number of mappings specified by Hit_MaxAssignmentsF.
        # Exclude any kmls that are currently associated with an active HIT.
        hitMaxAssignmentsF = int(mapc.getConfiguration('Hit_MaxAssignmentsF'))
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, hitMaxAssignmentsF)
        # If we have no kmls left, all kmls in the kml_data table have been
        # successfully processed. Notify Lyndon that more kmls are needed if we are in standalone mode.
        if nextKml is None:
            if (fqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                k.write("createHit: Alert: all F KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.\n")
                if hitStandAlone and hitNeedFKmlsAlert:
                    mapc.createAlertIssue("No F KMLs in kml_data table",
                            "Alert: all F KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.")
            fqaqcIssueCount += 1
            break
        else:
            if (fqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                fqaqcIssueCount = 0
            else:
                fqaqcIssueCount += 1
        remainingAssignments = hitMaxAssignmentsF - mappedCount

        # Create the FQAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts, maxAssignments=remainingAssignments)
        k.write("createHit: Created HIT ID %s with %d assignments for F KML %s\n" %
                (hitId, remainingAssignments, nextKml))

    # Create any needed N HITs.
    kmlType = MappingCommon.KmlNormal
    numReqdNonQaqcHits = max(Hit_AvailTarget - numNonQaqcHits, 0)
    if numReqdNonQaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s N HITs, and needs to create %s HITs\n" %
            (numNonQaqcHits, numReqdNonQaqcHits))

    for i in xrange(numReqdNonQaqcHits):
        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose mapped count by a trusted worker is less than
        # the number of mappings specified by Hit_MaxAssignmentsN.
        # Exclude any kmls that are currently associated with an active HIT.
        hitMaxAssignmentsN = int(mapc.getConfiguration('Hit_MaxAssignmentsN'))
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, hitMaxAssignmentsN)
        # If we have no kmls left, all kmls in the kml_data table have been
        # successfully processed. Notify Lyndon that more kmls are needed.
        if nextKml is None:
            if (nqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                k.write("createHit: Alert: all N KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.\n")
                if hitNeedNKmlsAlert:
                    mapc.createAlertIssue("No N KMLs in kml_data table",
                            "Alert: all N KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.")
            nqaqcIssueCount += 1
            break
        else:
            if (nqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                nqaqcIssueCount = 0
            else:
                nqaqcIssueCount += 1
        remainingAssignments = hitMaxAssignmentsN - mappedCount

        # Create the non-QAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts, maxAssignments=remainingAssignments)
        k.write("createHit: Created HIT ID %s with %d assignments for N KML %s\n" %
                (hitId, remainingAssignments, nextKml))

    # Release serialization lock.
    mapc.releaseSerializationLock()

    # Sleep for specified polling interval
    k.close()
    time.sleep(hitPollingInterval)


import datetime
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index(request):
    res=datetime.datetime.now()
    print('==================')
    print(res)
    # return HttpResponse(f' today is {res}')
    return render(request, 'newyear/index.html', {
        'date': res,
        'newyear': res.day==1 and res.month==1
    })


from aiogram.types import KeyboardButton, ReplyKeyboardMarkup

b1 = KeyboardButton("/Режим_работы")
b2 = KeyboardButton("/Расположение")
b3 = KeyboardButton("/Меню")
#b4 = KeyboardButton('Поделиться номером', request_contact=True)
#b5 = KeyboardButton('Отправить где я', request_location=True)

kb_client = ReplyKeyboardMarkup(resize_keyboard=True)

kb_client.add(b3).row(b2, b1)


lines = []

with open("F:\\programming\\github\\adventofcode\\2021\\python\\day03.txt", 'r') as f:
    lines = f.readlines()

gamma = ''
epsilon = ''

for c in range(len(lines[0].strip())):
    zeroCounter = 0
    for line in lines:
        if line[c] == '0':
            zeroCounter += 1

    if zeroCounter > 500:
        gamma += '0'
        epsilon += '1'
    else:
        gamma += '1'
        epsilon += '0'

print(int(gamma,2))
print(int(epsilon,2))

powerConsumption = int(gamma,2)*(int(epsilon,2))

print(powerConsumption)


from flask_restful import Resource
import requests
import os
import json
from flask import request
from helper import send_options, send_response

class Orders(Resource):
    def post(self):
        content_type = request.headers.get('Content-Type')
        if (content_type == 'application/json'):
            payload = request.get_json()
            response = save_order_details({"records":[{"fields":payload}]})
            if response.reason == 'OK':
                return send_response({}, 200)
            else:
                return send_response(response.text, 400)
        else:
            return send_response({"error": "invalid content type"}, 400)

    def options(self):
        return send_options("POST")

def save_order_details(payload):
    url = os.environ['AIRTABLE_URL']+'/orders'
    header = {"Authorization":"Bearer " + os.environ['AIRTABLE_KEY'], "Content-Type": "application/json"}

    return requests.post(url, data = json.dumps(payload), headers = header)

# sample airtable payload structure for dev reference
sample_payload = {
  "records": [
    {
      "fields": {
        "your-name": "claudia",
        "your-email": "claudia@sample.com",
        "phone-number": "9021111111",
        "pickup-location": "123 high st, Williamswood, Nova Scotia, Canada",
        "drop-location": "55 bently dr, Williamswood, Nova Scotia, Canada",
        "pickup-date": "2022-12-15",
        "item-description": "A wardrobe 73 inches high, 38 wide.\n2 regular sized doors, 3 closet doors, a few skinny windows, building materials 4 boxes of cedar shingles, 2 boxes...",
        "price": 115,
        "tos": 1,
        "record_date": "2022-12-16T02:46:00.000Z"
      }
    },
    {
      "fields": {
        "your-name": "prajwal",
        "your-email": "app@a.ca",
        "phone-number": "9029898888",
        "pickup-location": "8 Theakston Avenue, Halifax, Nova Scotia, Canada",
        "drop-location": "40 Theakston Avenue, Halifax, Nova Scotia, Canada",
        "pickup-date": "2021-12-10",
        "price": 85,
        "tos": 1,
        "record_date": "2021-12-12T03:21:00.000Z"
      }
    }
  ]
}


# Python program to check if year is a leap year or not

year = 2000

# # To get year (integer input) from the user
# # year = int(input("Enter a year: "))

# # divided by 100 means century year (ending with 00)
# # century year divided by 400 is leap year
# if (year % 400 == 0) and (year % 100 == 0):
#     print("{0} is a leap year".format(year))

# # not divided by 100 means not a century year
# # year divided by 4 is a leap year
# elif (year % 4 ==0) and (year % 100 != 0):
#     print("{0} is a leap year".format(year))

# # if not divided by both 400 (century year) and 4 (not century year)
# # year is not leap year
# else:
#     print("{0} is not a leap year".format(year))

# Creating a bubble sort function
def bubble_sort(list1):
    # Outer loop for traverse the entire list
    for i in range(0,len(list1)-1):
        for j in range(len(list1)-1):
            if(list1[j]>list1[j+1]):
                temp = list1[j]
                list1[j] = list1[j+1]
                list1[j+1] = temp
    return list1

list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
# Calling the bubble sort function
print("The sorted list is: ", bubble_sort(list1))


from typing import List
import collections
from pytest import mark

class Solution:
    def minWindow(s: str, t: str) -> str:
        if not s or not t:
            return ""
        t_dict = collections.Counter(t)
        l, r = 0, 0
        ans = float("inf"), l, r
        window_dict = {}
        required = len(t_dict)
        formed = 0
        while r < len(s):
            character = s[r]
            window_dict[character] = window_dict.get(character, 0) + 1
            if character in t_dict and t_dict[character] == window_dict[character]:
                formed += 1
            while l <= r and formed == required:
                character = s[l]
                if r - l + 1 < ans[0]:
                    ans = (r - l + 1, l, r)
                window_dict[character] -= 1
                if character in t_dict and window_dict[character] < t_dict[character]:
                    formed -= 1
                l += 1
            #increase l, finally clear window_dict = {}
            r += 1
        return "" if ans[0] == float("inf") else s[ans[1]:ans[2]+1]

    def minWindow_2(self, s: str, t: str) -> str:
        # Once tcount is established, we only care about lt, because there could
        # be duplicates in t, meaning some characters might be more than needed.
        # But the minWindow only achieved when all the frequencies are at most
        # 0, meaning there could be negatives. And once that criteria reached,
        # keep track when any character's frequency is above zero, that means
        # not all characters in t are included in s. No need to restablish tcount
        # again.
        ls, lt = len(s), len(t)
        tcount = collections.Counter(t)
        i, j = 0, 0
        minWindow = ls + 1
        while i < ls:
            if s[i] in tcount:
                if tcount[s[i]] > 0:
                    lt -= 1
                tcount[s[i]] -= 1
            while lt == 0:
                if i - j + 1 < minWindow:
                    minWindow = i - j + 1
                    result = s[j:i+1]
                if s[j] in tcount:
                    tcount[s[j]] += 1
                    if tcount[s[j]] > 0:
                        lt += 1
                j += 1
            i += 1
        return '' if minWindow == ls + 1 else result

@mark.parametrize('s, t, expected', [
        ("ADOBECODEBANC", "ABC", "BANC"),
        ("a", "a", "a"),
        ("a", "aa", ""),
    ])

def test_minWindow(s, t, expected):
    ans = Solution.minWindow(s, t)
    assert ans == expected


from pycocotools.coco import COCO
import skimage
import numpy as np
import psutil
import math
import subprocess

from matplotlib.path import Path

from random import shuffle

def pad(img):
  p00 = int((640 - img.shape[0]) / 2)
  p01 = 640 - img.shape[0] - p00
  p10 = int((640 - img.shape[1]) / 2)
  p11 = 640 - img.shape[1] - p10
  if len(img.shape) == 2:
    return np.pad(img, ((p00, p01), (p10, p11)), 'constant')
  else:
    assert len(img.shape) is 3
    return np.pad(img, ((p00, p01), (p10, p11), (0, 0)), 'constant')

PRINT_COUNT_INTERVAL = 50
PRINT_MEMORY_INTERVAL = 1000

PLACES_MAX_COUNT = 1000000

#https://stackoverflow.com/questions/50731785/create-random-shape-contour-using-matplotlib
def generate_random_shape(image_size):
  n = 8 # Number of possibly sharp edges
  r = .5 # magnitude of the perturbation from the unit circle,
  # should be between 0 and 1
  N = n*3+1 # number of points in the Path
  # There is the initial point and 3 points per cubic bezier curve. Thus, the curve will only pass though n points, which will be the sharp edges, the other 2 modify the shape of the bezier curve
  angles = np.linspace(0,2*np.pi,N)
  codes = np.full(N, Path.CURVE4)
  codes[0] = Path.MOVETO
  lengths = (2*r*np.random.random(N)+1-r) / (1 + r)
  # Alter lengths: [-1, 1] to [-X/2, X/2]
  min_fill_ratio = 0.1 / 2
  max_fill_ratio = 0.5 / 2
  diff = max_fill_ratio - min_fill_ratio
  scale_x = np.random.random() * diff + min_fill_ratio
  scale_y = np.random.random() * diff + min_fill_ratio
  move_x = scale_x + np.random.random() * (1 - 2 * scale_x)
  move_y = scale_y + np.random.random() * (1 - 2 * scale_y)
  #print(scale_x, scale_y, move_x, move_y, move_x + scale_x, move_y + scale_y)
  diff = max_fill_ratio - min_fill_ratio
  verts_x = move_x + np.cos(angles) * lengths * scale_x
  verts_y = move_y + np.sin(angles) * lengths * scale_y
  verts_x *= image_size
  verts_y *= image_size
  verts_x = np.uint16(verts_x)
  verts_y = np.uint16(verts_y)
  img = np.zeros((image_size, image_size), dtype=np.uint8)
  rr, cc = skimage.draw.polygon(verts_x, verts_y)
  img[rr, cc] = 1
  return img

def generate_random_shapes(image_size):
  img = generate_random_shape(image_size)
  for _ in range(4):
    img = img | generate_random_shape(image_size)
  return img

def run_cmd(cmd):
  return list(filter(lambda x: x is not "", subprocess.Popen(cmd,
      shell=True,
      stdout=subprocess.PIPE,
      universal_newlines=True).communicate()[0].split("\n")))

def read_set_places(data_type, image_size, count=None):
  data_dir='datasets/places365_standard/{}'.format(data_type)
  img_files = run_cmd("find {} -type f -size +1".format(data_dir))
  if not count:
    count = len(img_files)
  count = min(count, len(img_files), PLACES_MAX_COUNT)
  images = np.zeros([count, image_size, image_size, 4], dtype='float16')
  targets = np.zeros([count, image_size, image_size, 3], dtype='float16')
  shuffle(img_files)
  print("Count: ", data_type, len(img_files))
  for i in range(count):
    print("read_set_places ", i, img_files[i])
    input_img = skimage.io.imread(img_files[i])
    input_img = input_img / 256.
    if (len(input_img.shape) < 3):
      # Add one extra dim
      print(input_img.shape)
      input_img = np.array([input_img.transpose(), input_img.transpose(), input_img.transpose()]).transpose()
      print(input_img.shape)
    input_img = np.array(skimage.transform.resize(input_img, (image_size, image_size), anti_aliasing=True), dtype='float16')
    targets[i] = input_img
    # Sometimes don't add a mask.
    #if (np.random.random() < 0.05):
    #  mask = np.zeros([image_size, image_size, 1], dtype="uint8")
    #else:
    mask = generate_random_shapes(image_size).reshape(image_size, image_size, 1)
    input_img *= np.logical_not(mask)
    images[i] = np.append(input_img, mask, axis=2)
    i += 1

  print("images.nbytes: ", images.shape, images.nbytes, targets.shape, targets.nbytes)
  print("Memory info: ", psutil.virtual_memory())
  return images, targets

def read_set_coco(data_type, image_size, count=None):
  data_dir='datasets/coco'
  inputImgDir='{}/{}'.format(data_dir, data_type)
  annFile='{}/annotations/instances_{}.json'.format(data_dir, data_type)
  coco=COCO(annFile)
  catIds = coco.getCatIds(catNms=['person'])
  imgIds = coco.getImgIds(catIds=catIds)[0:count]
  if not count:
    # Add some images without a label present at all -- extra 10%, capped at 1000.
    EXTRA_IMAGES = min(1000, int(len(imgIds) / 10))
    set_diff = set(coco.getImgIds()) - set(imgIds)
    imgIds.extend(list(set_diff)[0:EXTRA_IMAGES])
  shuffle(imgIds)
  imgs = coco.loadImgs(imgIds)
  print("Count: ", data_type, len(imgs))
  num_train = 0
  images = np.zeros([len(imgs), image_size, image_size, 3], dtype='uint8')
  targets = np.zeros([len(imgs), image_size, image_size], dtype='uint8')
  print("Memory info: ", psutil.virtual_memory())
  i = 0
  for img in imgs:
    input_img = np.array(skimage.io.imread('{}/{}'.format(inputImgDir, img['file_name'])))
    if (len(input_img.shape) < 3):
      # Add one extra dim
      print(input_img.shape)
      input_img = np.array([input_img.transpose(), input_img.transpose(), input_img.transpose()]).transpose()
      print(input_img.shape)
    anns = coco.loadAnns(coco.getAnnIds(imgIds = img['id'], catIds = catIds, iscrowd = None))
    mask = np.zeros(input_img.shape[0:2], int)
    for ann in anns:
      mask = mask | coco.annToMask(ann)
    input_img = input_img * 1.
    mask = mask * 1.
    input_img = pad(input_img)
    target_img = pad(mask)
    input_img = np.array(skimage.transform.resize(input_img, (image_size, image_size), anti_aliasing=True), dtype='uint8')
    target_img = np.array(skimage.transform.resize(target_img, (image_size, image_size), anti_aliasing=False), dtype='uint8')
    images[i] = input_img
    targets[i] = target_img
    i += 1
    if i % PRINT_COUNT_INTERVAL == 0:
      print(i)
    if i % PRINT_MEMORY_INTERVAL == 0:
      print("Memory info: ", psutil.virtual_memory())
  print("images.nbytes: ", images.shape, images.nbytes)
  print("targets.nbytes: ", targets.shape, targets.nbytes)
  print("Memory info: ", psutil.virtual_memory())
  return images, targets

def get_name(sz, l, s):
  if l:
    return "npydata/{}-{}-{}.npy".format(s, sz, l)
  else:
    return "npydata/{}-{}.npy".format(s, sz)

def read_dataset_places(image_size, train_count, val_count):
  train_images, train_targets = read_set_places('train', image_size, train_count)
  val_images, val_targets = read_set_places('val', image_size, val_count)
  return train_images, train_targets, val_images, val_targets

def read_dataset_coco(image_size, train_count, val_count):
  train_images, train_targets = read_set_coco('train2017', image_size, train_count)
  val_images, val_targets = read_set_coco('val2017', image_size, val_count)
  return train_images, train_targets, val_images, val_targets

def load_dataset(image_size, train_count, val_count):
  train_images  = np.load(get_name(image_size, train_count, "train_images"))
  train_targets = np.load(get_name(image_size, train_count, "train_targets"))
  val_images    = np.load(get_name(image_size, val_count  , "val_images"))
  val_targets   = np.load(get_name(image_size, val_count  , "val_targets"))
  return train_images, train_targets, val_images, val_targets

def save_dataset(image_size, train_images, train_targets, val_images, val_targets):
  np.save(get_name(image_size, len(train_images ), "train_images"), train_images)
  np.save(get_name(image_size, len(train_targets), "train_targets"), train_targets)
  np.save(get_name(image_size, len(val_images   ), "val_images"   ), val_images)
  np.save(get_name(image_size, len(val_targets  ), "val_targets"  ), val_targets)

#train_images, train_targets, val_images, val_targets = read_dataset_coco(224, 1024, 32)
#save_dataset(224, train_images, train_targets, val_images, val_targets)
#load_dataset(224, 4, 1)


import unittest

from featurebox.pbs.job_manager import JobManager

class MyTestCase(unittest.TestCase):
    def setUp(self) -> None:
        if not hasattr(self, "jm"):
            self.jm = JobManager()
            self.path = ["/home/wcx/data/code_test/Ta2CO2/Zn/pure",
                         "/home/wcx/data/code_test/V2CO2/Zn",
                         "/home/wcx/data/code_test/V2CO2/nodoping",
                         "/home/wcx/data/code_test/Zr2CO2/Zn/pure_static"]

    def test_a_submit_from_path(self):
        jobids = self.jm.submit_from_path(path=self.path, file="p*.run")
        self.jm.dump_job_msg()
        self.jm.job_dir()
        self.assertGreater(len(jobids), 0)
        self.assertGreater(len(self.jm.msg), 0)
        assert len(self.jm.job_id()) == len(self.jm.job_dir())

    def test_b_delete_and_re_submit(self):
        self.jm.get_job_msg()
        k = [i for i in self.jm.msg.keys()]
        l = len(self.jm.msg)
        jobids = self.jm.delete(k[0])
        jjobss = self.jm.re_submit_from_path(old_id=k[0], file="p*.run")
        self.assertEqual(len(self.jm.msg), l)
        self.assertGreater(len(self.jm.deleted_msg), 0)
        self.assertNotEqual(jobids, jjobss)
        print(jobids, jjobss)

    def test_c_delete_and_re_submit2(self):
        self.jm.get_job_msg()
        k = [i for i in self.jm.msg.keys()]
        l = len(self.jm.msg)
        # jobids = self.jm.delete(k[:2])
        jobids = self.jm.delete("-".join([k[-3], k[-1]]))
        jjobss = self.jm.re_submit_from_path(old_id=k[:3], file="p*.run")
        print(len(self.jm.msg), l)
        self.assertGreater(len(self.jm.deleted_msg), 0)
        self.assertNotEqual(jobids, jjobss)
        print(jobids, jjobss)

    def test_d_hold_release(self):
        self.jm.get_job_msg()
        k = [i for i in self.jm.msg.keys()]
        jobids1 = self.jm.hold(k[-2:])
        jobids2 = self.jm.release(jobids1)
        print(jobids1, jobids2)
        self.assertEqual(jobids1, jobids2)

    def test_g_clear(self):
        self.jm.get_job_msg()
        jobids = self.jm.clear()
        assert len(jobids) > 2

    def test_h_log(self):
        self.jm.get_job_msg()
        print(self.jm.sparse())

    def test_i_load(self):
        self.jm.load_job_msg()
        print(self.jm.sparse())

    def test_j_id(self):
        print(self.jm.job_id())

    def test_k_dir(self):
        print(self.jm.job_dir())

if __name__ == '__main__':
    unittest.main()


'''
5. Escreva um programa que pergunte a idade de 6 pessoas, calcule e mostre:
a) a quantidade de pessoas menores de idade (idade < 18 anos)
b) média de idade destas pessoas
c) a idade da pessoa mais velha
d) percentual de pessoas com mais de 20 anos
'''

class Idades():

    def __init__(self,vetor1,vetor2,vetor3):
        self.vetor1 = vetor1
        self.vetor2 = vetor2
        self.vetor3 = vetor3

    def setVetor1(self, vetor1):
        self.vetor1 = vetor1

    def setVetor2(self, vetor2):
        self.vetor2 = vetor2

    def setVetor3(self, vetor3):
        self.vetor3 = vetor3

    def getCalculo(self):
        self.vetor1 = []
        self.vetor2 = []
        self.vetor3 = []
        for n in range(0,6,1):
            print ('Digite a sua idade:')
            idade = int(input())
            vetor1.append(idade)
            if idade < 18:
                vetor2.append(idade)
            elif idade > 20:
                vetor3.append(idade)

        media = sum(vetor1)/6
        maximo = max(vetor1)
        permaiorId = (len(vetor3) * 100) / len(vetor1)

        var1 = str(print('Quantidade de pessoas menores de idade: ', len(vetor2)))
        var2 = str(print('A média de idade é: ', media))
        var3 = str(print('A idade da pessoa mais velha é: ', maximo))
        var4 = str(print('Percentual de pessoas com mais de 20 anos: ', permaiorId))

        return var1, var2, var3, var4

vetor1 = []
vetor2 = []
vetor3 = []
idades = Idades(vetor1,vetor2,vetor3)

resposta = idades.getCalculo()


import unittest
from untils.HttpClient import HttpClientRequest
from untils.readYaml import baseConfig
from untils.op_mysql import execute
from log.log import logger
from ddt import ddt,data,file_data
from untils.op_excel import Excel_rw
from untils.tool import *
apiPre=baseConfig.api['test']
#apicase_path=baseConfig.APICASE_YAML_PATH
apicase_path=baseConfig.APICASE_EXCEL_PATH
@ddt
class TestCaseAPi(unittest.TestCase):
    '''
    作业：驱动excel表格用例，所有的sheet表格，所有的用例依次执行，而且打印接口执行个数
    '''
    index=0
    '''几个接口的用例'''
    #Cases_Info=Excel_rw(filename=apicase_path).read_api_cases() #读取excel所有的用例数据
    Cases_Info=get_api_excel_case()
    @classmethod
    def setUpClass(self) -> None:
        '''所有用例的前置'''
        self.request=HttpClientRequest()

    def tearDown(self) -> None:
        '''接口请求后，初始化headers'''
        self.request.init_headers()

    # @file_data(apicase_path)
    # def test01(self,**caseinfo):
    #     '''
    #     登录
    #     :return:
    #     '''
    #     TestCaseAPi.index+=1
    #     casename,method,apiname,data,extract,duanyan,headers=caseinfo['casename'],caseinfo['method'],\
    #                                                  caseinfo['apiname'],caseinfo['data'],\
    #                                                  caseinfo['extract'],caseinfo['duanyan'],caseinfo['headers']
    #     res=self.request.sendRequest(method=method,
    #                                  name=apiname,
    #                                  data=data,
    #                                  tiqu=extract,
    #                                  headers=headers,
    #                                  index=TestCaseAPi.index)#3.发请求过程
    #     #self.request.validate(yuqi=duanyan, shiji=res)
    #     result=self.request.validate(yuqi=duanyan,shiji=res) #返回元组（1布尔值，2验证信息）
    #     logger.info(result[1])
    #     self.assertTrue(result[0])#4.结果验证
    #     '''拿到响应值,写入到excel，输入到报告'''

    @data(*Cases_Info)
    def test01(self, Case_Info):
        '''
        登录
        :return:
        '''
        print(Case_Info)
        TestCaseAPi.index += 1
        casename, method, apiname, data, extract, duanyan, headers ,host= Case_Info['接口用例名称'], Case_Info['请求方式'], \
                                                                     Case_Info['接口url'], Case_Info['请求入参'], \
                                                                     Case_Info['提取变量'], Case_Info['检查点'], \
                                                                     Case_Info['请求头信息'],Case_Info['域名'],
        #excel里面字典类型数据转化
        data=json_loads(data)
        headers=json_loads(headers)
        duanyan=json_loads(duanyan)
        extract=json_loads(extract)
        url=host+apiname
        res = self.request.sendRequest(method=method,
                                       url=url,
                                       data=data,
                                       tiqu=extract,
                                       headers=headers,
                                       index=TestCaseAPi.index)  # 3.发请求过程
        result = self.request.validate(yuqi=duanyan, shiji=res)  # 返回元组（1布尔值，2验证信息）
        logger.info(result[1])
        self.assertTrue(result[0])  # 4.结果验证
        '''拿到响应值,写入到excel，输入到报告'''

if __name__ == '__main__':
    unittest.main()


#! /usr/bin/env python

# Way2SMS Desktop-App
# Author: Akash Shende
# Contact: akash321@gmail.com

from bs4 import BeautifulSoup as bs
from way2sms import __contact_list__

class Contacts():

	def __init__(self,html_data,c_list):
		#creating soup
		Contacts.soup=bs(''.join(html_data))

		Contacts.c_list=c_list

	def extract_name(self):
		names=Contacts.soup.findAll(attrs={"name":"Quckvalue"})
		#print names
		names=str(names[0]['value'])
		name_list=names.split("*")

		#for n in name_list:
		#	print n,"\n"

		return name_list

	def extract_number(self):
		numbers=Contacts.soup.findAll(attrs={"name":"Qucktitle"})
		numbers=str(numbers[0]['value'])
		number_list=numbers.split(',')

		#for n in number_list:
		#	print n,"\n"

		return number_list

	def build_list(self):

		list1=self.extract_name()
		list2=self.extract_number()

		for i in xrange(1,len(list1)):
			Contacts.c_list.append([str(list1[i]),str(list2[i])])


class Person:
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender

class Nodo:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

    def __repr__(self):
        return f'{self.data}'

class BinaryTree:
    def __init__(self, head):
        self.root = Nodo(head)
        self.child = []

    def insert(self, data, head):
        subTree = self.search(head)
        self.__insert_aux(data, subTree)

    def __insert_aux(self, data, actual):
        if data.gender == "F":
            if actual.left == None:
               actual.left = Nodo(data)
            else:
                self.__insert_aux(data,actual.left)
        elif data.gender == "M":
            if actual.right == None:
                actual.right = Nodo(data)
            else:
                self.__insert_aux(data,actual.right)

    def preOrden(self, root):
        if root is not None:
            print(root.data.name)
            self.preOrden(root.left)
            self.preOrden(root.right)

    def search(self, person):
        return self.__search_aux(person, self.root)

    def __search_aux(self, person, actual):
        if actual.data == None:
           return person.name + " doesnt found in the binary tree"
        if actual.data.name == person.name:
            return actual
        if  person.gender == "F":
            return self.__search_aux(person,actual.left)
        return self.__search_aux(person,actual.right)

    ##The fuction insert and search were taken from: https://github.com/imontoyah/ST0245-002/blob/master/talleres/taller10/BinaryTree.py

    def findGrandMa(self, person):
        foundPerson = self.search(person)
        if(foundPerson.left != None):
            if(foundPerson.left.left == None):
                return person.name + " doesn't have grandma"
            else:
                return person.name + "´s grandma is: " + foundPerson.left.left.data.name
        else:
            return person.name + " has not mother"

#M = Male and F = Female
class main:
    p1 = Person("Wilkenson", "M")
    p2 = Person("Joaquina", "F")
    p3 = Person("Sufranio", "M")
    p4 = Person("Eustaquia", "F")
    p5 = Person("Eustaquio", "M")
    p6 = Person("Florinda", "F")
    p7 = Person("Jovin", "M")
    p8 = Person("Piolina", "F")
    p9 = Person("Piolin", "M")
    p10 = Person("Wilberta", "F")
    p11 = Person("Usnavy", "M")

    b = BinaryTree(p1)
    b.insert(p2, p1 )
    b.insert(p3,p1)
    b.insert(p4, p2)
    b.insert(p5, p2)
    b.insert(p6, p4)
    #b.insert(p7, p5)
    b.insert(p8, p3)
    b.insert(p9, p3)
    #b.insert(p10, p8)
    b.insert(p11,p9)

    b.preOrden(b.root)
    print(b.findGrandMa(p1))
    print(b.findGrandMa(p2))
    print(b.findGrandMa(p3))
    print(b.findGrandMa(p4))
    #print(b.findGrandMa(p5))


# -*- mode: python; coding: utf-8; -*-

__author__ = "Kenny Meyer"
__email__ = "knny.myer@gmail.com"

from django.forms import ModelForm, Textarea
from django import forms
from django.core.exceptions import ValidationError

from models import FlashCard

RATINGS = ((0, 'Blackout'),
           (1, 'Barely Know it'),
           (2, 'Needs Work'),
           (3, 'Remembered'),
           (4, 'Solid'))

class RatingForm(forms.Form):
    id = forms.IntegerField(widget=forms.HiddenInput)
    rating = forms.ChoiceField(choices=RATINGS)

SORTING = ((0, 'By alphabet'),
           (1, 'By next practice'))

class SortFlashcardsForm(forms.Form):
    sort_by = forms.ChoiceField(choices=SORTING)

class FlashCardForm(ModelForm):
    class Meta():
        model = FlashCard
        fields = ['front', 'back']
        exclude = ('user')
        widgets = {
            'front': Textarea(attrs={'cols': 70, 'rows': 10}),
            'back': Textarea(attrs={'cols': 70, 'rows': 10}),
        }

# vim: ai ts=4 sts=4 et sw=4


import random
from fruitlist import fruits

def play_hangman():
    word_list = fruits
    secret_word = random.choice(word_list).lower()
    guessed_letters = []
    attempts = 6

    print("Vamos jogar Forca")
    print("A palavra contém", len(secret_word), "letras.")

    play_round(secret_word, guessed_letters, attempts)

def play_round(secret_word, guessed_letters, attempts):
    if attempts == 0:
        print("Game Over! Acabaram suas tentativas!!!")
        print("A palavra secreta era:", secret_word)
        return

    display_word = ""
    for letter in secret_word:
        if letter in guessed_letters:
            display_word += letter + " "
        else:
            display_word += "_ "

    print(display_word)

    if display_word.replace(" ", "") == secret_word:
        print("Parabéns! Você adivinhou corretamente.")
        return

    guess = input("Digite uma letra: ").lower()

    if len(guess) != 1 or not guess.isalpha():
        print("Entrada inválida. Por favor digite apenas uma letra.")
        play_round(secret_word, guessed_letters, attempts)
        return

    if guess in guessed_letters:
        print("Você já tentou essa letra. Tente novamente.")
        play_round(secret_word, guessed_letters, attempts)
        return

    guessed_letters.append(guess)

    if guess not in secret_word:
        attempts -= 1
        if attempts == 0:
            print('É o fim...')
        else:
            print("Você errou. Ainda restam", attempts, "tentativas...")

    play_round(secret_word, guessed_letters, attempts)

play_hangman()


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

# preprocess.py
# --------------
# Attribution Information: The recommendation system projects were developed at JD.com.
# The core projects and autograders were primarily created by Xincheng Xiong
# (xincheng.xiong@gmail.com) and ...

import pandas as pd
import numpy as np
import pickle
import h5py

full_colnames = ['brandname',
                 'item_first_cate_cd',
                 'item_first_cate_name',
                 'item_second_cate_cd',
                 'item_second_cate_name',
                 'item_third_cate_cd',
                 'item_third_cate_name',
                 'sale_ord_det_id',
                 'sale_ord_id',
                 'parent_sale_ord_id',
                 'sale_ord_type_cd',
                 'sale_ord_cate_cd',
                 'user_id',
                 'sale_ord_tm',
                 'sale_ord_dt',
                 'sale_ord_det_tm',
                 'out_wh_tm',
                 'ord_complete_tm',
                 'dt',
                 'item_sku_id',
                 'item_id',
                 'item_name']

use_colnames = ['brandname',
                'item_first_cate_cd',
                'item_first_cate_name',
                'item_second_cate_cd',
                'item_second_cate_name',
                'item_third_cate_cd',
                'item_third_cate_name',
                'user_id'
                ]
# # save partial data.

data = pd.read_csv('whole.csv', usecols=use_colnames, error_bad_lines=False, low_memory=False)
data.to_csv('part_data.csv', encoding='utf-8')

# read partial data.
data = pd.read_csv('part_data.csv', encoding='utf-8', low_memory=False)

# # save third category id to name dictionary

third_cate_name = data[['item_third_cate_cd', 'item_third_cate_name']].drop_duplicates(subset='item_third_cate_cd', keep='first').sort_values(by='item_third_cate_cd')
third_cate_name.to_csv('third_cate_id_to_name.csv', index=False, encoding='utf-8')
dic_id_to_name = dict(zip(third_cate_name.item_third_cate_cd, third_cate_name.item_third_cate_name))
pickle.dump(dic_id_to_name, open("third_cate_id_to_name.p", "wb"))

item_third_list = pd.Series.unique(data['item_third_cate_cd']).tolist()
item_third_list = sorted(item_third_list)

# move 狗粮 to the front
item_third_list.remove(7002)
item_third_list.remove(7004)
item_third_list.remove(7007)
item_third_list.remove(7016)
item_third_list.insert(0, 7002)

# # move 巧克力 to the front
# item_third_list.remove(1594)
# item_third_list.remove(5021)
# item_third_list.insert(0, 1594)

# shuffle the rows
user_id_list = pd.Series.unique(data['user_id']).tolist()
np.random.shuffle(user_id_list)

# save id_index_matching dictionary.
dic_item_to_index = {k: v for (v, k) in enumerate(item_third_list)}
dic_usr_to_index = {k: v for (v, k) in enumerate(user_id_list)}

# construct userID to itemID matrix
matrix = np.zeros((len(user_id_list), len(item_third_list)), dtype=np.uint8)

# fill the matrix.
ratings = [] # for collaborative filtering
for _, pair in data[['user_id','item_third_cate_cd']].iterrows():
    if pair[1] in [7004, 7007, 7016]:
    # if pair[1] in [5021]:
        if matrix[dic_usr_to_index[pair[0]], 0] == 0:
            matrix[dic_usr_to_index[pair[0]], 0] = 1
            ratings.append((dic_usr_to_index[pair[0]], 0, 1))
    else:
        if matrix[dic_usr_to_index[pair[0]], dic_item_to_index[pair[1]]] == 0:
            matrix[dic_usr_to_index[pair[0]], dic_item_to_index[pair[1]]] = 1
            ratings.append((dic_usr_to_index[pair[0]], dic_item_to_index[pair[1]], 1))

# delete label index, update item index dictionary
del item_third_list[0]
dic_item_to_index = {k: v for (v, k) in enumerate(item_third_list)}

# save the matrix and index dictionary.
# with h5py.File('data.h5', 'w') as hf:
#     hf.create_dataset('matrix', data=matrix)

pickle.dump(dic_item_to_index, open("data/dic_item_to_index.p", "wb"))
pickle.dump(dic_usr_to_index, open("data/dic_usr_to_index.p", "wb"))
np.save('data/matrix.npy', matrix)
np.save('data/ratings.npy',np.array(ratings))


__all__ = ['EstimatorConfig',
           'MultiClassXGBConfig',
           'BinClassXGBConfig',
           'RandomForestConfig',
           'ExtraRandomForestConfig',
           'GBDTConfig',
           'XGBRegressorConfig',
           'LGBMRegressorConfig',
           'Basic4x2']

from ..utils.log_utils import get_logger

class EstimatorConfig(object):
    """
    Estimator Argument is a class describes an estimator and its specific arguments, which is used to create concrete
    estimators in training.
    Every time we create a multi-grain scan layer or a cascade layer, we don't put concrete estimator instances into
    initialization parameters, but put the EstimatorConfig that describes the estimator.
    """
    def __init__(self, est_args=None):
        self.LOGGER = get_logger('estimator.estimator_configs')
        if est_args is None:
            self.est_args = {}
        else:
            self.est_args = est_args.copy()

    def get_est_args(self):
        """
        Get estimator argument.

        :return: estimator argument
        """
        return self.est_args.copy()

    def __str__(self):
        return "{}".format(self.est_args['est_type'])

    def __copy__(self):
        return EstimatorConfig(self.est_args)

class MultiClassXGBConfig(EstimatorConfig):
    """
    Multi-class XGBoost Classifier Argument.
    """
    def __init__(self, n_folds=3, nthread=-1, scale_pos_weight=1, num_class=None, silent=True,
                 objective="multi:softprob", subsample=0.9, n_estimators=60,
                 colsample_bytree=0.85, colsample_bylevel=1, max_depth=4, learning_rate=0.1):
        """
        Multi-class XGBoost Classifier Argument describes arguments of multi-class xgboost classifier.
        Parameters can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param num_class: number of classes to classify
        :param silent:
        :param objective: objective, default is 'multi:softprob'
        :param subsample:
        :param n_estimators:
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth:
        :param learning_rate:
        """
        super(MultiClassXGBConfig, self).__init__()
        assert num_class is not None, 'You must set number of classes!'
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'subsample': subsample,
            'n_estimators': n_estimators,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate
        }

class BinClassXGBConfig(EstimatorConfig):
    """
    Binary Class XGBoost Classifier.
    """
    def __init__(self, n_folds=3, nthread=-1, scale_pos_weight=1, num_class=2, silent=True,
                 objective="binary:logistic", subsample=1.0, num_boost_round=100,
                 colsample_bytree=1.0, colsample_bylevel=1.0, max_depth=6, learning_rate=0.1):
        """
        Binary-class XGBoost Classifier Argument describes arguments of multi-class xgboost classifier.
        Parameter can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param num_class: number of classes to classify
        :param silent:
        :param objective: objective, default is 'multi:softprob'
        :param subsample:
        :param num_boost_round:
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth:
        :param learning_rate:
        """
        super(BinClassXGBConfig, self).__init__()
        assert num_class is not None, 'You must set number of classes!'
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'subsample': subsample,
            'n_estimators': num_boost_round,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate
        }

class RandomForestConfig(EstimatorConfig):
    """
    Random Forest Argument.
    """
    def __init__(self, n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, max_features='sqrt',
                 criterion="gini", min_samples_split=2, min_impurity_decrease=0.,
                 min_samples_leaf=1, random_state=None):
        """
        Random Forest Arguments.

        :param n_folds:
        :param n_estimators:
        :param max_depth:
        :param n_jobs:
        :param max_features:
        :param criterion:
        :param min_samples_split:
        :param min_impurity_decrease:
        :param min_samples_leaf:
        :param random_state:
        """
        super(RandomForestConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLRF',
            'n_folds': n_folds,
            'n_estimators': n_estimators,
            'max_depth': max_depth,
            'n_jobs': n_jobs,
            'max_features': max_features,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_impurity_decrease': min_impurity_decrease,
            'min_samples_leaf': min_samples_leaf,
            'random_state': random_state
        }

class ExtraRandomForestConfig(EstimatorConfig):
    """
    Extremely Random Forest Argument.
    """
    def __init__(self, n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, max_features=1,
                 criterion="gini", min_samples_split=2, min_impurity_decrease=0.,
                 min_samples_leaf=1, random_state=None):
        """
        Completely Random Forest Arguments, also called Extremely Random Forest.

        :param n_folds:
        :param n_estimators:
        :param max_depth:
        :param n_jobs:
        :param max_features:
        :param criterion:
        :param min_samples_split:
        :param min_impurity_decrease:
        :param min_samples_leaf:
        :param random_state:
        """
        super(ExtraRandomForestConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLCRF',
            'n_folds': n_folds,
            'n_estimators': n_estimators,
            'max_depth': max_depth,
            'n_jobs': n_jobs,
            'max_features': max_features,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_impurity_decrease': min_impurity_decrease,
            'min_samples_leaf': min_samples_leaf,
            'random_state': random_state
        }

class GBDTConfig(EstimatorConfig):
    """
    Gradient Boosting Decision Tree Argument.
    """
    def __init__(self, n_folds=3, loss='ls', learning_rate=0.1, n_estimators=100,
                 subsample=1.0, criterion='friedman_mse', min_samples_split=2,
                 min_samples_leaf=1, min_weight_fraction_leaf=0.,
                 max_depth=3, min_impurity_decrease=0.,
                 min_impurity_split=None, init=None, random_state=None,
                 max_features=None, alpha=0.9, verbose=0, max_leaf_nodes=None,
                 warm_start=False, presort='auto'):
        """
        Gradient Boosting Decision Tree Argument.

        :param n_folds: how many folds to execute in cross validation
        :param loss:
        :param learning_rate:
        :param n_estimators:
        :param subsample:
        :param criterion:
        :param min_samples_split:
        :param min_samples_leaf:
        :param min_weight_fraction_leaf:
        :param max_depth:
        :param min_impurity_decrease:
        :param min_impurity_split:
        :param init:
        :param random_state:
        :param max_features:
        :param alpha:
        :param verbose:
        :param max_leaf_nodes:
        :param warm_start:
        :param presort:
        """
        super(GBDTConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLGBDT',
            'n_folds': n_folds,
            'loss': loss,
            'learning_rate': learning_rate,
            'n_estimators': n_estimators,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_samples_leaf': min_samples_leaf,
            'min_weight_fraction_leaf': min_weight_fraction_leaf,
            'max_depth': max_depth,
            'init': init,
            'subsample': subsample,
            'max_features': max_features,
            'min_impurity_decrease': min_impurity_decrease,
            'min_impurity_split': min_impurity_split,
            'random_state': random_state,
            'alpha': alpha,
            'verbose': verbose,
            'max_leaf_nodes': max_leaf_nodes,
            'warm_start': warm_start,
            'presort': presort
        }

class XGBRegressorConfig(EstimatorConfig):
    """
    Sklearn based xgboost regressor.
    """
    def __init__(self, n_folds=3,  max_depth=3, learning_rate=0.1, n_estimators=100,
                 silent=True, objective="reg:linear",
                 nthread=-1, gamma=0, min_child_weight=1, max_delta_step=0,
                 subsample=1, colsample_bytree=1, colsample_bylevel=1,
                 reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
                 base_score=0.5, seed=0, missing=None):
        """
        XGBoost Regressor Argument describes arguments of XGBoost Regressor.
        Parameter can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html.

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param n_estimators:
        :param silent:
        :param reg_lambda:
        :param reg_alpha:
        :param gamma:
        :param min_child_weight: Defines the minimum sum of weights of all observations required in a child.
                                 Used to control over-fitting. Higher values prevent a model from learning relations
                                  which might be highly specific to the particular sample selected for a tree.
                                 Too high values can lead to under-fitting hence, it should be tuned using CV.
        :param base_score:
        :param objective: objective, default is 'reg:linear'
        :param subsample: default=1
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth: default=3, The maximum depth of a tree, same as GBM.
                          Used to control over-fitting as higher depth will allow model to learn relations
                           very specific to a particular sample.
        :param learning_rate: default=0.1
        :param seed:
        """
        super(XGBRegressorConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'max_delta_step': max_delta_step,
            'n_estimators': n_estimators,
            'reg_lambda': reg_lambda,
            'reg_alpha': reg_alpha,
            'gamma': gamma,
            'min_child_weight': min_child_weight,
            'base_score': base_score,
            'subsample': subsample,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate,
            'seed': seed,
            'missing': missing
        }

class LGBMRegressorConfig(EstimatorConfig):
    """
    Sklearn based LightGBM regressor.
    """
    def __init__(self, n_folds=3, boosting_type="gbdt", num_leaves=31, max_depth=-1,
                 learning_rate=0.1, n_estimators=100,
                 subsample_for_bin=200000, objective=None, class_weight=None,
                 min_split_gain=0., min_child_weight=1e-3, min_child_samples=20,
                 subsample=1., subsample_freq=1, colsample_bytree=1.,
                 reg_alpha=0., reg_lambda=0., random_state=None,
                 n_jobs=-1, silent=True):
        """
        Initialize FLLGBMRegressor argument.

        :param n_folds:
        :param boosting_type:
        :param num_leaves: LightGBM use num_leaves to limit tree complexity.
                           Approximately, there are `num_leaves = 2^(max_depth)`.
        :param max_depth:
        :param learning_rate:
        :param n_estimators:
        :param subsample_for_bin:
        :param objective:
        :param class_weight:
        :param min_split_gain:
        :param min_child_weight:
        :param min_child_samples:
        :param subsample:
        :param subsample_freq:
        :param colsample_bytree:
        :param reg_alpha:
        :param reg_lambda:
        :param random_state:
        :param n_jobs:
        :param silent:
        """
        super(LGBMRegressorConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLLGBM',
            'n_folds': n_folds,
            'boosting_type': boosting_type,
            'num_leaves': num_leaves,
            'max_depth': max_depth,
            'learning_rate': learning_rate,
            'n_estimators': n_estimators,
            'subsample_for_bin': subsample_for_bin,
            'objective': objective,
            'class_weight': class_weight,
            'min_split_gain': min_split_gain,
            'min_child_weight': min_child_weight,
            'min_child_samples': min_child_samples,
            'subsample': subsample,
            'subsample_freq': subsample_freq,
            'colsample_bytree': colsample_bytree,
            'reg_alpha': reg_alpha,
            'reg_lambda': reg_lambda,
            'random_state': random_state,
            'n_jobs': n_jobs,
            'silent': silent
        }

def Basic4x2(n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, min_samples_leaf=1):
    crf = ExtraRandomForestConfig(n_folds=n_folds,
                                  n_estimators=n_estimators,
                                  max_depth=max_depth,
                                  n_jobs=n_jobs,
                                  min_samples_leaf=min_samples_leaf)
    rf = RandomForestConfig(n_folds=n_folds,
                            n_estimators=n_estimators,
                            max_depth=max_depth,
                            n_jobs=n_jobs,
                            min_samples_leaf=min_samples_leaf)
    est_configs = [
        crf, crf, crf, crf,
        rf, rf, rf, rf
    ]
    return est_configs


import random
import string

from extender import *

# ----------------------------------------
# Функция для получения рандомного текста.
def randomize(number):
    final_str = ""
    counter = -1
    while counter != number:
        final_str += random.randint(1, 3).__str__() + "\n"
        text_length = random.randint(1, 50)
        for i in range(text_length):
            final_str += random.choice(string.ascii_letters)
        if counter != number - 1:
            final_str += "\n"
        counter += 1
    return final_str


from sys import stdin

def gcd(a, b):
    if b:
        return gcd(b, a%b)
    return a

A, B = map(int, stdin.readline().strip().split())
g = gcd(A,B)
print(int(g))
print(int((A*B)/g))


import requests
from bs4 import BeautifulSoup
import os
import time
import csv

headers = {
	'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.1708.400 QQBrowser/9.5.9635.400'
}

def getHTMLtext(url, code="utf-8"):
	try:
		r =requests.get(url)
		r.raise_for_status()
		r.encoding = code
		# print(r.text)
		return r.text
	except:
		return ""

# parameter
# shareCode/year/season : num ,
def sharesCrawl(shareCode,year,season):
	# shareCodeStr = str(shareCode)
	yearStr = str(year)
	seasonStr = str(season)
	url = 'http://quotes.money.163.com/trade/lsjysj_'+shareCode+'.html?year='+yearStr+'&season='+seasonStr
	print(url)
	data = requests.get(url, headers=headers)
	soup = BeautifulSoup(data.text, 'html.parser')

	table = soup.findAll('table',class_='table_bg001')[0]
	rows = table.findAll('tr')

	return rows[::-1]

def writeCSVWithName(shareCode,name,beginYear,endYear):
	# shareCodeStr = str(shareCode)

	# url = 'http://quotes.money.163.com/trade/lsjysj_' + shareCode + '.html'
	# print(url)
	# data = requests.get(url, headers=headers)
	# soup = BeautifulSoup(data.text, 'html.parser')
	csvpath = './dataWithName/' + name + '.csv'
	if(os.path.exists(csvpath)):
		os.remove(csvpath)
	csvFile = open(csvpath, 'w',encoding='utf8',newline='')
	writer = csv.writer(csvFile)
	writer.writerow(('日期','开盘价','最高价','最低价','收盘价','涨跌额','涨跌幅','成交量','成交金额','振幅','换手率'))

	for i in range(beginYear, endYear + 1):
		try:
			# print (str(i) + ' is going')
			# time.sleep(4)
			for j in range(1, 5):
				rows = sharesCrawl(shareCode,i,j)
				for row in rows:
					csvRow = []
					for cell in row.findAll('td'):
						csvRow.append(cell.get_text().replace(',',''))
					if csvRow != []:
						writer.writerow(csvRow)
				# time.sleep(3)
				# print(str(i) + '年' + str(j)  + '季度is done')
		except Exception as e:
			print(e)
			continue
	csvFile.close()

# 读取csv至字典
csvpath = r'D:\python\paper\sz50.csv' # 上证50股票名单
csvFile = open(csvpath, "r",encoding='utf-8')
reader = csv.reader(csvFile)
# 建立空字典
stocks = []
stockdic = {}
for item in reader:
	# 忽略第一行
	if reader.line_num == 1:
		continue
	stocks.append(str(item[1]))
	stockdic[str(item[1])] = item[0]
	# print(item)
csvFile.close()

i = 0
for k in stocks:
	if(i >= 100):
		break
	writeCSVWithName(k,stockdic[k],2007,2017)
	# print(k+","+stockdic[k])
	i = i + 1


# 汉诺塔问题
def move(n, a, b, c):
	# n表示有几个圆盘
	if n == 0:
		return
	else:
		move(n-1, a, c, b)
		print('moving from %s to %s'%(a, c))
		move(n-1, b, a, c)
	return

if __name__ == '__main__':
	# 初始化三个塔
	move(6, 'a', 'b', 'c')


''' 11   Desenvolva um gerador de tabuada, capaz de gerar a tabuada de qualquer número inteiro entre 1 a 10. O usuário deve informar de qual numero ele deseja ver a tabuada. A saída deve ser conforme o exemplo abaixo:
Tabuada de 5:
5 X 1 = 5
5 X 2 = 10
...
5 X 10 = 50
OBS: Deve ser utilizado a estrutura de repetição "while"
'''
tabuada = int(input("Tabuada do número: "))
i = 0
while i <=10:
    print(tabuada, "x", i, "=", tabuada*i)
    i+=1


from mlc.utils.io_utils import get_files_given_a_pattern
import numpy as np
import json
import os
from pathlib import Path

def get_stats_scores(dir_results):
    scores = get_scores(dir_results)

    data_out = dict(training_exp=Path(dir_results).stem)
    for scores_by in ["best_2d_iou_score", "best_3d_iou_score"]:
        scores = {k: v for k, v in sorted(scores.items(), key=lambda item: item[1]['best_iou_valid_score'][scores_by])}
        list_scores = [sc['best_iou_valid_score'][scores_by] for sc in scores.values()]
        data_out[f"by_{scores_by}"] = {list(scores.keys())[-1]: scores[list(scores.keys())[-1]]}
        data_out[f"by_{scores_by}"]["stats"]=dict(
                    std=np.std(list_scores),
                    variance=np.std(list_scores)**2,
                    median=np.median(list_scores),
                    mean=np.mean(list_scores),
                    max=np.max(list_scores),
                    min=np.min(list_scores),
                    scores=list_scores,
                    samples=list_scores.__len__())

    # scores = {k: v for k, v in sorted(scores.items(), key=lambda item: item[1]['best_h_valid_score'])}
    # list_scores = [sc['best_h_valid_score'] for sc in scores.values()]
    # data_out[f"by_best_h_valid_score"] = {list(scores.keys())[-1]: scores[list(scores.keys())[-1]]}
    # data_out[f"by_best_h_valid_score"]["stats"]=dict(
    #                 std=np.std(list_scores),
    #                 variance=np.std(list_scores)**2,
    #                 median=np.median(list_scores),
    #                 mean=np.mean(list_scores),
    #                 max=np.max(list_scores),
    #                 min=np.min(list_scores),
    #                 scores=list_scores,
    #                 samples=list_scores.__len__())
    return data_out

def get_scores(training_dir):
    list_json_fn  = get_files_given_a_pattern(training_dir, "best_score.json", exclude='log')
    if list_json_fn.__len__() == 0:
        assert list_json_fn.__len__() > 0, f"No best_score.json files found at {os.path.join(training_dir)}"
    scores = {}
    for json_fn in list_json_fn:
        exp_label = json_fn.split("/")[-2]
        scores[exp_label] = json.load(open(os.path.join(json_fn, "best_score.json")))
    return scores


import datetime
import os
import jwt
from typing import Optional

def create_jwt(
    username: str,
    secret_key: Optional[str] = None,
    expires_seconds: Optional[int] = None,
):
    if not secret_key:
        secret_key = os.environ["SECRET_KEY"]

    if not expires_seconds:
        expires_seconds = int(os.environ["JWT_ACCESS_TOKEN_EXPIRES_SECONDS"])

    iat = datetime.datetime.now(tz=datetime.timezone.utc)
    exp = iat + datetime.timedelta(seconds=expires_seconds)

    payload = {"sub": username, "iat": iat, "exp": exp}

    return jwt.encode(payload=payload, key=secret_key)


#import Adafruit_BME280
import os
import json
import random

PiWeather = "/var/www/html/cash_json/PiWeather.json"

def readWeather(sensor):
    degrees = "%.1f" % random.uniform(71.2, 73.4)
    hectopascals = "%.2f" % (random.randint(930, 937) * 0.02953)
    humidity = random.randint(12, 15)

    j_obj = {};
    j_obj['module'] = "weather"
    j_obj['temp'] = degrees
    j_obj['pressure'] = hectopascals
    j_obj['humidity'] = humidity

    output = json.dumps(j_obj)

    file_obj = open(PiWeather, "w")
    file_obj.truncate()
    file_obj.write(output)
    file_obj.close()


from collections import defaultdict
import os
import pickle
import time
import pandas as pd

import networkx as nx
import random
from numpy import cumsum, array
import psutil
from tqdm import tqdm

indexstorepath = "/data/lijiajun/infogather/opendata/index/"

fileppr = os.path.join(indexstorepath,"ppr_matrix.pkl")
with open(fileppr ,"rb") as f:
    pprdict = pickle.load(f)

inversedfile = os.path.join(indexstorepath,"inversed_index.pkl")
with open(inversedfile, 'rb') as file:
    inversed = pickle.load(file)

KIVfile =  os.path.join(indexstorepath,"KIV.pkl")
with open(KIVfile, 'rb') as file:
    KIV = pickle.load(file)

KIAfile =  os.path.join(indexstorepath,"KIA.pkl")
with open(KIAfile, 'rb') as file:
    KIA = pickle.load(file)

docNofile =  os.path.join(indexstorepath,"docNo.pkl")
with open(docNofile, 'rb') as file:
    docNo = pickle.load(file)

print("graph load")

def querytable_columns(tablefile,column,inversed,KIV,KIA,docNo,pprdict):
    df = pd.read_csv(tablefile,encoding= 'ISO-8859-1',low_memory=False,lineterminator='\n')
    columnvalues = df[column].unique()

    columnvalues_len = len(columnvalues)

    KIA_docs_set = set(KIA[column])

    KIV_docs_list = []
    for value in columnvalues:
        KIV_docs_list += inversed[value]

    KIV_docs_set = set(KIV_docs_list)

    KIV_KIA_set = KIA_docs_set & KIV_docs_set

    # compute weight
    weigth_dict = {}
    for doc in KIV_KIA_set:
        docvalue_set = set(KIV[doc])
        doclen = len(docvalue_set)
        overlapevalues = docvalue_set & set(columnvalues)
        weight = len(overlapevalues) / min(doclen,columnvalues_len)
        weigth_dict[doc] = weight

    # get all the weigted dict
    total_ppr_list = []
    for doc  in KIV_KIA_set:
        doc_weigth = weigth_dict[doc]
        doc_dict_weigth = {key:value*doc_weigth for key,value in pprdict[doc].items()}
        total_ppr_list.append(doc_dict_weigth)

    # compute the  weighted dict
    total_ppr = defaultdict(float)
    for ppr_ele in total_ppr_list:
        for key,value in ppr_ele.items():
            total_ppr[key] += value

    sorted_dict = dict(sorted(total_ppr.items(), key=lambda x: x[1],reverse=True))

    return_doc_list = list(sorted_dict.keys())[:30]

    return_name = []
    for docnumber in return_doc_list:
        return_name.append(docNo[docnumber])

    return return_name
# queryfilepath = "/data_ssd/webtable/large/split_1/csvData10000001.csv"
# columnname= "Sample Date"
# res = querytable_columns(queryfilepath,columnname,inversed,KIV,KIA,graph,docNo)

filepath = "/data_ssd/opendata/small/small_join.csv"
indexstorepath = "/data/lijiajun/infogather/opendata/index"

df = pd.read_csv(filepath)
resultdict = {}

total_rows = len(df)
stime = time.time()
# 使用 tqdm 添加进度条
for index, row in tqdm(df.iterrows(), total=total_rows):
    try:
        querytable = row[0]
        column = row[1]
        if querytable and column:
            queryfilepath = os.path.join("/data_ssd/opendata/small/query",querytable)
            res_list = querytable_columns(queryfilepath,column,inversed,KIV,KIA,docNo,pprdict)
            key = querytable + str("###") + column
            resultdict[key] = res_list
    except Exception as e:
        print(row)
        print(e)
process = psutil.Process(os.getpid())
print("当前内存占用：%s" % (process.memory_info().rss / 1024 / 1024/1024))
etime = time.time()

print("time consumer ",etime - stime)
print("运行完成")


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

import os

from brew.parsers import JSONDataLoader
from brew.parsers import parse_recipe

"""
Build a recipe using data parsers.
"""

def main():

    recipe = {
        u"name": u"Pale Ale",
        u"start_volume": 7.0,
        u"final_volume": 5.0,
        u"grains": [
            {
                u"name": u"pale malt 2-row us",
                u"data": {u"color": 1.8, u"ppg": 37},
                u"weight": 13.96,
            },
            {
                u"name": u"caramel crystal malt 20l",
                u"data": {u"color": 20.0, u"ppg": 35},
                u"weight": 0.78,
            },
        ],
        u"hops": [
            {
                u"name": u"centennial",
                u"data": {u"percent_alpha_acids": 0.14},
                u"weight": 0.57,
                u"boil_time": 60.0,
            },
            {
                u"name": u"cascade us",
                u"data": {u"percent_alpha_acids": 0.07},
                u"weight": 0.76,
                u"boil_time": 5.0,
            },
        ],
        u"yeast": {u"name": u"Wyeast 1056", u"data": {u"percent_attenuation": 0.75}},
        u"data": {u"brew_house_yield": 0.70, u"units": u"imperial"},
    }

    data_dir = os.path.abspath(os.path.join(os.getcwd(), "data/"))
    loader = JSONDataLoader(data_dir)
    beer = parse_recipe(recipe, loader)
    print(beer.format())

if __name__ == "__main__":
    main()


#! /usr/bin/env python2.7
"""
    This file contains get_qr_data,get_item_details,get_inventory_string and Camera1.
"""

import rospy
import cv2
import datetime
from cv_bridge import CvBridge, CvBridgeError
from pyzbar.pyzbar import decode

def get_qr_data(arg_image):
    """
        This function decode data from qr code and return package info.
        :param arg_image: Details of Packagen in QR code encoded form.
        :return String of Decoded information of Packagen.
    """

    qr_result = decode(arg_image)
    for i in range(len(qr_result)):
          print(qr_result[i].data)
    if len(qr_result) > 0:
          return qr_result[0].data
    else :
          return 'NA'

def get_item_details(color):
  """
    This function returns specification of Packgen based on color.
    :param color: It is color of the Packgen! Its possible values are red,yellow and green.
    :return:details,details is a dict containing information of packgen.It contains item,sku_alpha,priority,cost and Estimated Time of Delivery.
  """

  details = {'item':'Na','sku_alpha':'Na','priority':'Na','cost':'Na','Estimated Time of Delivery':'Na'}

  if color == 'red':
    details = {'item':'Medicines','sku_alpha':'R','priority':'HP','cost':'450','Estimated Time of Delivery':'1'}
  elif color == 'yellow':
    details = {'item':'Food','sku_alpha':'Y','priority':'MP','cost':'250','Estimated Time of Delivery':'3'}
  elif color == 'green':
    details = {'item':'Clothes','sku_alpha':'G','priority':'LP','cost':'150','Estimated Time of Delivery':'5'}

  return details

def get_inventory_string(color, i, j):
  """
  This function create a coma separated string of packgen information to send data on the inventory sheet.
  :param color:It is color of packgen.It can be red,yellow and green.
  :param i:i is index of column of packgen in the shelf.
  :param j:j is index of row of packgen in the shelf.
  :return: returns a string containing each column details of inventory sheet separated by comas.
  """
  details = get_item_details(color)
  date_now = datetime.datetime.now()
  storage_number = 'R'+str(i)+' C'+str(j)
  return 'Inventory,'+details['sku_alpha']+str(i)+str(j)+date_now.strftime("%m")+date_now.strftime("%y")+','+details['item']+','+details['priority']+','+storage_number+','+details['cost']+',1'

class Camera1:
  """
  This is a class for finding

   details of each packgen present in the sheet, using camera1 module.
  """

  def __init__(self,temp):
    """
    This is constructor of class Camera1
    :param temp: A variable to unregister subscriber after execution.
    """
    self.bridge = CvBridge()
    self.temp = temp
    self.list = {}
    self. inventory = []

  def callback(self,data):
        """
        This function is callback of camera1 module.
        :param data: result of camera1
        :return: null
        """
        cv_image = []
        try:
          cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
          rospy.logerr(e)

        image = cv_image
        l={}
        for i in range(4):
          for j in range(3):
            down=160*i
            side=180*j
            crop_image=image[280+down:440+down,90+side:270+side]
            colr = get_qr_data(crop_image)
            l['packagen'+str(i)+str(j)]= colr
            self.inventory.append(get_inventory_string(colr, i, j))
            rospy.loginfo(get_qr_data(crop_image))
        print(l,self.inventory,'Callback')
        self.list = l
        self.temp.unregister()
        cv2.waitKey(3)


'''
This file reproduce the faro sift use in python
'''
#%% Import section
import numpy as np
import cv2 as cv
#%% Im read
img = cv.imread('ReferenceImages/rearCIC.png')
gray= cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#%% sift creator
sift = cv.xfeatures2d.SIFT_create()
#%% sift detection
kp = sift.detect(gray,None)
img = cv.drawKeypoints(gray, kp, img)

cv.imwrite('sift_keypoints.jpg', img)


from flask import Flask, request, make_response, Response
from flask_restful import Api, Resource, reqparse
import time, json, os, yaml, io
from API_executor.Authentication import invalidToken
import tarfile

import Database as db
import config

file_error_string = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                            <error>
                                1308
                                <description>Given app package file is invalid: Unsupported Format</description>
                            </error>'''

def allowed_file(filename):
        return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in set(["gz", "tar"])

def createApplicationJSON(creationDate,
                              lastupdateTime,
                              descriptorSchemaVersion,
                              info,
                              app,
                              signed,
                              appid,
                              version,
                              name,
                              description,
                              apptype,
                              published,
                              resources,
                              cpuUsage,
                              memoryUsage):
        return {
                    "icon": {
                        "caption": "icon",
                        "href": None
                    },
                    "images": [],
                    "packages": [
                        {
                            "href": "api/v1/appmgr/localapps/%s:1/packages/e0c9d17e-05a5-4253-a0c9-55e8a6da12c6" % appid
                        }
                    ],
                    "creationDate": creationDate,
                    "lastUpdatedDate": lastupdateTime,
                    "descriptor": {
                        "descriptor-schema-version": descriptorSchemaVersion,
                        "info": info,
                        "app": app
                    },
                    "signed": signed,
                    "localAppId": appid,
                    "version": version,
                    "name": name,
                    "description": {
                        "contentType": "text",
                        "content":  description
                    },
                    "releaseNotes": {
                        "contentType": "text"
                    },
                    "appType": apptype,
                    "categories": [],
                    "vendor": "",
                    "published": published,
                    "services": [],
                    "profileNeeded": resources["profile"],
                    "cpuUsage": cpuUsage,
                    "memoryUsage": memoryUsage,
                    "classification": "APP",
                    "properties": [],
                    "sourceAppName": str(appid)+":"+str(version)
                }

# /api/v1/appmgr/localapps/upload
def post(args, request, uploadDir, filename):
    if db.checkToken(args["x-token-id"]):
        # Extracting file
        os.chdir(uploadDir)

        tmpDir = str(hash(filename))
        if not os.path.exists(tmpDir):
            os.makedirs(tmpDir)

        if (filename.endswith("tar.gz")):
            tar = tarfile.open(filename, "r:gz")
            os.chdir(tmpDir)
            tar.extractall()
            tar.close()

        elif (filename.endswith("tar")):
            tar = tarfile.open(filename, "r:")
            os.chdir(tmpDir)
            tar.extractall()
            tar.close()

        # Opening YAML Description of the application
        try:
            with open("package.yaml", 'r') as stream:
                app_data = yaml.load(stream)
        except IOError:
            return file_error_string, 400, {"Content-Type": "application/xml"}
        os.chdir("../")

        if db.getMyApps(searchByName = app_data["info"]["name"]).count() != 0:
            return '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                    <error>
                        1316
                        <description>An app with the same deployId already exists. Please make sure that the characters of the app do not match with any of the existing apps.</description>
                    </error>''', 409, {"Content-Type": "application/xml"}

        appJson = createApplicationJSON(creationDate=int(time.time()),
                                        lastupdateTime=int(time.time()),
                                        descriptorSchemaVersion=app_data["descriptor-schema-version"],
                                        info=app_data["info"],
                                        app=app_data["app"],
                                        signed=False,
                                        appid=-1,
                                        version=app_data["info"]["version"],
                                        name=app_data["info"]["name"],
                                        description=app_data["info"]["description"],
                                        apptype=app_data["app"]["type"],
                                        published=False,
                                        resources=app_data["app"]["resources"],
                                        cpuUsage=0,
                                        memoryUsage=0)
        if(args["x-publish-on-upload"] == "true" or
            args["x-publish-on-upload"] == "True" or
            args["x-publish-on-upload"] == True):
            appJson["published"] = True
        appID = str(db.addLocalApplication(appJson))
        appJson["localAppId"] = appID
        appJson["sourceAppName"] = str(appID)+":1"
        os.rename(tmpDir, appID)

        os.chdir("../")
        appReturn = db.getLocalApplication(appID)
        del appReturn["_id"]
        response = Response(json.dumps(appReturn), 201, mimetype="application/json;charset=UTF-8")
        return response # and finally yes, it returns also the charset here!
    else:
        return invalidToken()

# /api/v1/appmgr/localapps/<appid>:<appversion>
def put(args, data, appURL):
    if db.checkToken(args["x-token-id"]):
        tmp = appURL.split(":")
        appid = tmp[0]
        if len(tmp) > 1:
            appversion = tmp[1]
        else:
            appversion = 1 # default value
        if(not db.localApplicationExists(appid, appversion)):
            return notFoundApp(appid)
        if(appversion == None):
            return notFoundApp(appid) # this error is returned even if the application exists, but no version is specified
        db.updateLocalApplication(appid, data)
        # Another error should be returned if the data passed is not completed as stored in DB. All the field
        # have to be passed, also the unchanged ones. I ignore this error.
        app = db.getLocalApplication(appid)
        del app["_id"]
        return app, 200, {"Content-Type": "application/json"}
    else:
        return invalidToken()

# /api/v1/appmgr/apps/<appid> <-- WTF? Why apps and not localapps?!!! CISCOOOOO!!!!
def delete(args, appURL):
    tmp = appURL.split(":")
    appid = tmp[0]
    if len(tmp) > 1:
        appversion = tmp[1]
    else:
        appversion = 1 # default value

    if db.checkToken(args["x-token-id"]):
        if args["x-unpublish-on-delete"] == None:
            app = db.getLocalApplication(appid)
            if app == None:
                return "", 200 # Yes, it returns 200 even if the application doesn't exists
            if app["published"]:
                return """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                            <error>
                                1303
                                <description>App %s is in use: Unable to delete apps with name %s
                            As app with name %s and version%d is in published state</description>
                        </error>""" % (app["name"], app["name"], app["name"], app["name"]), 400, {"Content-Type": "application/xml"} # TODO: customize returned error name
        db.deleteLocalApplication(appid)
        return "", 200
    else:
        return invalidToken()

# /api/v1/appmgr/localapps/ Undocumented but works!
def get(args):
    if db.checkToken(args["x-token-id"]):
        data = {"data": []}
        apps = db.getLocalApplications()
        for app in apps:
            del app["_id"] # removing internal ID, not JSON serializable object
            data["data"].append(app)
        return data, 200, {"Content-Type": "application/json"}
    else:
        return invalidToken()

def notFoundApp(appid):
    return """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <error>
                1301
                <description>An app with given id %s cannot be found.</description>
            </error>""" % appid, 404, {"Content-Type": "application/xml"}


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Author       : windz
Date         : 2022-01-04 19:49:41
LastEditTime : 2022-01-17 11:09:46
LastEditors  : windz
FilePath     : /data/Zhaijx/mowp/data/nanopore_data/Repository/script/get_3end_within_intron_reads.py
'''

import sys

try:
    infile = sys.argv[1]  # 20211028_col_seedling_total_R1.read.info.txt
    outfile = sys.argv[2]  # 20211028_col_seedling_total_R1.intron.read.info.txt
except IndexError:
    print('Input or Output not detected')

if infile.endswith('.gz'):
    import gzip
    f = gzip.open(infile, 'rt')
else:
    f = open(infile, 'r')

with open(outfile, 'w') as o:
    line = next(f)
    o.write(line)

    for line in f:
        line_ = line.rstrip().split('\t')
        if line_[24] == 'intron':
            o.write(line)

f.close()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Time: 2019/11/18 17:31
# Author: Hou hailun

"""
给定两个没有重复元素的数组 nums1 和 nums2 ，其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。
nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在，对应位置输出-1。
示例 1:
输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
    对于num1中的数字4，你无法在第二个数组中找到下一个更大的数字，因此输出 -1。
    对于num1中的数字1，第二个数组中数字1右边的下一个较大数字是 3。
    对于num1中的数字2，第二个数组中没有下一个更大的数字，因此输出 -1。
示例 2:
输入: nums1 = [2,4], nums2 = [1,2,3,4].
输出: [3,-1]
解释:
    对于num1中的数字2，第二个数组中的下一个较大数字是3。
    对于num1中的数字4，第二个数组中没有下一个更大的数字，因此输出 -1。
"""

class Solution(object):
    def nextGreaterElement(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        # 方法1： 暴力求解, 两次循环遍历O(N*N)
        # if nums1 is None or nums2 is None:
        #     return [-1] * len(nums1)
        #
        # res = [-1] * len(nums1)
        # for i in range(len(nums1)):
        #     ix = nums2.index(nums1[i])
        #     print('---',i, ix)
        #     print('--------------')
        #     for j in range(ix, len(nums2)):
        #         print(j, nums2[j])
        #         if nums2[j] > nums1[i]:
        #             res[i] = nums2[j]
        #             break
        # return res

        # 方法2: 栈实现
        stack = []
        hashmap = {}
        # 遍历nums2，把大值作为val，小值作为key，只要第一个大值
        for num in nums2:
            while stack and stack[-1] < num:
                hashmap[stack.pop()] = num
            stack.append(num)
        print(hashmap)
        return [hashmap.get(x, -1) for x in nums1]

obj = Solution()
ret = obj.nextGreaterElement([4,1,2], [1,3,4,2])
print(ret)