from __future__ import unicode_literals
from __future__ import division

import os
import sys

import StringIO

import codecs

from panda3d.core import *
from panda3d.egg import *

from common import *

from pymeshio import pmd
from pymeshio.pmd import reader as pmdReader

DEBUG = True
DEBUG = False

# Get the location of the 'py' file I'm running:
CWD = os.path.abspath(sys.path[0])

def pmd2model(pmd):
  pmdModel = pmdLoad(pmd)
  p3dmodel = pmd2p3d(pmdModel)
  return(p3dmodel)

def pmdLoad(f_pmd):
  model = None
  if os.path.isfile(f_pmd):
    model = pmdReader.read_from_file(f_pmd)
  return(model)

def pmdInfo(model, screen=False):
  lines = []
  if isinstance(model, pmd.Model):
    lines.append(u'path         : %s' % model.path)
    lines.append(u'version      : %s' % model.version)
    lines.append(u'name(jpn)    : %s' % model.name.decode('shift_jis', errors='replace'))
    lines.append(u'name(eng)    : %s' % model.english_name.decode('shift_jis', errors='replace').strip())
    lines.append(u'comment(jpn) : \n{0}\n{1}\n{0}'.format('-'*80, model.comment.decode('shift_jis', errors='replace')))
    lines.append(u'comment(eng) : \n{0}\n{1}\n{0}'.format('-'*80, model.english_comment.decode('shift_jis', errors='replace')))
    lines.append(u'='*80)

    lines.append(u'toon_textures: Total {1}.\n{0}'.format('-'*80, len(model.toon_textures)))
    idx = 0
    for texture in model.toon_textures:
      lines.append('%4d : %s' % (idx, texture))
      idx += 1
    lines.append(u'='*80)

    lines.append(u'materials    : Total {1}.\n{0}'.format('-'*80, len(model.materials)))
    idx = 0
    for mat in model.materials:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  diffuse   : (%s, %s, %s)' % (mat.diffuse_color.r, mat.diffuse_color.g, mat.diffuse_color.b))
      lines.append(u'  alpha     : %.2f' % mat.alpha)
      lines.append(u'  specular  : (%s, %s, %s), %.2f' % (mat.specular_color.r, mat.specular_color.g, mat.specular_color.b, mat.specular_factor))
      lines.append(u'  ambient   : (%s, %s, %s)' %  (mat.ambient_color.r, mat.ambient_color.g, mat.ambient_color.b))
      lines.append(u'  edge_flag : %s' % mat.edge_flag)
      lines.append(u'  texture   : %s' % mat.texture_file)
      lines.append(u'  toon      : %4d' % (mat.toon_index))
      lines.append(u'  vertexs   : %4d' % mat.vertex_count)
    lines.append(u'='*80)

    lines.append(u'bones        : Total {1}.\n{0}'.format('-'*80, len(model.bones)))
    idx = 0
    for bone in model.bones:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)    : %s' % bone.name.decode('shift_jis', errors='replace'))
      lines.append(u'  name(eng)    : %s' % bone.english_name.decode('shift_jis', errors='replace'))
      lines.append(u'  index        : %4d' % bone.index)
      lines.append(u'  type         : %4d' % bone.type)
      lines.append(u'  pos          : %s' % str(bone.pos.to_tuple()))

      lines.append(u'  parent       : %4d, %s' % (bone.parent_index, (bone.parent.name.decode('shift_jis', errors='replace') if bone.parent else u'NULL')))
      lines.append(u'  tail         : %4d, %s' % (bone.tail_index, bone.tail))
      lines.append(u'  children     : %s' % bone.children[:5])
      lines.append(u'  ik_index     : %4d' % bone.ik_index)
    lines.append(u'='*80)

    lines.append(u'ik_list      : Total {1}.\n{0}'.format('-'*80, len(model.ik_list)))
    idx = 0
    for ik in model.ik_list:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  index      : %4d' % ik.index)
      lines.append(u'  target     : %s' % ik.target)
      lines.append(u'  weight     : %4d' % ik.weight)
      lines.append(u'  length     : %4d' % ik.length)
      lines.append(u'  children   : %s' % str(ik.children[:5]))
      lines.append(u'  iterations : %4d' % ik.iterations)
    lines.append(u'='*80)

    lines.append(u'morphs       : Total {1}.\n{0}'.format('-'*80, len(model.morphs)))
    idx = 0
    for morph in model.morphs:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)    : %s' % morph.name.decode('shift_jis', errors='replace'))
      lines.append(u'  name(eng)    : %s' % morph.english_name.decode('shift_jis', errors='replace'))
      lines.append(u'  type         : %4d' % morph.type)
      lines.append(u'  pos_list     : %s' % str(morph.pos_list[:4]))
      lines.append(u'  vertex_count : %4d' % morph.vertex_count)
      lines.append(u'  indices      : %4d, %s' % (len(morph.indices), morph.indices[:5]))
    lines.append(u'='*80)

    lines.append(u'morph_indices: Total {1}.\n{0}'.format('-'*80, len(model.morph_indices)))
    idx = 0
    for indices in model.morph_indices:
      if idx != 0: lines.append('')
      lines.append(u'  %8d : %8d' % (idx, indices))
      idx += 1
    lines.append(u'='*80)

    lines.append(u'bone_group_list : Total {1}.\n{0}'.format('-'*80, len(model.bone_group_list)))
    idx = 0
    for slot in model.bone_group_list:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)    : %s' % slot.name.decode('shift_jis', errors='replace'))
      lines.append(u'  name(eng)    : %s' % slot.english_name.decode('shift_jis', errors='replace'))
    lines.append(u'='*80)

    lines.append(u'bone_display_list : Total {1}.\n{0}'.format('-'*80, len(model.bone_display_list)))
    idx = 0
    for slot in model.bone_display_list:
      if idx != 0: lines.append('')
      lines.append(u'  %8d : %s' % (idx, slot))
      idx += 1
    lines.append(u'='*80)

    lines.append(u'rigidbodies  : Total {1}.\n{0}'.format('-'*80, len(model.rigidbodies)))
    idx = 0
    for rigidbody in model.rigidbodies:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)          : %s' % rigidbody.name.decode('shift_jis', errors='replace'))
      lines.append(u'  bone_index         : %4d' % rigidbody.bone_index)
      lines.append(u'  collision_group    : %4d' % rigidbody.collision_group)
      lines.append(u'  no_collision_group : %4d' % rigidbody.no_collision_group)
      lines.append(u'  shape              : %4d, %s, %s, %s' % (rigidbody.shape_type, rigidbody.shape_size.to_tuple(), rigidbody.shape_position.to_tuple(), rigidbody.shape_rotation.to_tuple()))
      lines.append(u'  param              : %4d, %.4f, %.4f, %.4f, %.4f' % (rigidbody.mass, rigidbody.linear_damping, rigidbody.angular_damping, rigidbody.restitution, rigidbody.friction))
      lines.append(u'  mode               : %4d' % rigidbody.mode)
    lines.append(u'='*80)

    lines.append(u'joints       : Total {1}.\n{0}'.format('-'*80, len(model.joints)))
    idx = 0
    for joint in model.joints:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)         : %s' % joint.name.decode('shift_jis', errors='replace'))
      lines.append(u'  rigidbody_index   : %4d, %4d' % (joint.rigidbody_index_a, joint.rigidbody_index_b))
      lines.append(u'  position          : %s' % str(joint.position.to_tuple()))
      lines.append(u'  rotation          : %s' % str(joint.rotation.to_tuple()))
      lines.append(u'  translation_limit : %s, %s' % (joint.translation_limit_min.to_tuple(), joint.translation_limit_max.to_tuple()))
      lines.append(u'  rotation_limit    : %s, %s' % (joint.rotation_limit_min.to_tuple(), joint.rotation_limit_max.to_tuple()))
      lines.append(u'  spring_constant   : %s, %s' % (joint.spring_constant_translation.to_tuple(), joint.spring_constant_rotation.to_tuple()))
    lines.append(u'='*80)

    lines.append(u'no_parent_bones : Total {1}.\n{0}'.format('-'*80, len(model.no_parent_bones)))
    idx = 0
    for bone in model.no_parent_bones:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  name(jpn)    : %s' % bone.name.decode('shift_jis', errors='replace'))
      lines.append(u'  name(eng)    : %s' % bone.english_name.decode('shift_jis', errors='replace'))
    lines.append(u'='*80)

    lines.append(u'vertices     : Total {1}.\n{0}'.format('-'*80, len(model.vertices)))
    idx = 0
    for vertex in model.vertices:
      if idx != 0: lines.append('')
      idx += 1
      lines.append(u'  pos       : %s' % str(vertex.pos.to_tuple()))
      lines.append(u'  normal    : %s' % str(vertex.normal.to_tuple()))
      lines.append(u'  uv        : %s' % str(vertex.uv.to_tuple()))
      lines.append(u'  bone0     : %s' % str(vertex.bone0))
      lines.append(u'  bone1     : %s' % str(vertex.bone1))
      lines.append(u'  weight0   : %s' % str(vertex.weight0))
      lines.append(u'  edge_flag : %4d' % vertex.edge_flag)
    lines.append(u'='*80)

    lines.append(u'indices      : Total {1}.\n{0}'.format('-'*80, len(model.indices)))
    idx = 0
    for indic in model.indices:
      lines.append(u'  %8d : %8d' % (idx, indic))
      idx += 1
    lines.append(u'='*80)

  if screen:
    for line in lines:
      print(line)
  return(lines)
  pass

def pmd2p3d(pmd_model):
  return(loadPmdBody(pmd_model))

def loadPmdBody(pmd_model, alpha=True):
  modelPath = os.path.dirname(pmd_model.path)

  #
  # load textures
  #
  textures = TextureCollection()
  for tex in pmd_model.toon_textures:
    tex_path = os.path.join(modelPath, tex)
    if tex.lower() in ['toon01.bmp', 'toon02.bmp', 'toon03.bmp', 'toon04.bmp', 'toon05.bmp',
                       'toon06.bmp', 'toon07.bmp', 'toon08.bmp', 'toon09.bmp', 'toon10.bmp']:
      tex_path = u'toon/%s' % tex
    log(u'Loading Texture : %s' % tex_path)
    texture = loadTexture(tex_path)
    if texture:
      textures.append(texture)
      log(u'Loaded Texture : %s' % tex_path, force=True)
      log(texture)
    else:
      textures.append(Texture('NULL'))
      log('Texture Error: %s' % tex_path)

  #
  # load materials
  #
  materials = MaterialCollection()
  matIndex = 0
  for mat in pmd_model.materials:
    matName = u'mat_%04d' % matIndex
    log(u'Loading Material : %s' % matName)
    material = Material(matName)
    material.setDiffuse(VBase4(mat.diffuse_color.r, mat.diffuse_color.g, mat.diffuse_color.b, mat.alpha))

    # material.setSpecular(VBase4(mat.specular_color.r, mat.specular_color.g, mat.specular_color.b, 1))
    if mat.specular_factor > 0 or (mat.specular_color.r != 1 and mat.specular_color.g != 1 and mat.specular_color.b != 1):
      material.setSpecular(VBase4(mat.specular_color.r, mat.specular_color.g, mat.specular_color.b, 1))
      material.setShininess(mat.specular_factor*25)
    else:
      material.setSpecular(VBase4(mat.ambient_color.r, mat.ambient_color.g, mat.ambient_color.b, 0.01))
      material.setShininess(0)

    material.setAmbient(VBase4(mat.ambient_color.r, mat.ambient_color.g, mat.ambient_color.b, 1))

    material.setLocal(True)
    # material.setTwoside(True)
    materials.addMaterial(material)
    matIndex += 1
    log(u'Loaded Material : %s' % (matName), force=True)

  modelName = pmd_model.name.decode('shift_jis', errors='replace')
  #
  # load vertices(vertex list)
  #
  formatArray = GeomVertexArrayFormat()
  formatArray.addColumn(InternalName.make(str("edge_factor")), 1, Geom.NTFloat32, Geom.COther)
  formatArray.addColumn(InternalName.make(str("drawFlag")), 1, Geom.NTUint8, Geom.COther)
  formatArray.addColumn(InternalName.make(str("index")), 1, Geom.NTUint32, Geom.CIndex)
  # formatArray.addColumn(InternalName.make(str("morph")), 1, Geom.NTFloat32, Geom.CMorphDelta)

  format = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2())
  format.addArray(formatArray)
  format = GeomVertexFormat.registerFormat(format)

  vdata = GeomVertexData(modelName, format, Geom.UHDynamic)
  vdata.setNumRows(len(pmd_model.vertices))

  vertex = GeomVertexWriter(vdata, 'vertex')
  normal = GeomVertexWriter(vdata, 'normal')
  color = GeomVertexWriter(vdata, 'color')
  texcoord = GeomVertexWriter(vdata, 'texcoord')
  edge = GeomVertexWriter(vdata, 'edge_factor')
  index = GeomVertexWriter(vdata, 'index')

  idx = 0
  for v in pmd_model.vertices:
    vertex.addData3f(V2V(v.pos))
    # normal.addData3f(V2V(v.normal))
    normal.addData3f(N2N(v.normal))
    color.addData4f(.95, .95, .95, 1)
    texcoord.addData2f(v.uv.x, v.uv.y)
    edge.addData1f(1.0)
    index.addData1i(idx)
    idx += 1

  #
  # load polygons face
  #
  vIndex = 0
  model = Character(modelName)
  model.setPythonTag('path', pmd_model.path)
  model.setPythonTag('version', str(pmd_model.version))
  model.setPythonTag('name', modelName)
  model.setPythonTag('english_name', pmd_model.english_name)
  model.setPythonTag('comment', pmd_model.comment)
  model.setPythonTag('english_comment', pmd_model.english_comment)

  modelBody = ModelRoot('Body')
  model.addChild(modelBody)

  matIndex = 0
  texList = dict()
  for mat in pmd_model.materials:
    matName = u'mat_%04d' % matIndex
    prim = GeomTriangles(Geom.UHStatic)
    log(u'Loading Node : %s' % matName)
    for idx in xrange(vIndex, vIndex+mat.vertex_count, 3):
      # flip trig-face for inverted axis-y/axis-z
      prim.addVertices(pmd_model.indices[idx+2], pmd_model.indices[idx+1], pmd_model.indices[idx+0])

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node = GeomNode(matName)
    node.addGeom(geom)
    nodePath = NodePath(node)

    #
    # set polygon face material
    #
    nodePath.setMaterial(materials[matIndex], 1) #Apply the material to this nodePath
    nodePath.setTwoSided(materials[matIndex].getTwoside())

    nodePath.setPythonTag('material', materials[matIndex])
    nodePath.setPythonTag('pickableObjTag', 1)

    #
    # set polygon face textures
    #
    texFileMain = None
    texFileSphere = None

    if (not mat.texture_file) and mat.toon_index < 0:
      nodePath.setTransparency(TransparencyAttrib.MDual, matIndex)

    if mat.alpha<1:
      nodePath.setTransparency(TransparencyAttrib.MAlpha, matIndex)

    if mat.texture_file and len(mat.texture_file) >= 0:
      texName = mat.texture_file.decode('shift_jis', errors='replace')
      tex_list = texName.split('*')
      if len(tex_list)==1:
        tex_list = texName.split('/')
      if len(tex_list)==2:
        texFileMain = tex_list[0].strip()
        texFileSphere = tex_list[1].strip()
      else:
        ext = os.path.splitext(texName)[1]
        if ext.lower() in ['.spa', '.sph']:
          texFileMain = None
          texFileSphere = texName
        else:
          texFileMain = texName
          texFileSphere = None

      if texFileMain:
        if not texFileMain in texList:
          texList[texFileMain] = loadTexture(os.path.join(modelPath, texFileMain))
          if texList[texFileMain]:
            log(u'Loaded Texture : %s' % texFileMain, force=True)
            # texList[texFileMain].setWrapU(Texture.WM_clamp)

        texMain = texList[texFileMain]
        if texMain and texMain.hasRamImage():
          if mat.edge_flag:
            # 輪郭有效
            texMain.setBorderColor(VBase4(mat.diffuse_color.r, mat.diffuse_color.g, mat.diffuse_color.b, 1))
          pass

          ts_main = TextureStage('%s_%3d_main' % (matName, matIndex))
          ts_main.setColor(VBase4(mat.ambient_color.r, mat.ambient_color.g, mat.ambient_color.b, 1))
          ts_main.setSort(matIndex)
          ts_main.setPriority(matIndex)

          if not texFileSphere:
            ts_main.setMode(TextureStage.MReplace)
          else:
            # ts_main.setMode(TextureStage.MModulate)
            # ts_main.setMode(TextureStage.MModulateGloss)
            ts_main.setMode(TextureStage.MModulateGlow)

          if hasAlpha(texMain):
            nodePath.setTransparency(TransparencyAttrib.MDual, matIndex)

            texImage = texMain.getRamImageAs('RGB')
            pixel_LT = texImage.getData()[0:3]
            if pixel_LT[0] == chr(0xff) and pixel_LT[1] == chr(0xff) and pixel_LT[2] == chr(0xff):
              print('--> Left-Top Pixel is WHITE')
              nodePath.setTransparency(TransparencyAttrib.MAlpha, matIndex)
            elif pixel_LT[0] == chr(0x00) and pixel_LT[1] == chr(0x00) and pixel_LT[2] == chr(0x00):
              print('--> Left-Top Pixel is BLACK')
              nodePath.setTransparency(TransparencyAttrib.MAlpha, matIndex)

          else:
            nodePath.setTransparency(TransparencyAttrib.MMultisample, matIndex)

          nodePath.setTexture(ts_main, texMain)
          nodePath.setTexScale(ts_main, 1, -1, -1)

      if texFileSphere:
        texMode = TextureStage.MReplace
        ext = os.path.splitext(texFileSphere)[1]
        if ext.lower() in ['.spa']:
          texMode = TextureStage.MAdd
        elif ext.lower() in ['.sph']:
          # texMode = TextureStage.MGlow
          # texMode = TextureStage.MModulateGlow
          texMode = TextureStage.MModulate
          # texMode = TextureStage.MBlend

        # texMode = TextureStage.MBlend
        if not texFileSphere in texList:
          texList[texFileSphere] = loadTexture(os.path.join(modelPath, texFileSphere))

        texSphere = texList[texFileSphere]
        if texSphere and texSphere.hasRamImage():
          log(u'Loaded Texture : %s' % texFileSphere, force=True)
          # texSphere.setWrapU(Texture.WM_clamp)
          # texSphere.setWrapV(Texture.WM_clamp)

          ts_sphere = TextureStage('%s_%03d_sphere' % (matName, matIndex))
          ts_sphere.setMode(texMode)
          ts_sphere.setSort(matIndex)
          ts_sphere.setPriority(matIndex)

          nodePath.setTexGen(ts_sphere, TexGenAttrib.MEyeSphereMap, 2)
          nodePath.setTexture(ts_sphere, texSphere, 1)
          nodePath.setTexScale(ts_sphere, 1, -1, -1)

          if not texFileMain:
            if hasAlpha(texSphere):
              nodePath.setTransparency(TransparencyAttrib.MDual, matIndex)

    if mat.toon_index>=0 and textures[mat.toon_index] and textures[mat.toon_index].hasRamImage():
      # texMode = TextureStage.MModulateGlow
      # texMode = TextureStage.MModulateGloss
      texMode = TextureStage.MModulate
      texToon = textures[mat.toon_index]
      # texToon.setMagfilter(Texture.FTNearestMipmapNearest)
      # texToon.setMinfilter(Texture.FTNearestMipmapNearest)
      # texToon.setAnisotropicDegree(30)
      # texToon.setWrapU(Texture.WM_clamp)

      ts_toon = TextureStage('%s_%03d_toon' % (matName, matIndex))
      # ts_toon.setColor(VBase4(1,1,1,.67))
      ts_toon.setMode(texMode)
      ts_toon.setSort(matIndex)
      ts_toon.setPriority(matIndex)

      nodePath.setTexGen(ts_toon, TexGenAttrib.MEyeSphereMap, 2)
      nodePath.setTexture(ts_toon, texToon, 1)
      nodePath.setTexScale(ts_toon, 1, -1, -1)

    # print(nodePath.getTransparency())
    nodePath.setAntialias(AntialiasAttrib.MAuto)
    if nodePath.getTransparency() == TransparencyAttrib.MNone:
      nodePath.setTwoSided(True)
    if mat.edge_flag:
      nodePath.setTwoSided(True)
    if mat.alpha < 1:
      nodePath.setTwoSided(True)

    vIndex += mat.vertex_count
    modelBody.addChild(node)
    matIndex += 1
    log(u'Loaded Node : %s' % matName, force=True)

  modelPath = NodePath(model)
  # modelPath.setShaderAuto()
  return(modelPath)
  pass

def loadPmdBone(pmd_model):
  def GetParentNode(root, parent_index):
    node = None
    if parent_index == -1:
      node = root
      pass
    else:
      for child in root.getChildren():
        node = GetParentNode(child, parent_index)
        if node:
          break
        else:
          boneIndex = child.getPythonTag('boneIndex')
          if boneIndex == parent_index:
            node = child
            break
        pass
    return(node)
    pass

  #
  # Load Bone outline for display
  #
  data = EggData()
  data.read('stages/bone.egg')
  # data.read('stages/bone_oct.egg')
  # data.read('stages/bone_cone.egg')
  dnp = NodePath(loadEggData(data))
  dnp.setColor(LVector4f(1,1,0,1))
  boneOutline = dnp.node().getChild(0)
  min_point = LPoint3f()
  max_point = LPoint3f()
  dnp.calcTightBounds(min_point, max_point)
  bone_size = LPoint3f(max_point.x-min_point.x, max_point.y-min_point.y, max_point.z-min_point.z)

  #
  # Load Bone data
  #
  formatArray = GeomVertexArrayFormat()
  formatArray.addColumn(InternalName.make(str("vindex")), 1, Geom.NTUint32, Geom.CIndex)
  formatArray.addColumn(InternalName.make(str("tindex")), 1, Geom.NTFloat32, Geom.COther)
  formatArray.addColumn(InternalName.make(str("pindex")), 1, Geom.NTFloat32, Geom.COther)

  format = GeomVertexFormat(GeomVertexFormat.getV3c4())
  format.addArray(formatArray)
  format = GeomVertexFormat.registerFormat(format)

  boneNode = PandaNode('Bones')
  boneIndex = 0
  for bone in pmd_model.bones:
    boneName = bone.name.decode('shift_jis', errors='replace')
    log(u'Loading Bone : %s' % boneName, force=True)

    #
    # load vertices(vertex list)
    #
    vdata = GeomVertexData(boneName+'_vdata', format, Geom.UHDynamic)
    vdata.setNumRows(3)

    vertex = GeomVertexWriter(vdata, 'vertex')
    color = GeomVertexWriter(vdata, 'color')
    vindex = GeomVertexWriter(vdata, 'vindex')
    tindex = GeomVertexWriter(vdata, 'tindex')
    pindex = GeomVertexWriter(vdata, 'pindex')

    node = GeomNode(boneName)

    tu = LVector3f(bone.tail.x, bone.tail.y, bone.tail.z)
    log(tu.cross(LVector3f(bone.pos.x, bone.pos.y, bone.pos.z)))

    if bone.tail_index >= 0:
      t = V2V(pmd_model.bones[bone.tail_index].pos)
    else:
      t = V2V(bone.pos+bone.tail)
    vertex.addData3f(t)
    color.addData4f(.95, .95, 0, 1) # Yellow
    vindex.addData1i(boneIndex)
    tindex.addData1i(bone.tail_index)
    pindex.addData1i(bone.parent_index)

    v = V2V(bone.pos)
    vertex.addData3f(v)
    color.addData4f(0, .95, 0.95, 1) # Cyan
    vindex.addData1i(boneIndex)
    tindex.addData1i(bone.tail_index)
    pindex.addData1i(bone.parent_index)

    geom = Geom(vdata)

    prim = GeomLines(Geom.UHDynamic)
    prim.addVertex(0)
    prim.addVertex(1)
    geom.addPrimitive(prim)

    node.addGeom(geom)

    node.setPythonTag('english_name', bone.english_name)
    node.setPythonTag('position', V2V(bone.pos))
    node.setPythonTag('parent_index', bone.parent_index)
    node.setPythonTag('tail_index', bone.tail_index)
    node.setPythonTag('tail_position', V2V(bone.tail))
    # if bone.ik:
    #   iklink = map(lambda ik: {
    #     'bone_index':ik.bone_index,
    #     'limit_angle':ik.limit_angle,
    #     'limit_max':LVector3f(V2V(ik.limit_max)),
    #     'limit_min':LVector3f(V2V(ik.limit_min))
    #     }, bone.ik.link)

    #   node.setPythonTag('ik.limit_radian', bone.ik.limit_radian)
    #   node.setPythonTag('ik.loop', bone.ik.loop)
    #   node.setPythonTag('ik.target_index', bone.ik.target_index)
    #   node.setPythonTag('ik.link', bone.ik.link)
    # else:
    #   node.setPythonTag('ik', None)

    node.setPythonTag('index', bone.index)
    node.setPythonTag('boneIndex', boneIndex)
    node.setPythonTag('pickableObjTag', 1)

    vd = vdist(v, t)
    scale = vd / bone_size.z
    s_x = scale if scale<.25 else .25
    s_y = scale if scale<.25 else .25
    s_z = scale #if scale<.25 else .25
    s = LVector3f(s_x, s_y, s_z)

    r = getHprFromTo(v, t)
    trans = TransformState.makePosHprScale(v, r, s)
    bo = boneOutline.makeCopy()
    bo.setName(boneName)
    bo.setTransform(trans)
    bo.setPythonTag('pickableObjTag', 1)

    parentNode = GetParentNode(boneNode, bone.parent_index)
    if isinstance(parentNode, PandaNode):
      # print('PNode: ', parentNode)
      parentNode.addChild(node)
      parentNode.addChild(bo)
    elif isinstance(parentNode, GeomNode):
      # print('GNode: ', parentNode)
      parentNode.addGeom(node)
      # parentNode.addGeom(bo)
    boneIndex += 1

  np = NodePath(boneNode)
  np.setRenderModeWireframe()
  # np.setPythonTag('pickableObjTag', 1)
  # ofs = OFileStream('bonelist.txt', 3)
  # np.ls(ofs, 2)
  np.hide()
  return(np)

def loadPmdMorph(pmd_model):
  #
  # Load Morph data
  #
  formatArray = GeomVertexArrayFormat()
  formatArray.addColumn(InternalName.make(str("vindex")), 1, Geom.NTUint32, Geom.CIndex)
  # formatArray.addColumn(InternalName.make(str("v.morph")), 3, Geom.NTFloat32, Geom.CMorphDelta)
  formatArray.addColumn(InternalName.make(str("vmorph")), 3, Geom.NTFloat32, Geom.COther)
  formatArray.addColumn(InternalName.make(str("transform_index")), 1, Geom.NTUint32, Geom.CIndex)
  formatArray.addColumn(InternalName.make(str("transform_weight")), 3, Geom.NTUint32, Geom.COther)
  formatArray.addColumn(InternalName.make(str("emotion.morph.strange")), 1, Geom.NTFloat32, Geom.COther)

  format = GeomVertexFormat(GeomVertexFormat.getV3())
  format.addArray(formatArray)
  format = GeomVertexFormat.registerFormat(format)

  morphNode = PandaNode('Morphs')
  morphIndex = 0
  morphBase = None
  for morph in pmd_model.morphs:
    morphName = morph.name.decode('shift_jis', errors='replace')
    log(u'Loading Morph : %s' % morphName, force=True)

    if morphIndex==0 and morphName == 'base':
      morphBase = morph
      morphIndex += 1
      continue

    #
    # load vertices(vertex list)
    #
    vdata = GeomVertexData(morphName+'_vdata', format, Geom.UHDynamic)
    # vdata.setNumRows(6)

    vertex = GeomVertexWriter(vdata, 'vertex')
    vindex = GeomVertexWriter(vdata, 'vindex')
    # vmorph = GeomVertexWriter(vdata, 'v.morph')
    vmorph = GeomVertexWriter(vdata, 'vmorph')
    transform_index = GeomVertexWriter(vdata, 'transform_index')
    transform_weight = GeomVertexWriter(vdata, 'transform_weight')
    column_morph_slider = GeomVertexWriter(vdata, 'emotion.morph.strange')

    node = GeomNode(morphName)

    morphData = None
    morphID = encode(morphName)
    morphEggText = []
    morphEggText.append('<CoordinateSystem> { Z-up }')
    morphEggText.append('<Group> %s_ACTOR {' % morphID)
    morphEggText.append('  <DART> { 1 }')
    morphEggText.append('  <Group> %s {' % morphID)
    morphEggText.append('    <VertexPool> %s {' % morphID)

    prim = GeomPoints(Geom.UHDynamic)
    vdata.setNumRows(len(morph.pos_list))
    for idx in xrange(len(morph.pos_list)):
      i = morphBase.indices[morph.indices[idx]]
      v = V2V(pmd_model.vertices[i].pos)
      o = V2V(morph.pos_list[idx])

      vertex.addData3f(v)
      vindex.addData1i(i)
      vmorph.addData3f(o)
      transform_index.addData1i(i)
      transform_weight.addData3f(o)
      column_morph_slider.addData1f(1.0)
      prim.addVertex(idx)

      morphEggText.append('      <Vertex> %d {' % idx)
      morphEggText.append('        %.11f %.11f %.11f' % (v.x, v.y, v.z))
      morphEggText.append('        <Dxyz> Wedge { %.6f %.6f %.6f }' % (o.x, o.y, o.z))
      morphEggText.append('      }')

    morphEggText.append('    }')
    morphEggText.append('  }')
    morphEggText.append('}')

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node.addGeom(geom)

    egg = EggData()
    egg.read(StringStream('\n'.join(morphEggText)))
    action = loadEggData(egg).getChild(0)
    node.addChild(action)

    node.setPythonTag('english_name', morph.english_name)
    node.setPythonTag('morph_type', 1)
    node.setPythonTag('morph_data', morphData)
    node.setPythonTag('morph_index', morphIndex)
    node.setPythonTag('pickableObjTag', 1)
    morphNode.addChild(node)

    morphIndex += 1

  np = NodePath(morphNode)
  np.hide()
  return(np)
  pass

def loadPmdSlot(pmd_model):
  #
  # Load Display Slot data
  #
  slotNode = PandaNode('Slots')
  slotIndex = 0
  for slot in pmd_model.display_slots:
    slotName = slot.name.decode('shift_jis', errors='replace')
    log(u'Loading Slot : %s' % slotName, force=True)
    node = PandaNode(slotName)
    node.setPythonTag('english_name', slot.english_name)
    node.setPythonTag('references', slot.references)
    node.setPythonTag('special_flag', slot.special_flag)
    node.setPythonTag('slotIndex', slotIndex)
    node.setPythonTag('pickableObjTag', 1)

    slotNode.addChild(node)
    slotIndex += 1
  np = NodePath(slotNode)
  np.hide()
  return(np)

def loadPmdRigid(pmd_model):
  #
  # Load Rigid data
  #
  rigidNode = PandaNode('Rigid')
  rigidIndex = 0
  for rigid in pmd_model.rigidbodies:
    rigidName = rigid.name.decode('shift_jis', errors='replace')
    log(u'Loading RigidBodies : %s' % rigidName, force=True)
    node = PandaNode(rigidName)
    node.setPythonTag('english_name', rigid.english_name)
    node.setPythonTag('bone_index', rigid.bone_index)
    node.setPythonTag('collision_group', rigid.collision_group)
    node.setPythonTag('no_collision_group', rigid.no_collision_group)
    node.setPythonTag('shape_type', rigid.shape_type)
    node.setPythonTag('shape_size', V2V(rigid.shape_size))
    node.setPythonTag('shape_position', V2V(rigid.shape_position))
    node.setPythonTag('shape_rotation', R2DV(rigid.shape_rotation))
    node.setPythonTag('param.mass', rigid.param.mass)
    node.setPythonTag('param.linear_damping', rigid.param.linear_damping)
    node.setPythonTag('param.angular_damping', rigid.param.angular_damping)
    node.setPythonTag('param.restitution', rigid.param.restitution)
    node.setPythonTag('param.friction', rigid.param.friction)
    node.setPythonTag('mode', rigid.mode)
    node.setPythonTag('rigidIndex', rigidIndex)
    node.setPythonTag('pickableObjTag', 1)

    rigidNode.addChild(node)
    rigidIndex += 1
  np = NodePath(rigidNode)
  np.hide()
  return(np)

def loadPmdJoint(pmd_model):
  #
  # Load Joints data
  #
  jointNode = PandaNode('Joints')
  jointIndex = 0
  for joint in pmd_model.joints:
    jointName = joint.name.decode('shift_jis', errors='replace')
    log(u'Loading RigidBodies : %s' % jointName, force=True)
    node = PandaNode(jointName)
    node.setPythonTag('english_name', joint.english_name)
    node.setPythonTag('joint_type', joint.joint_type)
    node.setPythonTag('rigidbody_index_a', joint.rigidbody_index_a)
    node.setPythonTag('rigidbody_index_b', joint.rigidbody_index_b)
    node.setPythonTag('position', V2V(joint.position))
    node.setPythonTag('rotation', R2DV(joint.rotation))
    node.setPythonTag('translation_limit_min', V2V(joint.translation_limit_min))
    node.setPythonTag('translation_limit_max', V2V(joint.translation_limit_max))
    node.setPythonTag('rotation_limit_min', R2DV(joint.rotation_limit_min))
    node.setPythonTag('rotation_limit_max', R2DV(joint.rotation_limit_max))
    node.setPythonTag('spring_constant_translation', V2V(joint.spring_constant_translation))
    node.setPythonTag('spring_constant_rotation', R2DV(joint.spring_constant_rotation))
    node.setPythonTag('jointIndex', jointIndex)
    node.setPythonTag('pickableObjTag', 1)

    jointNode.addChild(node)
    jointIndex += 1
  np = NodePath(jointNode)
  np.hide()
  return(np)

def displayPmdModelInfo(model):
  # print(dir(model))
  info = pmdInfo(model)
  fn = os.path.splitext(os.path.basename(model.path))
  log(os.path.join(CWD, fn[0]+'_info'+'.txt'))
  with codecs.open(os.path.join(CWD, fn[0]+'_info'+'.txt'), 'w', encoding='utf8') as f:
    f.writelines(os.linesep.join(info))
  pass

def testPMD(pmd):
  pmdModel = pmdLoad(pmd)
  if pmdModel:
    print(pmdModel.path)
    displayPmdModelInfo(pmdModel)

    from direct.showbase.ShowBase import ShowBase
    base = ShowBase()

    p3dnode = pmd2p3d(pmdModel)
    p3dnode.reparentTo(base.render)

    base.run()
    pass
  pass

def loadPmdModel(modelfile):
  p3dnode = None
  try:
    mmdFile = os.path.relpath(modelfile)
  except:
    mmdFile = modelfile
    pass

  if os.path.altsep:
    mmdFile = mmdFile.replace('\\', os.path.altsep)
  ext = os.path.splitext(mmdFile)[1].lower()
  if ext in ['.pmd']:
    mmdModel = pmdLoad(mmdFile)
    if mmdModel:
      p3dnode = loadPmdBody(mmdModel)
      morphs = loadPmdMorph(mmdModel)
      if morphs:
        morphs.reparentTo(p3dnode)
      bones = loadPmdBone(mmdModel)
      if bones:
        bones.reparentTo(p3dnode)
      # slots = loadPmdSlot(mmdModel)
      # if slots:
      #   slots.reparentTo(p3dnode)
  elif ext in ['.pmx']:
    mmdModel = pmxLoad(mmdFile)
    if mmdModel:
      p3dnode = pmx2p3d(mmdModel)
      morphs = loadPmxMorph(mmdModel)
      morphs.reparentTo(p3dnode)
  elif ext in ['', '.egg', '.pz', '.bam']:
    return(p3dnode)
  return(p3dnode)

if __name__ == '__main__':
  pmxFile = u'../models/meiko/meiko.pmx'
  pmxFile = u'../models/apimiku/Miku long hair.pmx'
  # pmxFile = u'../models/cupidmiku/Cupid Miku.pmx'

  pmxFile = u'../models/alice/alice.pmd'

  if len(sys.argv) > 1:
    if len(sys.argv[1]) > 0:
      pmxFile = sys.argv[1]

  ext = os.path.splitext(pmxFile)[1].lower()
  if ext in ['.pmd']:
    testPMD(pmxFile)
  elif ext in ['.pmx']:
    testPMX(pmxFile)

  # testPMD(pmdFile)


import logging
from logging.handlers import RotatingFileHandler
import pickle
from datetime import datetime
from pathlib import Path
from collections import OrderedDict
from typing import List, Union

from automators.result import Result
from automators.utils.logger import Logging as linkajaLogging

TIME_INDICATOR_FORMAT = '%b_%Y'

def loggerHandler(filename='./logs.log', maxMB: float = 10, backupCount=5): # will be renamed to setupLogger
    logging.getLogger('urllib3').setLevel(logging.INFO)
    logging.getLogger('uiautomator2').setLevel(logging.INFO)
    linkajaLogging.enable()
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    handler = RotatingFileHandler(filename=filename, encoding='utf-8', mode='a', maxBytes=int(maxMB*(1024*1024)), backupCount=backupCount)
    FMT = logging.Formatter(fmt='[%(asctime)s,%(msecs)d %(levelname)s %(threadName)s:%(name)s:%(filename)s:%(lineno)d] %(message)s',
                            datefmt='%d-%m-%Y:%H:%M:%S')
    handler.setFormatter(FMT)
    logger.addHandler(handler)

def addPickle(result: Union[Result, List[Result]], filename: str = 'Hist.pick', monthlyHistory=True, monthlyHistoryFilenameFormat='{name}_{time_indicator}.{ext}'):
    '''
    Accepts a result object or a list of result objects and dumps it into given filename or monthlyHistory formatted filename(s).
    '''
    resultLists = {}
    name,ext = '.'.join(filename.split('.')[:-1]), filename.split('.')[-1]

    if result is None:
        return

    if isinstance(result, Result):
        results = [result]
    elif isinstance(result, list):
        isAllResults = all([isinstance(obj, Result) for obj in result])
        if not isAllResults:
            return print("Given list has element(s) other than Result object.")
        results = result
    else:
        return print("Given object of class <{}> is not valid.".format(result.__class__.__name__))

    if monthlyHistory:
        for result in results:
            time_indicator = result.time.strftime(TIME_INDICATOR_FORMAT)
            filename = monthlyHistoryFilenameFormat.format(name=name, ext=ext, time_indicator=time_indicator)
            resultLists[filename] = resultLists.get(filename, []) + [result]

        for filename, resultList in resultLists.items():
            data = []
            try:
                with open(filename, 'rb') as f:
                    data.extend(pickle.load(f))
            except (FileNotFoundError):
                print('Creating pickle file {}...'.format(filename))

            data.extend(resultList)

            with open(filename, 'wb') as f:
                pickle.dump(data, f)
    else:
        data = []
        try:
            with open(filename, 'rb') as f:
                data.extend(pickle.load(f))
        except (FileNotFoundError):
            print('Creating pickle file {}...'.format(filename))

        data.extend(results)

        with open(filename, 'wb') as f:
            pickle.dump(data, f)

def addPickles(*args, **kwargs):
    '''
    Extension for <addPickle> function for multiple results to avoid confusion.
    '''
    return addPickle(*args, **kwargs)

def dumpObject(obj, filename: str = 'dump.pick'):
    with open(filename, 'wb') as f:
        pickle.dump(obj, f)

def readPickle(filename: str = 'Hist.pick', getdata=False, monthlyHistory=True, FilenameSearcherFormatter='{name}_{time_indicator}.{ext}'):
    filesData = {}
    name,ext = '.'.join(filename.split('.')[:-1]), filename.split('.')[-1]
    FilenameSearcher = FilenameSearcherFormatter.format(name=name, ext=ext, time_indicator='*')
    if monthlyHistory:
        files = [name.__str__() for name in list(Path().glob(FilenameSearcher))]
    else:
        files = [filename]

    if len(files) <= 0:
        print('Pickle file not found.')
        return

    try:
        for filename in files:
            with open(filename, 'rb') as f:
                filesData[filename] = pickle.load(f)
    except FileNotFoundError:
        print('Pickle file not found.')
        return

    if monthlyHistory:
        filesData = OrderedDict({k:v for k,v in sorted(filesData.items(), key=lambda x: datetime.strptime(x[0], FilenameSearcherFormatter.format(name=name, ext=ext, time_indicator=TIME_INDICATOR_FORMAT)))})

    if getdata:
        return filesData

    for fileindex, (pfile, data) in enumerate(filesData.items()):
        head = "##{} File:{}".format(fileindex, pfile)
        transactions = '\n'.join(["Transaction#{}\n{}".format(i, res.detailed()) for i, res in enumerate(data)])
        footer = "\n\n\n"
        print('\n'.join([head,transactions,footer]))


# You are given two different cells of the chessboard.
# Write a program that determines whether the bishop can get from the first cell to the second in one move.
# The program receives as input four numbers from 1 to 8 each, specifying the column number and line number,
# first for the first cell, then for the second cell.
# The program should output "YES" if the bishop moves from the first cell to the second one, or "NO" otherwise.
#
# Input data format
# The input to the program is four numbers from 1 to 8.
#
# Output data format
# The program should display the text in accordance with the condition of the problem.
#
# Note. The chess bishop moves along the diagonals.
a1 = int(input())
b1 = int(input())
a2 = int(input())
b2 = int(input())
if (a1 - b1) == (a2 - b2) or (a1 + b1) == (a2 + b2):
    print('YES')
else:
    print('NO')


import random
import time

from opentelemetry.metrics import get_meter_provider, set_meter_provider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
    ConsoleMetricExporter,
    PeriodicExportingMetricReader,
)
from opentelemetry.sdk.metrics.view import SumAggregation, View

# Create a view matching the histogram instrument name `http.client.request.latency`
# and configure the `SumAggregation` for the result metrics stream
hist_to_sum_view = View(
    instrument_name="http.client.request.latency", aggregation=SumAggregation()
)

# Use console exporter for the example
exporter = ConsoleMetricExporter()

# Create a metric reader with stdout exporter
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
provider = MeterProvider(
    metric_readers=[
        reader,
    ],
    views=[
        hist_to_sum_view,
    ],
)
set_meter_provider(provider)

meter = get_meter_provider().get_meter("view-change-aggregation", "0.1.2")

histogram = meter.create_histogram("http.client.request.latency")

while 1:
    histogram.record(99.9)
    time.sleep(random.random())


import html
import random
import requests

from twitch.client import TwitchClient
from .models import Tag, Quote

def store_quote(author: str, text: str, tag_text: str):
    tag, _ = Tag.objects.get_or_create(text=tag_text)
    return Quote.objects.create(text=text, tag=tag, author=author)

def get_random_quote_by_tag(message_text: str) -> str:
    tag = Tag.objects.filter(text__in=message_text.split()).first()
    if tag:
        quote = random.choice(tag.quotes.all())
        return format_quote(quote=quote)
    else:
        raise Tag.DoesNotExist

def get_weather(city):
    response = requests.get('http://wttr.in/{0}?format=4'.format(city))
    return response.text

def get_random_quote():
    quote = random.choice(Quote.objects.all())
    return format_quote(quote=quote)

def format_quote(quote: Quote) -> str:
    return "{}\n{}\n{}".format(quote.text, quote.author or 'Unknown', quote.created_at.strftime("%Y-%m-%dT%H-%M") or '')

def get_keyword_quote_count(keyword: str) -> str:
    count = Quote.objects.filter(tag__text=keyword).count()
    return "'{}' - {} quotes.".format(keyword, count)

def format_stream(idx, stream):
    return (
        f'- {idx} - \n'
        f'{html.escape(stream["title"])}\n'
        f'\n'
        f'streamer: {html.escape(stream["user_name"])}\n'
        f'\n'
        f'viewers: {stream["viewer_count"]}\n'
        f'Stream link\n'
        f'\n'
        f'\n'
    )

def get_stream_list_by_game(game_name: str):
    client = TwitchClient()

    streams = client.streams_by_game(game_name=game_name)
    raw_streams = streams['data']

    # limit to 5 for now
    if len(raw_streams) >= 5:
        raw_streams = raw_streams[:5]

    parsed_streams = []
    for idx, stream in enumerate(raw_streams):
        parsed_streams.append(format_stream(idx, stream))

    return ' '.join(parsed_streams)


import pandas as pd
import xarray as xr
from .util import max_date

def cases_covidtracking():
    url = "http://covidtracking.com/api/us/daily.csv"

    us_data = (
        pd.read_csv(url, parse_dates=[0], index_col=[0])
        .rename(
            columns={
                "positive": "cases",
                "death": "deaths",
                "totalTestResults": "tests",
            }
        )
        .sort_index()
    )

    data = xr.Dataset.from_dataframe(us_data[["cases", "deaths", "tests"]]).expand_dims(
        {"location": ["United States"]}
    )
    data.attrs["date"] = max_date(data)
    data.attrs["source"] = "COVID Tracking Project"
    data.attrs["source_url"] = url

    return data


import csv

from datetime import datetime

from django.core.management.base import BaseCommand
from django.utils import timezone

from projects.models import Project
from categories.models import Category, Subcategory
from locations.models import Location
from creators.models import Creator

def convert_timestamp_to_datetime(timestamp: str):
    return datetime.fromtimestamp(int(timestamp), tz=timezone.utc)

class Command(BaseCommand):
    help = "Populate Database"

    def add_arguments(self, parser):
        parser.add_argument("projects_path", help="Projects File Path")

    def handle(self, *args, **kwargs):
        project_path = kwargs["projects_path"]

        with open(project_path) as file:
            project_data = csv.DictReader(file, delimiter=",")

            current_row_count = 0
            total_row_count = sum(1 for row in file)

            file.seek(0)

            print("Seed Started...")

            for row in project_data:

                category = Category.objects.get_or_create(
                    name=row["category_name"],
                )[0]

                subcategory = Subcategory.objects.get_or_create(
                    name=row["subcategory"],
                    category=category,
                )[0]

                location = Location.objects.get_or_create(
                    name=row["location_name"],
                    localized_name=row["location_localized_name"],
                    country_full=row["location_expanded_country"],
                    country=row["location_country"],
                    state=row["location_state"],
                )[0]

                creator = Creator.objects.get_or_create(
                    name=row["creator_name"], slug=row["creator_slug"]
                )[0]

                Project.objects.get_or_create(
                    name=row["name"],
                    description=row["blurb"],
                    backers=row["backers_count"],
                    created_at=convert_timestamp_to_datetime(row["created_at"]),
                    deadline=convert_timestamp_to_datetime(row["deadline"]),
                    goal=float(row["goal"].replace(",", ".")),
                    pledged=float(row["pledged"].replace(",", ".")),
                    usd_pledged=float(row["usd_pledged"].replace(",", ".")),
                    currency=row["currency"],
                    launched_at=convert_timestamp_to_datetime(row["launched_at"]),
                    state=row["state"],
                    state_changed_at=convert_timestamp_to_datetime(
                        row["state_changed_at"],
                    ),
                    subcategory=subcategory,
                    location=location,
                    creator=creator,
                )

                current_row_count += 1

                if current_row_count % (total_row_count // 100) == 0:
                    row_percentage = round((current_row_count / total_row_count) * 100, 2)
                    self.stdout.write(f"\tProcessed { row_percentage }% of {total_row_count} lines")


#Usage: python3 strict_kegg_parser.py -i path/to/kofamsccan/output -l Nitrogen_list -k kegg_control_file

import os
import sys
from csv import DictWriter
import argparse
import pandas as pd

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input_dir", help="Full file path to kegg input files")
parser.add_argument("-l", "--kegg_list", help="List of KO numbers to search for")
parser.add_argument("-k", "--pathways", help="KEGG control file")

args = parser.parse_args()

print( "Input_directory {}\n KEGG_list {}\n KEGG_pathway_file {} ".format(
        args.input_dir,
        args.kegg_list,
        args.pathways,
        ))

#Read in kofamscan output from detailed run
kegg_files = [file for file in os.listdir(args.input_dir) if file.endswith("kegg_output")]

for kegg_file in kegg_files:
   output_file = kegg_file+"_reliable"
   output = open(args.input_dir+"/"+output_file,'a')
   for line in open(args.input_dir+"/"+kegg_file):
      if line.startswith('*'):
         output.write(line)
   output.close()

reliable_kegg_file = [file for file in os.listdir(args.input_dir) if file.endswith("_reliable")] #Read in newly created file with only reliable annotations
#Replace whitespaces with a tab
for rel_file in reliable_kegg_file:
   filepath_in = args.input_dir+"/"+rel_file
   filepath_out = args.input_dir+"/"+rel_file+"_tabbed"
   with open(filepath_in, 'r') as file_in, open(filepath_out, 'w') as file_out:
       for line in file_in:
           data = line.split()  # splits the content removing spaces and final newline
           line_w_tabs = "\t".join(data) + '\n'
           file_out.write(line_w_tabs)

#Read in and parse the tabbed file
tabbed_kegg_files = [file for file in os.listdir(args.input_dir) if file.endswith("_tabbed")]
for tab_file in tabbed_kegg_files:
   kegg_df = pd.read_csv(args.input_dir+"/"+tab_file,sep = '\t',usecols=[1,2,4],names=["Gene","KO","Score"]) #Read in tabbed file into pandas dataframe
   kegg_df_dups_removed = kegg_df.groupby(["Gene","KO"], as_index=False, sort=False)["Score"].max() #Keep annotation with highest score
   kegg_subset_df = kegg_df_dups_removed[["Gene","KO"]] #Convert to mapper output format
   final_file = args.input_dir+"/"+tab_file+"_final" #Generate empty output file
   kegg_subset_df.to_csv(final_file, sep='\t', index=False) #Write subsetted df to file

#Parse and summarize KO counts from all bins

#Convert search file to list
query_list = []
with open(os.path.join(args.input_dir, args.kegg_list)) as query_input:
        for line in query_input:
                query_list.append(line.strip("\n"))

#create list of kegg files to parse through
kegg_files = [file for file in os.listdir(args.input_dir) if file.endswith("final")]
ko_list = []

#Create a dictionary for each genome, with all query KOs set to a count of 0.
for kegg in kegg_files:
        ko_entries = []
        genome_dict = {"Genome": str(kegg)}
        for ko in query_list:
                genome_dict[ko] = 0
#Parse through each kofamscan_outputfile per genome
        with open(os.path.join(args.input_dir, kegg)) as input:
                for line in input:
                        if len(line.split("\t")) == 1: #if there is no KO annotation for this gene, ignore it
                                pass
                        else:
                                gene, ko = line.strip("\n").split("\t")
                                ko_entries.append(ko) #Add KO to list
        input.close()
        for ko_entry in ko_entries:
                if ko_entry in genome_dict:
                        genome_dict[ko_entry] += 1
                else:
                        pass
        ko_list.append(dict(genome_dict)) #Append new dictionary to overall list

#Write it out to a file
keys = ko_list[0].keys()
with open((os.path.join(args.input_dir,"KEGG_output_count_table.txt")),"w") as output:
        dict_writer = DictWriter(output, keys, delimiter = "\t")
        dict_writer.writeheader()
        for item in ko_list:
                dict_writer.writerow(item)
output.close()

#Read in count file
sample_KO_matrix = pd.read_csv(args.input_dir+'/KEGG_output_count_table.txt', sep="\t", header=0)
sample_KO_matrix_df =sample_KO_matrix.replace(to_replace='_kegg_output_detailed_reliable_tabbed_final', value='',regex=True)
sample_KO_matrix_df = sample_KO_matrix_df.rename(columns={'Genome': 'KO'})
sample_KO_matrix_df = sample_KO_matrix_df.set_index('KO',inplace=False)
sample_KO_matrix_df_transp = sample_KO_matrix_df.transpose()
sample_KO_matrix_df_transp = sample_KO_matrix_df_transp.reset_index(level=0, inplace=False)
sample_KO_matrix_df_transp = sample_KO_matrix_df_transp.rename(columns={'index': 'KO'})
sample_KO_matrix_df_transp.index = sample_KO_matrix_df_transp.index.rename('Index')

#Read in control file
KEGG_control = pd.read_csv(args.input_dir+'/'+args.pathways, sep="\t", header=0)
#Merge dataframes
KEGG_control_df = pd.merge(KEGG_control,sample_KO_matrix_df_transp,on='KO',how='outer')
#Write to file
KEGG_control_df.to_csv(args.input_dir+'/KO_pathway_summary.tab', sep='\t', index=False)

#Delete interim files
int_files = os.listdir(args.input_dir)
for int_file in int_files:
    if int_file.endswith(("_reliable","_tabbed","final")):
        os.remove(os.path.join(args.input_dir, int_file))


from django.urls import path

from anime.views import *

urlpatterns = [
    path('my-animes/', my_animes, name='my_animes'),
    path('register/', anime_register, name='anime_register'),
    path('update/<uuid:id>', anime_update, name='anime_update'),
    path('delete/<uuid:id>', anime_delete, name='anime_delete'),
]


import sys

def main():
    input_file_path = "folder/input.txt"
    output_file_path = "folder/output.txt"
    if len(sys.argv) > 1:
        # Open files
        input_file = open(input_file_path, mode='r')
        output_file = open(output_file_path, mode='a')
        txt_found_in_file = False
        # Input Args
        input_txt = sys.argv[1]
        # Read lines
        for line in input_file:
            if input_txt in line:
                txt_found_in_file = True
        # If input is not in input file
        if not txt_found_in_file:
            # Write line
            output_file.writelines(f"{input_txt}\n")

        # Close files
        input_file.close()
        output_file.close()

if __name__ == "__main__":
    main()


from web3 import Web3
from web3.middleware import geth_poa_middleware
from hexbytes import HexBytes
import arbitrage_config
import math

def getTXNResult(w3,tx):
    # replay the transaction locally:
    try:
        ret = w3.eth.call(tx)
        return (True,ret)
    except Exception as e:
        return (False,str(e))
def wei_to_eth(w):
    return w/(10**18)
def tcdec_to_tc(tc, tc_decimals):
    return tc/(10**tc_decimals)

def trade():
    # Connect to blockchain
    if(arbitrage_config.config['connection_is_ipc']):
        w3 = Web3(Web3.IPCProvider(arbitrage_config.config['connection_uri']))
    else:
        w3 = Web3(Web3.WebsocketProvider('wss://andromeda.cs.virginia.edu/geth'))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)
    # Debug: Test connection
    # w3.is_connected()
    # ----------------------------------------------------------------------------
    # Find my current holdings (h_now = q_e * p_e + q_t * p_t)
    p_e = arbitrage_config.config['price_eth']
    p_t = arbitrage_config.config['price_tc']
    q_e = wei_to_eth(w3.eth.get_balance(arbitrage_config.config['account_address']))
    tc_address = arbitrage_config.config['tokencc_addr']
    tc_abi = arbitrage_config.itokencc_abi
    tc = w3.eth.contract(address=tc_address, abi=tc_abi)
    tc_decimals = tc.functions.decimals().call()
    q_t = tcdec_to_tc(tc.functions.balanceOf(arbitrage_config.config['account_address']).call(), tc_decimals)
    h_now = q_e * p_e + q_t * p_t
    f = arbitrage_config.config['dex_fees']
    gas_price = arbitrage_config.config['gas_price']
    # ----------------------------------------------------------------------------
    # For each DEX (only single transactions), find best eth and token trades (delta_t and delta_e)
    #                                           Calculate holdings for each of these trades
    max_profit = 0
    max_proposed_txn = ''
    params = [0.0,0.0,0.0,h_now]
    dex_abi = arbitrage_config.idex_abi
    for dex_address in arbitrage_config.config['dex_addrs']:
        dex = w3.eth.contract(address=dex_address, abi=dex_abi)
        x_d = wei_to_eth(dex.functions.x().call())
        y_d = tcdec_to_tc(dex.functions.y().call(), tc_decimals)
        k_d = dex.functions.k().call() / 10**(tc_decimals + 18)
        # Best trade in of token?
        delta_t = -1*y_d + math.sqrt((1-f) * k_d * (p_e/p_t)) # in tokens (no decimals)
        delta_t = min(q_t, delta_t) # Cannot trade more tokens than I have...
        delta_t = min(arbitrage_config.config['max_tc_to_trade'], delta_t) # Cannot trade more tc than assignment max
        delta_t = max(0, delta_t)
        # Need delta_t to be w/ decimals here, could go tokens * tc.decimals()
        proposed_txn = tc.functions.transfer(dex_address, int(delta_t * 10**tc_decimals)).build_transaction({
                'gas': 1000000,
                'gasPrice': w3.to_wei(gas_price, 'gwei'),
                'from': arbitrage_config.config['account_address'],
                'nonce': w3.eth.get_transaction_count(arbitrage_config.config['account_address']),
                'chainId': 67834503
                })
        g = 50000
        #g = w3.eth.estimate_gas(proposed_txn)
        profit = (((q_e + (1 - f)*x_d - (1 - f)*k_d/(y_d + delta_t))) * p_e + (q_t - delta_t) * p_t - wei_to_eth(g) * p_e) - h_now
        if profit > max_profit:
            params[0] = (1 - f)*x_d - (1 - f)*(k_d/(y_d + delta_t)) # +Eth
            params[1] = -1 * delta_t # -Token
            params[2] = (wei_to_eth(g) + f*x_d - f*(k_d/(y_d + delta_t))) * p_e # (w/ my understanding) (Decimals?)
            params[3] = h_now + profit
            max_profit = profit
            max_proposed_txn = proposed_txn
        # best trade in of eth?
        delta_e = -1*x_d + math.sqrt((1-f) * k_d * (p_t/p_e))
        delta_e = min(q_e, delta_e) # Cannot trade more eth than I have...
        delta_e = min(arbitrage_config.config['max_eth_to_trade'], delta_e) # Cannot trade more eth than assignment max...
        delta_e = max(0, delta_e)
        proposed_txn = {
            'nonce': w3.eth.get_transaction_count(arbitrage_config.config['account_address']),
            'to': dex_address,
            'value': w3.to_wei(delta_e * 10**18, 'wei'),
            'gas': 1000000,
            'gasPrice': w3.to_wei(gas_price, 'gwei'),
            'chainId': 67834503
        }
        #g = w3.eth.estimate_gas(proposed_txn)
        profit = (q_t + (1 - f)*y_d - (1 - f)*(k_d/(x_d + delta_e))) * p_t + (q_e - delta_e) * p_e - wei_to_eth(g) * p_e - h_now
        if profit > max_profit:
            params[0] = (-1 * delta_e)# -Eth
            params[1] = (1 - f)*y_d - (1 - f)*(k_d/(x_d + delta_e))# +Token (w/ my understanding)
            params[2] = wei_to_eth(g) * p_e + (f*y_d - f*(k_d/(x_d + delta_e))) * p_t # (w/ my understanding) (Decimals?)
            params[3] = h_now + profit
            max_profit = profit
            max_proposed_txn = proposed_txn
    # If there are profitable trades (holdings_after > holdings_before), take the best one
    if max_profit > 0:
        signed_txn = w3.eth.account.sign_transaction(max_proposed_txn, private_key=arbitrage_config.config['account_private_key'])
        ret = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
    h_after = tcdec_to_tc(tc.functions.balanceOf(arbitrage_config.config['account_address']).call(), tc_decimals)*p_t +  wei_to_eth(w3.eth.get_balance(arbitrage_config.config['account_address']))*p_e
    arbitrage_config.output(params[0], params[1], params[2], h_after)
    return 0

if __name__ == '__main__':
    trade()


def ifPseudoPelindrom(string, start,end):
    while start <= end:
        if string[start]==string[end]:
            start+=1
            end-=1
        else:
            return False
    return True

def ifPelindrom(string):
    start=0
    end=len(string)-1
    while start<end:
        if string[start]==string[end]:
            start+=1
            end-=1
            continue

        else:
            check_left=ifPseudoPelindrom(string,start+1,end)
            check_right=ifPseudoPelindrom(string,start,end-1)

            if check_left or check_right:
                return 1
            else:
                return 2

    return 0

def solution():
    for string in strings:
        print(ifPelindrom(string))

if __name__ == "__main__":
    num=0
    strings=[]

    with open("input17609.txt","r") as file:
        num=int(file.readline())

        strings=[list(file.readline().strip()) for _ in range(num)]
    solution()


#! /usr/bin/env python
######################################################################
#  Written by Kevin L. Sitze on 2006-12-03
#  This code may be used pursuant to the MIT License.
######################################################################

import re

__all__ = ( 'quote', )

_bash_reserved_words = {
    'case'     : True,
    'coproc'   : True,
    'do'       : True,
    'done'     : True,
    'elif'     : True,
    'else'     : True,
    'esac'     : True,
    'fi'       : True,
    'for'      : True,
    'function' : True,
    'if'       : True,
    'in'       : True,
    'select'   : True,
    'then'     : True,
    'until'    : True,
    'while'    : True,
    'time'     : True
}

####
#  _quote_re1 escapes double-quoted special characters.
#  _quote_re2 escapes unquoted special characters.

_quote_re1 = re.compile( r"([\!\"\$\\\`])" )
_quote_re2 = re.compile( r"([\t\ \!\"\#\$\&\'\(\)\*\:\;\<\>\?\@\[\\\]\^\`\{\|\}\~])" )

def quote( *args ):
    """Combine the arguments into a single string and escape any and
    all shell special characters or (reserved) words.  The shortest
    possible string (correctly quoted suited to pass to a bash shell)
    is returned.
    """
    s = "".join( args )
    if _bash_reserved_words.has_key( s ):
        return "\\" + s
    elif s.find( '\'' ) >= 0:
        s1 = '"' + _quote_re1.sub( r"\\\1", s ) + '"'
    else:
        s1 = "'" + s + "'"
    s2 = _quote_re2.sub( r"\\\1", s )
    if len( s1 ) <= len( s2 ):
        return s1
    else:
        return s2

if __name__ == '__main__':

    import sys
    import traceback
    from types import FloatType, ComplexType

    def assertEquals( exp, got ):
        """assertEquals( exp, got )

        Two objects test as "equal" if:

        * they are the same object as tested by the 'is' operator.
        * either object is a float or complex number and the absolute
          value of the difference between the two is less than 1e-8.
        * applying the equals operator ('==') returns True.
        """
        if exp is got:
            r = True
        elif ( type( exp ) in ( FloatType, ComplexType ) or
               type( got ) in ( FloatType, ComplexType ) ):
            r = abs( exp - got ) < 1e-8
        else:
            r = ( exp == got )
        if not r:
            print >>sys.stderr, "Error: expected <%s> but got <%s>" % ( repr( exp ), repr( got ) )
            traceback.print_stack()

    for word in _bash_reserved_words:
        assertEquals( "\\" + word, quote( word ) )

    for char in ( '\t',
                  ' ', '!', '"', '#',
                  '$', '&', "'", '(',
                  ')', '*', ':', ';',
                  '<', '>', '?', '@',
                  '[', ']', '^', '`',
                  '{', '|', '}', '~' ):
        assertEquals( "\\" + char, quote( char ) )

    assertEquals( "'this is a simple path with spaces'",
                  quote( 'this is a simple path with spaces' ) )
    assertEquals( "don\\'t", quote( "don't" ) )
    assertEquals( '"don\'t do it"', quote( "don't do it" ) )


"""
Definition for a Directed graph node
class DirectedGraphNode:
    def __init__(self, x):
        self.label = x
        self.neighbors = []
"""

class Solution:
    """
    @param: graph: A list of Directed graph node
    @return: Any topological order for the given graph.
    """

    def topSort(self, graph):
        indegree = self.get_indegree(graph)

        queue = collections.deque([node for node in indegree if indegree[node] == 0])
        order = []

        while queue:
            curt_node = queue.popleft()
            order.append(curt_node)
            for neighbor in curt_node.neighbors:
                indegree[neighbor] -= 1
                if indegree[neighbor] == 0:
                    queue.append(neighbor)

        return order

    def get_indegree(self, graph):
        indegree = {node: 0 for node in graph}
        for node in graph:
            for neighbor in node.neighbors:
                indegree[neighbor] += 1

        return indegree


'''
Created on Mar 30, 2020

@author: zen
'''

#if __name__ == '__main__':
RMB = [200, 100, 20, 10, 5, 1]
NUM = 6
X = 628
count = 0
for i in range(NUM):
    use = X // RMB[i]
    count += use
    X = X - RMB[i] * use
    print('需要面额为{} 的 {} 张'.format(RMB[i],use))
    print('剩余需要支付金额{}'.format(X))

print(count)

贪心算法/贪心法找钱.py


from spack.package import install, join_path, mkdirp
from pprint import pprint

# https://spack.readthedocs.io/en/latest/build_systems/makefilepackage.html
class Oasis3Mct(MakefilePackage):
    """ACCESS-NRI's fork of https://gitlab.com/cerfacs/oasis3-mct OASIS3-MCT 2.0."""

    homepage = "https://www.access-nri.org.au"
    git = "https://github.com/ACCESS-NRI/oasis3-mct.git"

    maintainers = ["harshula"]

    version("master", branch="master")

    variant("deterministic", default=False, description="Deterministic build.")
    variant("optimisation_report", default=False, description="Generate optimisation reports.")

    depends_on("netcdf-fortran@4.5.2:")
    # Depend on virtual package "mpi".
    depends_on("mpi")

    phases = ["edit", "build", "install"]

    # TODO: Implement a determine_version(cls, exe)

    __builddir = "compile_oa3-mct"
    __incdir = join_path(__builddir, "build", "lib")
    __libdir = join_path(__builddir, "lib")
    __pkgdir = join_path(__libdir, "pkgconfig")
    __makefiledir = join_path("util", "make_dir")
    __makeinc = join_path(__makefiledir, "make.inc")
    __libs = {"mct": {}, "mpeu": {}, "psmile.MPI1": {}, "scrip": {}}

    # doc/oasis3mct_UserGuide.pdf:
    # If module mod_oasis is used in the models, it is enough to include
    # the path of the psmile objects and modules
    # ($ARCHDIR/build/lib/psmile.MPI1) for the compilation and to use the
    # psmile library $ARCHDIR/lib/libpsmile.MPI1.a when linking.
    # If module mod_prism is used in the models, it is necessary to include
    # the path of the psmile and of the mct objects and modules for the
    # compilation (i.e. $ARCHDIR/build/lib/psmile.MPI1 and /mct and to
    # use both the psmile and mct libraries $ARCHDIR/lib/libpsmile.MPI1.a
    # and libmct.a and libmpeu.a when linking.

    __libs["mct"]["incfiles"] = [
        "m_accumulatorcomms.mod",
        "m_accumulator.mod",
        "m_attrvectcomms.mod",
        "m_attrvect.mod",
        "m_attrvectreduce.mod",
        "m_chars.mod",
        "m_convertmaps.mod",
        "mct_mod.mod",
        "m_die.mod",
        "m_dropdead.mod",
        "m_errorhandler.mod",
        "m_exchangemaps.mod",
        "m_fccomms.mod",
        "m_filename.mod",
        "m_fileresolv.mod",
        "m_flow.mod",
        "m_generalgridcomms.mod",
        "m_generalgrid.mod",
        "m_globalmap.mod",
        "m_globalsegmapcomms.mod",
        "m_globalsegmap.mod",
        "m_globaltolocal.mod",
        "m_indexbin_char.mod",
        "m_indexbin_integer.mod",
        "m_indexbin_logical.mod",
        "m_inpak90.mod",
        "m_ioutil.mod",
        "m_list.mod",
        "m_mall.mod",
        "m_matattrvectmul.mod",
        "m_mctworld.mod",
        "m_merge.mod",
        "m_mergesorts.mod",
        "m_mpif90.mod",
        "m_mpif.mod",
        "m_mpout.mod",
        "m_navigator.mod",
        "m_permuter.mod",
        "m_rankmerge.mod",
        "m_realkinds.mod",
        "m_rearranger.mod",
        "m_router.mod",
        "m_sortingtools.mod",
        "m_sparsematrixcomms.mod",
        "m_sparsematrixdecomp.mod",
        "m_sparsematrix.mod",
        "m_sparsematrixplus.mod",
        "m_sparsematrixtomaps.mod",
        "m_spatialintegral.mod",
        "m_spatialintegralv.mod",
        "m_stdio.mod",
        "m_string.mod",
        "m_strtemplate.mod",
        "m_traceback.mod",
        "m_transfer.mod",
        "m_zeit.mod",
    ]

    __libs["mpeu"]["incfiles"] = []

    __libs["psmile.MPI1"]["incfiles"] = [
        "mod_oasis_advance.mod",
        "mod_oasis_coupler.mod",
        "mod_oasis_data.mod",
        "mod_oasis_getput_interface.mod",
        "mod_oasis_grid.mod",
        "mod_oasis_io.mod",
        "mod_oasis_ioshr.mod",
        "mod_oasis_kinds.mod",
        "mod_oasis_method.mod",
        "mod_oasis.mod",
        "mod_oasis_mpi.mod",
        "mod_oasis_namcouple.mod",
        "mod_oasis_parameters.mod",
        "mod_oasis_part.mod",
        "mod_oasis_string.mod",
        "mod_oasis_sys.mod",
        "mod_oasis_timer.mod",
        "mod_oasis_var.mod",
        "mod_prism.mod",
    ]

    __libs["scrip"]["incfiles"] = [
        "constants.mod",
        "grids.mod",
        "iounits.mod",
        "kinds_mod.mod",
        "mod_oasis_flush.mod",
        "netcdf_mod.mod",
        "remap_bicubic.mod",
        "remap_bicubic_reduced.mod",
        "remap_bilinear.mod",
        "remap_bilinear_reduced.mod",
        "remap_conservative.mod",
        "remap_distance_weight.mod",
        "remap_gaussian_weight.mod",
        "remap_vars.mod",
        "remap_write.mod",
        "timers.mod",
    ]

    def __init__(self, args):
        super().__init__(args)

        for k in self.__libs.keys():
            self.__libs[k]["filename"] = "lib" + k + ".a"
            self.__libs[k]["filerelpath"] = join_path(
                "lib", self.__libs[k]["filename"]
            )
            self.__libs[k]["pcname"] = "oasis3-" + k + ".pc"
            self.__libs[k]["pcrelpath"] = join_path(
                "lib", "pkgconfig", self.__libs[k]["pcname"]
            )
            self.__libs[k]["pcpath"] = join_path(
                self.__pkgdir, self.__libs[k]["pcname"]
            )

        # Uncomment to print package files and directories
        # pprint(self.__libs)

    def url_for_version(self, version):
        return "https://github.com/ACCESS-NRI/oasis3-mct/tarball/{0}".format(version)

    def __create_pkgconfig(self, spec, prefix):

        mkdirp(self.__pkgdir)
        for k in self.__libs.keys():
            text = f"""\
prefix={prefix}
exec_prefix=${{prefix}}
libdir=${{exec_prefix}}/lib
includedir=${{prefix}}/include

Name: {k}
Description: OASIS3-MCT 2.0 {k} Library for Fortran
Version: 2.0
Libs: -L${{libdir}} -l{k}
Cflags: -I${{includedir}}/{k}
"""

            with open(self.__libs[k]["pcpath"], "w", encoding="utf-8") as pc:
                nchars_written = pc.write(text)

            if nchars_written < len(text):
                raise OSError

    # https://spack-tutorial.readthedocs.io/en/ecp21/tutorial_advanced_packaging.html
    @property
    def libs(self):
        # NOTE: The order matters during the linking step of the cice5 build!
        # "-lpsmile.MPI1 -lmct -lmpeu -lscrip"
        libraries = ["libpsmile.MPI1", "libmct", "libmpeu", "libscrip"]
        return find_libraries(
            libraries, root=self.prefix, shared=False, recursive=True
        )

    # Following pattern:
    # https://spack-tutorial.readthedocs.io/en/latest/tutorial_buildsystems.html
    def edit(self, spec, prefix):
        SRCDIR = self.stage.source_path
        makeinc_path = join_path(SRCDIR, self.__makeinc)
        config = {}

        # TODO: https://github.com/ACCESS-NRI/ACCESS-OM/issues/12
        NCI_OPTIM_FLAGS = "-g3 -O2 -axCORE-AVX2 -debug all -check none -traceback"
        CFLAGS = ""
        if "+deterministic" in self.spec:
            NCI_OPTIM_FLAGS = "-g0 -O0 -axCORE-AVX2 -debug none -check none"
            CFLAGS = "-g0"

        if "+optimisation_report" in self.spec:
            NCI_OPTIM_FLAGS += " -qopt-report=5 -qopt-report-annotate"

        config["pre"] = f"""
# CHAN	: communication technique used in OASIS3 (MPI1/MPI2/NONE)
CHAN            = MPI1
#
# Paths for libraries, object files and binaries
#
# COUPLE	: path for oasis3-mct main directory
COUPLE={SRCDIR}
# ARCHDIR       : directory created when compiling
ARCHDIR= $(COUPLE)/compile_oa3-mct
AR          = ar
ARFLAGS     = -rvD
CFLAGS      = {CFLAGS}
F           = $(F90)
f90         = $(F90)
f           = $(F90)
"""

        config["gcc"] = f"""
# Compiling and other commands
MAKE        = make
F90         = mpif90 -Wall -fallow-argument-mismatch
CC          = gcc
LD          = mpif90
MCT_FCFLAGS =
#
# CPP keys and compiler options
#
CPPDEF    = -Duse_netCDF -Duse_comm_$(CHAN) -D__VERBOSE -DTREAT_OVERLAY
F90FLAGS_1  =
"""

        # module load intel-compiler/2019.5.281
        config["intel"] = f"""
# Compiling and other commands
MAKE        = /usr/bin/make
F90         = mpifort
CC          = mpicc
LD          = $(F90)

# -g is necessary in F90FLAGS and LDFLAGS for pgf90 versions lower than 6.1
# For compiling in double precision, put -r8
# For compiling in single precision, remove -r8 and add -Duse_realtype_single
# Causes non-deterministic builds: -traceback -debug all
NCI_INTEL_FLAGS = -r8 -i4 -fpe0 -convert big_endian -fno-alias -ip -check noarg_temp_created
NCI_REPRO_FLAGS = -fp-model precise -fp-model source -align all
ifeq ($(DEBUG), yes)
    NCI_DEBUG_FLAGS = -g3 -O0 -fpe0 -no-vec -debug all -check all -no-vec -traceback
    F90FLAGS_1      = $(NCI_INTEL_FLAGS) $(NCI_REPRO_FLAGS) $(NCI_DEBUG_FLAGS)
    CPPDEF          = -Duse_netCDF -Duse_comm_$(CHAN) -DTREAT_OVERLAY -DDEBUG -D__VERBOSE
    MCT_FCFLAGS     = $(NCI_REPRO_FLAGS) $(NCI_DEBUG_FLAGS) -ip
else
    NCI_OPTIM_FLAGS = {NCI_OPTIM_FLAGS}
    F90FLAGS_1      = $(NCI_INTEL_FLAGS) $(NCI_REPRO_FLAGS) $(NCI_OPTIM_FLAGS)
    CPPDEF          = -Duse_netCDF -Duse_comm_$(CHAN) -DTREAT_OVERLAY
    MCT_FCFLAGS     = $(NCI_REPRO_FLAGS) $(NCI_OPTIM_FLAGS) -ip
endif
"""

        config["post"] = f"""
f90FLAGS_1  = $(F90FLAGS_1)
FFLAGS_1    = $(F90FLAGS_1)
fFLAGS_1    = $(F90FLAGS_1)
CCFLAGS_1   =
LDFLAGS     =
#
###################
#
# Additional definitions that should not be changed
#
FLIBS		= ""
# BINDIR        : directory for executables
BINDIR          = $(ARCHDIR)/bin
# LIBBUILD      : contains a directory for each library
LIBBUILD        = $(ARCHDIR)/build/lib
# INCPSMILE     : includes all *o and *mod for each library
INCPSMILE       = -I$(LIBBUILD)/psmile.$(CHAN) -I$(LIBBUILD)/mct

F90FLAGS  = $(F90FLAGS_1) $(INCPSMILE) $(CPPDEF)
f90FLAGS  = $(f90FLAGS_1) $(INCPSMILE) $(CPPDEF)
FFLAGS    = $(FFLAGS_1) $(INCPSMILE) $(CPPDEF)
fFLAGS    = $(fFLAGS_1) $(INCPSMILE) $(CPPDEF)
CCFLAGS   = $(CCFLAGS_1) $(INCPSMILE) $(CPPDEF)
#
#############################################################################
"""

        with open(makeinc_path, "w") as makeinc:
            makeinc.write(
                config["pre"]
                + config[self.compiler.name]
                + config["post"]
            )

    def build(self, spec, prefix):
        # See doc/oasis3mct_UserGuide.pdf:
        #
        # compiles all OASIS3-MCT libraries mct, scrip and psmile:
        # make -f TopMakefileOasis3
        with working_dir(join_path(self.stage.source_path, self.__makefiledir)):
            build = Executable("make")
            build("-f", "TopMakefileOasis3")

        # Upstream is missing a pkgconfig files, so we'll create them.
        self.__create_pkgconfig(spec, prefix)

    def install(self, spec, prefix):

        mkdirp(prefix.lib.pkgconfig)

        src_dst = []
        for libname in self.__libs.keys():

            mkdirp(join_path(prefix.include, libname))

            for f in [
                self.__libs[libname]["filerelpath"],
                self.__libs[libname]["pcrelpath"],
            ]:
                src_dst.append(
                    (join_path(self.__builddir, f), join_path(prefix, f))
                )

            for f in self.__libs[libname]["incfiles"]:
                src_dst.append(
                    (
                        join_path(self.__incdir, libname, f),
                        join_path(prefix.include, libname, f),
                    )
                )

        # Uncomment to print source and destination tuples
        # pprint(src_dst)
        [install(s, d) for (s, d) in src_dst]


"""empty message

Revision ID: 267cbf583d49
Revises: fdd28027966c
Create Date: 2020-04-02 10:25:24.509904

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '267cbf583d49'
down_revision = 'fdd28027966c'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(op.f('ix_forestilling_prod_nr'), 'forestilling', ['prod_nr'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_forestilling_prod_nr'), table_name='forestilling')
    # ### end Alembic commands ###


# encoding: utf-8

# @author: wenxuan
# @contact: 749935253@qq.com
# @software: PyCharm
# @file: Model.py
# @time: 19-6-29 下午2:07
import torch
import torch.nn as nn
import torch.nn.functional as F
import sklearn
print(sklearn.__version__)

class Net(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool1(F.relu(self.conv1(x)))
        x = self.pool2(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def initializer_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.xavier_normal_(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight.data, 0.01)
                m.bias.data.zero_()

net = Net()
net.initializer_weights()


import numpy as np
class DetectorNoise:
	"""
	compute the total detector noise as a function of wavelength
	"""
	def __init__(self,wavelength,R,n_ints):
		self.wavelength = wavelength
		self.sigma = self.JWST_total_detector_noise(self.wavelength,R,n_ints)

	# utility functions
	def JWST_total_detector_noise(self,wavelength, R, n_ints):
		n = len(wavelength)
		N_d_total = np.zeros(n)
		# For NIRISS
		index_1 = np.nonzero(np.logical_and(wavelength>=1, wavelength < 2.5))
		N_d_NIRISS = 18
		n_pix_NIRISS = 25*2
		R_native_NIRISS = 700
		N_d_total[index_1] = self.detector_noise_eqn(N_d_NIRISS,n_pix_NIRISS,n_ints,R_native_NIRISS,R)

		# for NIRCam
		index_2 = np.nonzero(np.logical_and(wavelength>=2.5, wavelength < 5))
		N_d_NIRCam = 18
		n_pix_NIRCam = 2*2
		R_native_NIRCam = 1700
		N_d_total[index_2] = self.detector_noise_eqn(N_d_NIRCam,n_pix_NIRCam,n_ints,R_native_NIRCam,R)

		# for MIRI
		index_3 = np.nonzero(np.logical_and(wavelength>=5., wavelength < 11.))
		N_d_NIRCam = 28
		n_pix_NIRCam = 2*2
		R_native_NIRCam = 100
		N_d_total[index_3] = self.detector_noise_eqn(N_d_NIRCam,n_pix_NIRCam,n_ints,R_native_NIRCam,R)

		return N_d_total

	def detector_noise_eqn(self,N_d,n_pix,n_ints,R_native,R):
		# N_d: total detector noise of a single integration
		# n_pix: the numeber of spatial x 2 spectra pixels smmed in each R_native
		#        native 2-pixel resolution element of the selected observing mode
		# n_ints: number of integrations during exposure time t
		# R: final binned spectra resolution
		return N_d*np.sqrt(n_pix*n_ints*R_native/R)


import threading
import socket
from src.Message import Message
from src.Message import MessageType
import json
import os

BUFFERSIZE = 1024
class Tracker:
    members = []
    socket = None
    lock = threading.Lock()

    def __init__(self, host, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.bind((host, port))

        self.initListening()

    def initListening(self):
        print('tracker will start listening...', flush=True)
        while True:
            response, address = self.socket.recvfrom(1024)
            message = Message.parse(response)
            if(message.type == MessageType.GET_MEMBERS):
                self.joinSwarm(address)
                continue
            if(message.type == MessageType.KILL):
                print('finishing', flush=True)
                os._exit(0)

            raise NotImplementedError(message.type)

    def joinSwarm(self, address):

        print('(%s, %s) getting members' % (address[0], address[1]) , flush=True)

        message = Message(
            MessageType.GET_MEMBERS,
            json.dumps(self.members)
        )

        print('New member (%s, %s) joining swarm' % (address[0], address[1]) , flush=True)

        self.members.append(address)

        self.socket.sendto(
            message.toByteStr(),
            address
        )


import pytest

from workday import WorkDay

@pytest.mark.parametrize(
    "day, start_time, end_time, expected_payment",
    [
        ('MO', '06:00', '08:00', 50),
        ('SU', '07:00', '08:00', 30),
        ('SA', '07:00', '11:01', 100),
        ('WE', '20:00', '21:00', 20),
        ('MO', '08:00', '10:01', 40),
        ('MO', '08:00', '20:4', 200)
    ]
)
def test_total_payment_by_day(day, start_time, end_time, expected_payment):
    work_day = WorkDay(day, start_time, end_time)
    assert work_day.compute_payment() == expected_payment


# Canadian Computing Competition: 2007 Stage 1, Junior #3
#
# In the CCC version of the game, there are 10 possible dollar amounts:
# $ 100, $ 500, $ 1 000, $ 5 000, $ 10 000, $ 25 000 , $ 50 000, $ 100 000, $ 500 000, $ 1 000 000
# sealed in imaginary briefcases. These dollar amounts are numbered 1 − 10
# (i.e. 1 → $ 100, 2 → $ 500, 3 → $ 1 000, ..., 10 → $ 1 000 000).
# Before the game starts the contestant will have chosen one of the briefcases as his/hers to possibly keep.
# During the game, some of the ten possible dollar amounts have been eliminated from the game
# because the contestant has selected some of the other briefcases and revealed the amounts inside.
#
# At some point, the contestant will stop opening briefcases,
# and a "Banker" will offer the contestant cash in exchange for what might be contained in his/her chosen briefcase.
# Then the contestant is asked: "Deal or No Deal?".
#
# Write a program that helps a player decide if he/she should choose "deal" or "no deal",
# by calculating the average of the remaining amounts
# (i.e., all unopened briefcases, including his/her "own" briefcase),
# and comparing that value to the "Banker's" offer. If the offer is higher than the average,
# then the player should "deal" otherwise, the player should say "no deal".

# Input Specification
# The user must input a number n ( 1 ≤ n < 10) which indicates how many cases have been opened so far,
# followed by a list of integers between 1 and 10 representing the values in the game that have been eliminated,
# followed by the "Banker's" offer. For example: 3, 2, 5, 10, 300 indicates that
# briefcases containing $ 500, $ 10 000, and $ 1 000 000 have been eliminated and the Banker's offer is $ 300.
# You may assume that no duplicate case numbers are entered for the eliminated values,
# and you may assume that the "Banker's" offer is an integer greater than 10.

# Output Specification
# The program will print out one of two statements: deal or no deal.

values = {
    "1": 100, "2": 500, "3": 1000, "4": 5000, "5": 10000, "6": 25000, "7": 50000, "8": 100000, "9": 500000,
    "10": 1000000}
total = 1691600
numOpened = int(input())
opened = []
left = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
leftSum = 0
for i in range(numOpened):
    opened.append(input())
offer = int(input())
for i in range(len(opened)):
    total -= values[opened[i]]
    for j in range(len(left)):
        if opened[i] == left[j]:
            del left[j]
            break
if total/len(left) < offer:
    print("deal")
else:
    print("no deal")

# cases = [100, 500, 1000, 5000, 10000, 25000, 50000, 100000, 500000, 1000000]
# newc = cases.copy()
#
# num = int(input())
# for i in range(num):
#     index = int(input()) - 1
#     item = cases[index]
#     newc.remove(item)
# avg = sum(newc)/len(newc)
#
# offer = int(input())
# if offer>avg:
#     print('deal')
# else:
#     print('no deal')

J3/CCC '07 J3 - Deal or No Deal Calculator.py


"""
Stellar Parameters Hess Diagram
-------------------------------
This example shows how to create Hess diagrams of the Segue Stellar Parameters
Pipeline (SSPP) data to show multiple features on a single plot.  The left
panel shows the density of the points on the plot.  The right panel shows
the average metallicity in each pixel, with contours reflecting the density
shown in the left plot.
"""
# Author: Jake VanderPlas <vanderplas@astro.washington.edu>
# License: BSD
#   The figure is an example from astroML: see http://astroML.github.com
import numpy as np
from matplotlib import pyplot as plt

#------------------------------------------------------------
# Get SDSS SSPP data
from astroML.datasets import fetch_sdss_sspp
data = fetch_sdss_sspp()

# do some reasonable magnitude cuts
rpsf = data['rpsf']
data = data[(rpsf > 15) & (rpsf < 19)]

# get the desired data
logg = data['logg']
Teff = data['Teff']
FeH = data['FeH']

#------------------------------------------------------------
# Plot the results using the binned_statistic function
from astroML.stats import binned_statistic_2d
N, xedges, yedges = binned_statistic_2d(Teff, logg, FeH,
                                        'count', bins=100)
FeH_mean, xedges, yedges = binned_statistic_2d(Teff, logg, FeH,
                                               'mean', bins=100)

# Define custom colormaps: Set pixels with no sources to white
cmap = plt.cm.jet
cmap.set_bad('w', 1.)

cmap_multicolor = plt.cm.jet
cmap_multicolor.set_bad('w', 1.)

# Create figure and subplots
fig = plt.figure(figsize=(8, 4))
fig.subplots_adjust(wspace=0.25, left=0.1, right=0.95,
                    bottom=0.07, top=0.95)

#--------------------
# First axes:
plt.subplot(121, xticks=[4000, 5000, 6000, 7000, 8000])
plt.imshow(np.log10(N.T), origin='lower',
           extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],
           aspect='auto', interpolation='nearest', cmap=cmap)
plt.xlim(xedges[-1], xedges[0])
plt.ylim(yedges[-1], yedges[0])
plt.xlabel(r'$\mathrm{T_{eff}}$')
plt.ylabel(r'$\mathrm{log(g)}$')

cb = plt.colorbar(ticks=[0, 1, 2, 3],
                  format=r'$10^{%i}$', orientation='horizontal')
cb.set_label(r'$\mathrm{number\ in\ pixel}$')
plt.clim(0, 3)

#--------------------
# Third axes:
plt.subplot(122, xticks=[4000, 5000, 6000, 7000, 8000])
plt.imshow(FeH_mean.T, origin='lower',
           extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],
           aspect='auto', interpolation='nearest', cmap=cmap_multicolor)
plt.xlim(xedges[-1], xedges[0])
plt.ylim(yedges[-1], yedges[0])
plt.xlabel(r'$\mathrm{T_{eff}}$')
plt.ylabel(r'$\mathrm{log(g)}$')

cb = plt.colorbar(ticks=np.arange(-2.5, 1, 0.5),
                  format=r'$%.1f$', orientation='horizontal')
cb.set_label(r'$\mathrm{mean\ [Fe/H]\ in\ pixel}$')
plt.clim(-2.5, 0.5)

# Draw density contours over the colors
levels = np.linspace(0, np.log10(N.max()), 7)[2:]
plt.contour(np.log10(N.T), levels, colors='k', linewidths=1,
            extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

plt.show()


import logging
import sys

from src.core.game import Game

def main(argv):

    logger = logging.getLogger('debug')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler(sys.stderr))

    port = 8089

    if len(argv) > 1:
        port = argv[1]

    Game.getInstance().run('localhost', port)

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


from django.db import models
from django.utils import timezone

from article.models import Article
from django.contrib.auth.models import User

# Create your models here.
# 留言
class Comment(models.Model):
    # 作者1對多
    author = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='comments'
    )
    # 文章1對多
    article = models.ForeignKey(
        Article,
        on_delete=models.CASCADE,
        related_name='comments'
    )
    # 內容
    content = models.TextField()
    created = models.DateTimeField(default=timezone.now)
    # 父評論
    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='children'
    )
    class Meta:
        ordering = ['-created']

    def __str__(self):
        return self.content[:20]


#!/usr/bin/python3
"""
Contains rectangle
"""
from sys import argv
from models.base import Base

class Rectangle(Base):
    """representation of a rectangle inherits from Base"""
    def __init__(self, width, height, x=0, y=0, id=None):
        """Class constructor"""
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        super().__init__(id)

    @property
    def width(self):
        """width getter"""
        return self.__width

    @property
    def height(self):
        """height getter"""
        return self.__height

    @property
    def x(self):
        """x getter"""
        return self.__x

    @property
    def y(self):
        """y getter"""
        return self.__y

    @width.setter
    def width(self, value):
        """set width"""
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if value <= 0:
            raise ValueError("width must be > 0")
        self.__width = value

    @height.setter
    def height(self, value):
        """set height"""
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if value <= 0:
            raise ValueError("height must be > 0")
        self.__height = value

    @x.setter
    def x(self, value):
        """set x"""
        if not isinstance(value, int):
            raise TypeError("x must be an integer")
        if value < 0:
            raise ValueError("x must be >= 0")
        self.__x = value

    @y.setter
    def y(self, value):
        """set y"""
        if not isinstance(value, int):
            raise TypeError("y must be an integer")
        if value < 0:
            raise ValueError("y must be >= 0")
        self.__y = value

    def area(self):
        """defines area"""
        return self.__width * self.__height

    def display(self):
        """prints the display"""
        for j in range(self.y):
            print()
        for i in range(self.height):
            for le in range(self.x):
                print(" ", end="")
            for k in range(self.width):
                print("#", end="")
            print()

    def __str__(self):
        """string representation"""
        return "[Rectangle] ({:d}) {:d}/{:d} - {:d}/{:d}".format(self.id,
                                                                 self.__x,
                                                                 self.__y,
                                                                 self.__width,
                                                                 self.__height)

    def update(self, *args, **kwargs):
        """update print in stdout"""
        if len(args):
            for i, a in enumerate(args):
                if i == 0:
                    self.id = a
                elif i == 1:
                    self.width = a
                elif i == 2:
                    self.height = a
                elif i == 3:
                    self.x = a
                elif i == 4:
                    self.y = a
        else:
            if "id" in kwargs:
                self.id = kwargs["id"]
            if "width" in kwargs:
                self.width = kwargs["width"]
            if "height" in kwargs:
                self.height = kwargs["height"]
            if "x" in kwargs:
                self.x = kwargs["x"]
            if "y" in kwargs:
                self.y = kwargs["y"]

    def to_dictionary(self):
        """returns the dictionary representation"""
        d = {}
        d["x"] = self.x
        d["y"] = self.y
        d["id"] = self.id
        d["height"] = self.height
        d["width"] = self.width
        return d


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head==None or head.next==None:
            return head
        ans=head.next
        def swap(node):
            if node==None or node.next==None:
                return
            temp=node.next.next
            node.next.next=node
            # node.next=temp==None ? None ?:
            if temp==None:
                node.next=None
            elif temp.next==None:
                node.next=temp
            else:
                node.next=temp.next
            return swap(temp)
        swap(head)
        return ans


import numpy as np

def gradient_descent(x,y):
    m_curr = b_curr = 0 #start eith some values of m and b and then take these baby steps to reach to a global minima
    iterations = 10000 #defining how many baby steps you are going to do
    n = len(x)
    learning_rate = 0.08 #defining learning rate. here random value is given. See hoe the algorithm behaves and then tweak those parameters

    for i in range(iterations):
        y_predicted = m_curr * x + b_curr #calculate the predicted value of y
        cost = (1/n) * sum([val**2 for val in (y-y_predicted)]) #printing cost which is the cost function
        md = -(2/n)*sum(x*(y-y_predicted)) #calculate m derivative
        bd = -(2/n)*sum(y-y_predicted) #calculate b derivative
        m_curr = m_curr - learning_rate * md #adjusting m current
        b_curr = b_curr - learning_rate * bd #adjusting b current
        print ("m {}, b {}, cost {} iteration {}".format(m_curr,b_curr,cost, i)) #printing each iteration value along with cost at each iteration

x = np.array([1,2,3,4,5]) # np.array i.e. numpy array is used because matrix multiplication is very convenient with this, also this tends to be faster than simple python list
y = np.array([5,7,9,11,13])

gradient_descent(x,y)


class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if len(s) != len(t): return False
        ls = [0] * 26
        lt = [0] * 26
        for i in range(len(s)):
            ls[ord(s[i])-ord('a')] += 1
            lt[ord(t[i])-ord('a')] += 1
        for i in range(26):
            if ls[i] != lt[i]: return False
        return True


import datetime
import math
import numpy as np

import pandas as pd

START_HISTORICAL_DATE = datetime.datetime(2006, 1, 1)

def calc_sharp_ratio(ret: pd.Series):
    return math.sqrt(252)*ret.mean()/ret.std()

def calc_drawdown(ret: pd.Series):
    term = pd.DataFrame({
        "ret": ret.dropna().cumsum(),
        'high_watermark_index': ret.dropna().cumsum().index,
        'high_watermark_ret': np.NaN,
        'drawdown_deep': np.NaN,
        'drawdown_duration': np.NaN
    })
    if len(term) == 0:
        return term

    highest_watermark_index = None
    for d in term.index:
        row = term.loc[d]
        if not highest_watermark_index or row['ret'] >= term.loc[highest_watermark_index]['ret']:
            highest_watermark_index = d
        term.loc[d, 'high_watermark_index'] = highest_watermark_index
    term['high_watermark_ret'] = term['high_watermark_index'].transform(
        lambda x: term.loc[x]['ret'], axis=0)

    term['drawdown_deep'] = term['high_watermark_ret'] - term['ret']
    term['drawdown_duration'] = term.index - term['high_watermark_index']
    return term


# -*- encoding: utf-8 -*-
# Author: li_colin
from handler_exec import req, CashExecAction

Ex = CashExecAction()
req.setOperation1("queryAsset")
req.setOperation2("queryPosition")
req.setOperation3("newInsertOrder")
req.setOperation4("queryAsset")
req.setOperation5("queryPosition")

if req.getOperation1():
    Ex.handle(req, req.getOperation1())
if req.getOperation2():
    Ex.handle(req, req.getOperation2())
if req.getOperation3():
    Ex.handle(req, req.getOperation3())
if req.getOperation4():
    Ex.handle(req, req.getOperation3())
if req.getOperation5():
    Ex.handle(req, req.getOperation3())
print(req.getXtpID())


from time import time
from config import args
from utils.paragraph import Paragraph
from utils.vocabulary import Vocabulary
from utils.estimate import esitmate

if __name__ == "__main__":
    if args.storage == "binary_linear_list":
        from storage.binary_linear import BinaryLinearList
        storage = BinaryLinearList(args.max_gram)
    if args.storage == "linear_list":
        from storage.linear import LinearList
        storage = LinearList()
    if args.storage == "hash_bucket":
        from storage.hash_bucket import HashBucket
        storage = HashBucket()
    if args.storage == "trie":
        from storage.trie import Trie
        storage = Trie()
    vocabulary = Vocabulary.load(args.vocabulary_path, args.max_gram, storage)

    if args.tokenizer == "forward":
        from tokenizer.forward import Forward
        tokenizer = Forward(vocabulary)
    if args.tokenizer == "backward":
        from tokenizer.backward import Backward
        tokenizer = Backward(vocabulary)
    if args.tokenizer == "segmentation":
        from tokenizer.segmentation import OmniSegmentation
        tokenizer = OmniSegmentation(vocabulary)

    if args.multiple_process:
        from tokenizer.multi_process import MultiProcess
        tokenizer = MultiProcess(tokenizer)

    paragraph = Paragraph.load(args.data_path, False, args.max_line)

    start = time()
    paragraph.tokenize(tokenizer)
    end = time()

    paragraph.save(args.result_path + "/" + args.tokenizer + "+" +
                   args.storage + ".txt")

    if args.save_result:
        precision, callback, F1 = esitmate(
            args.result_path + "/" + args.tokenizer + "+" + args.storage +
            ".txt", args.standard_path)
        with open(args.output_path, 'a') as f:
            f.write("tokenizer: " + args.tokenizer + "\n")
            f.write("multi-process: " + str(args.multiple_process) + "\n")
            f.write("storage: " + args.storage + "\n")
            f.write("max gram: " + str(args.max_gram) + "\n")
            f.write("max line: " + str(args.max_line) + "\n")
            f.write("precision: " + str(precision) + "\n")
            f.write("callback: " + str(callback) + "\n")
            f.write("F1: " + str(F1) + "\n")
            f.write("time for tokenizing: " + str(end - start) + "\n")
            f.write("\n")