"""Plot mode from FEM"""
import sys

import meshio
import numpy as np
from matplotlib import pyplot as plt

def ri_to_c(filename: str) -> np.array:
    val = np.loadtxt(filename)
    return val[:, 2] + 1j * val[:, 3]

def print_val() -> None:
    val0 = ri_to_c("data/eigs_s0")
    val1 = ri_to_c("data/eigs_s1")
    print(np.abs(val0 - val1).max())

    return None

def plot_mode(filename: str) -> None:
    reader = meshio.read(f"{filename}")

    x = reader.points

    quad = reader.cells[0].data
    tri = np.zeros((2 * np.size(quad, 0), 3), dtype=int)
    for q in range(np.size(quad, 0)):
        tri[2 * q, :] = [quad[q, 0], quad[q, 1], quad[q, 2]]
        tri[2 * q + 1, :] = [quad[q, 0], quad[q, 2], quad[q, 3]]

    term = list(reader.point_data.keys())
    print("-" * len(f"Real part .... ← {term[0]}"))
    print(f"Real part .... ← {term[0]}")
    print(f"Imaginary part ← {term[1]}")
    print(f"Modulus ...... ← {term[2]}")

    ur = reader.point_data[term[0]]
    ui = reader.point_data[term[1]]
    um = reader.point_data[term[2]]

    plt.subplot(2, 2, 1)
    urM = np.amax(np.abs(ur))
    plt.tricontourf(x[:, 0], x[:, 1], tri, ur, 128, cmap="RdBu_r")
    plt.axis("equal")
    plt.clim(-urM, urM)
    plt.colorbar()
    plt.title("Real part", fontsize=15)

    plt.subplot(2, 2, 2)
    uiM = np.amax(np.abs(ui))
    plt.tricontourf(x[:, 0], x[:, 1], tri, ui, 128, cmap="RdBu_r")
    plt.axis("equal")
    plt.clim(-uiM, uiM)
    plt.colorbar()
    plt.title("Imaginary part", fontsize=15)

    plt.subplot(2, 2, 3)
    umM = np.amax(um)
    plt.tricontourf(x[:, 0], x[:, 1], tri, um, 128)
    plt.axis("equal")
    plt.clim(0.0, umM)
    plt.colorbar()
    plt.title("Modulus", fontsize=15)

    plt.subplot(2, 2, 4)
    ua = np.arctan2(ui, ur)
    plt.tricontourf(x[:, 0], x[:, 1], tri, ua, 128, cmap="twilight_shifted")
    plt.axis("equal")
    cbar = plt.colorbar(ticks=[-np.pi, -np.pi / 2.0, 0, np.pi / 2.0, np.pi])
    cbar.ax.set_yticklabels(
        [r"$-\pi$", r"$-\frac{\pi}{2}$", r"$0$", r"$\frac{\pi}{2}$", r"$\pi$"]
    )
    plt.title("Argument", fontsize=15)

    plt.show()

    return None

if __name__ == "__main__":
    # print_val()

    filename = sys.argv[1]
    plot_mode(filename)

section 6 - FEM computations/peanut_a_cst_resonances/plot_FEM.py


import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import sqlite3

sql = """
      SELECT t.sirad_id,
             adjusted_gross_income,
             credit_score
        FROM tax t
  INNER JOIN credit_scores cs
          ON t.sirad_id=cs.sirad_id
      """

with sqlite3.connect("build/research_v1.db") as cxn:
    df = pd.read_sql(sql, cxn, index_col="sirad_id")

print(df.head())

sns.set_style("whitegrid")
sns.regplot("adjusted_gross_income", "credit_score", data=df, truncate=True)
plt.tight_layout()
plt.savefig("scatterplot.png")


import numpy as np

import elfi
from elfi.utils import stochastic_optimization, weighted_cov

class TestStochasticOptimization():

    def test_1dim_x2(self):
        fun = lambda x : x.dot(x)
        bounds = ((-1, 1),)
        its = int(1e3)
        polish=True
        loc, val = stochastic_optimization(fun, bounds, its, polish)
        assert abs(loc - 0.0) < 1e-5
        assert abs(val - 0.0) < 1e-5

def test_weighted_cov():
    cov = [[.5, -.3], [-.3, .7]]
    x = np.random.RandomState(12345).multivariate_normal([1,2], cov, 1000)
    w = [1]*len(x)
    assert np.linalg.norm(weighted_cov(x, w) - cov) < .1


# -*- coding:utf8 -*-
from flask_restful_swagger_2 import Schema

class EmailModel(Schema):
    type = 'String'
    format = 'email'

class ResponseModel(Schema):
    type = 'object'
    property = {
        'code': {
            'type': 'integer',
            'format': 'int64'
        },
        'message': {
            'type': 'string'
        },
        'data': {
            'type': 'object'
        }
    }
    required = ['code', 'message']


from __future__ import print_function, absolute_import, division

from os import makedirs
from os.path import dirname, exists, join, basename
import sys
import json
import argparse
import numpy as np
import bpy
from mathutils import Matrix
import mathutils

def set_cycles(w=None, h=None, n_samples=None):
  scene = bpy.context.scene
  scene.render.engine = 'CYCLES'
  cycles = scene.cycles

  cycles.use_progressive_refine = True
  if n_samples is not None:
    cycles.samples = n_samples
  cycles.max_bounces = 100
  cycles.min_bounces = 10
  cycles.caustics_reflective = False
  cycles.caustics_refractive = False
  cycles.diffuse_bounces = 10
  cycles.glossy_bounces = 4
  cycles.transmission_bounces = 4
  cycles.volume_bounces = 0
  cycles.transparent_min_bounces = 8
  cycles.transparent_max_bounces = 64

  # Avoid grainy renderings (fireflies)
  world = bpy.data.worlds['World']
  world.cycles.sample_as_light = True
  cycles.blur_glossy = 5
  cycles.sample_clamp_indirect = 5

  # Ensure no background node
  world.use_nodes = True
  try:
    world.node_tree.nodes.remove(world.node_tree.nodes['Background'])
  except KeyError:
    pass

  scene.render.tile_x = 16
  scene.render.tile_y = 16
  if w is not None:
    scene.render.resolution_x = w
  if h is not None:
    scene.render.resolution_y = h
  scene.render.resolution_percentage = 100
  scene.render.use_file_extension = True
  scene.render.image_settings.file_format = 'PNG'
  scene.render.image_settings.color_mode = 'RGB'
  scene.render.image_settings.color_depth = '8'
  cycles.device = "GPU"

  for scene in bpy.data.scenes:
    scene.cycles.device = 'GPU'

def add_object(model_path, rot_mat=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
               trans_vec=(0, 0, 0), scale=1, name=None):
  # Import
  if model_path.endswith('.obj'):
    bpy.ops.import_scene.obj(filepath=model_path, axis_forward='-Z', axis_up='Y')
  else:
    raise NotImplementedError("Importing model of this type")

  obj_list = []
  for i, obj in enumerate(bpy.context.selected_objects):
    # Rename
    if name is not None:
      if len(bpy.context.selected_objects) == 1:
        obj.name = name
      else:
        obj.name = name + '_' + str(i)

    # Compute world matrix
    # trans_4x4 = Matrix.Translation(trans_vec)
    trans_4x4 = Matrix.Translation(trans_vec)
    rot_4x4 = Matrix(rot_mat).to_4x4()
    scale_4x4 = Matrix(np.eye(4))  # don't scale here

    #rot_aux=Matrix(rot_aux).to_4x4()
    # intrinsics=1.0e+03 * Matrix([[1.0668,0,0.3130],[0,1.0675,0.2413],[0,0,0.0010]])
    obj.matrix_world = trans_4x4 * rot_4x4 * scale_4x4
    rot_aux = ((1.0, 0.0, 0.0),
               (0.0, -1.0, 0.0),
               (0.0, 0.0, -1.0))

    rot_aux = Matrix(rot_aux).to_4x4()
    obj.matrix_world = rot_aux * obj.matrix_world

    obj.scale = (scale, scale, scale)

    obj_list.append(obj)

  if len(obj_list) == 1:
    return obj_list[0]
  else:
    return obj_list

def add_camera(xyz=(0, 0, 0), rot_vec_rad=(0, 0, 0), name=None,
               proj_model='PERSP', f=35, sensor_fit='HORIZONTAL',
               sensor_width=22.118):
  bpy.ops.object.camera_add()
  cam = bpy.context.active_object

  if name is not None:
    cam.name = name

  cam.location = xyz
  cam.rotation_euler = rot_vec_rad

  cam.data.type = proj_model
  cam.data.lens = f
  cam.data.sensor_fit = sensor_fit
  cam.data.sensor_width = sensor_width
  cam.rotation_mode = "YZX"

  return cam

def render_to_file(outpath):
  outdir = dirname(outpath)
  if not exists(outdir):
    makedirs(outdir)

  # Set active camera, just in case
  for o in bpy.data.objects:
    if o.type == 'CAMERA':
      bpy.context.scene.camera = o
      break

  # Render
  bpy.context.scene.render.filepath = outpath
  bpy.ops.render.render(write_still=True)

def render(data, output_path, division_num):
  # w, h = data['img_size']
  w, h = 640, 480
  set_cycles(w=w, h=h, n_samples=50)

  # Remove all default objects
  for obj in bpy.data.objects:
    obj.select = True
  bpy.ops.object.delete()

  # Object
  obj = add_object(data['model'], data['rot_mat'],
                   data['trans_mat'], name='object')

  # Lighting
  world = bpy.data.worlds['World']
  world.light_settings.use_ambient_occlusion = True
  world.light_settings.ao_factor = 0.9
  data['focal_length'] = 35.27039762259147

  # Camera
  camera = add_camera(name='camera', proj_model='PERSP',
                      f=data['focal_length'], sensor_fit='HORIZONTAL')

  camera.data.clip_end = 1e10

  # https://github.com/panmari/stanford-shapenet-renderer/blob/master/render_blender.py
  bpy.context.scene.use_nodes = True
  tree = bpy.context.scene.node_tree
  links = tree.links

  # Add passes for additionally dumping albedo and normals.
  bpy.context.scene.render.layers["RenderLayer"].use_pass_normal = True
  bpy.context.scene.render.layers["RenderLayer"].use_pass_color = True

  render_layers = tree.nodes.new('CompositorNodeRLayers')

  depth_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
  depth_file_output.label = 'Depth Output'
  # Remap as other types can not represent the full range of depth.
  map = tree.nodes.new(type="CompositorNodeMapValue")
  # Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
  map.offset = [-data['depth_minmax'][0]+0.0]
  map.size = [6.0]
  map.use_min = True
  map.min = [0]
  links.new(render_layers.outputs['Depth'], map.inputs[0])
  links.new(map.outputs[0], depth_file_output.inputs[0])

  scale_normal = tree.nodes.new(type="CompositorNodeMixRGB")
  scale_normal.blend_type = 'MULTIPLY'
  # scale_normal.use_alpha = True
  scale_normal.inputs[2].default_value = (0.5, 0.5, 0.5, 1)
  links.new(render_layers.outputs['Normal'], scale_normal.inputs[1])

  bias_normal = tree.nodes.new(type="CompositorNodeMixRGB")
  bias_normal.blend_type = 'ADD'
  # bias_normal.use_alpha = True
  bias_normal.inputs[2].default_value = (0.5, 0.5, 0.5, 0)
  links.new(scale_normal.outputs[0], bias_normal.inputs[1])

  normal_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
  normal_file_output.label = 'Normal Output'
  links.new(bias_normal.outputs[0], normal_file_output.inputs[0])

  albedo_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
  albedo_file_output.label = 'Albedo Output'
  links.new(render_layers.outputs['Color'], albedo_file_output.inputs[0])

  for output_node in [depth_file_output, normal_file_output, albedo_file_output]:
    output_node.base_path = ''

  scene = bpy.context.scene
  # scene.render.filepath = "./render/" + output_path + "{:04d}".format(division_num) + ".png"
  render_dir = join(output_path, "render", "{:04d}".format(division_num))
  if not exists(render_dir):
    makedirs(render_dir)

  render_name = basename(data["mask"]).replace("mask", "render")
  scene.render.filepath = join(render_dir, render_name)
  print(output_path)
  # depth_file_output.file_slots[0].path = "./depth/" + output_path + "{:04d}".format(division_num) + "#"
  depth_dir = join(output_path, "depth", "{:04d}".format(division_num))
  if not exists(depth_dir):
    makedirs(depth_dir)

  depth_name = basename(data["mask"]).replace("mask", "depth").split(".")[0]
  depth_file_output.file_slots[0].path = join(depth_dir, depth_name) + "#"

  # normal_file_output.file_slots[0].path = "./normals/" + output_path + "{:04d}".format(division_num) + "#"
  normal_dir = join(output_path, "normal", "{:04d}".format(division_num))
  if not exists(normal_dir):
    makedirs(normal_dir)

  normal_name = basename(data["mask"]).replace("mask", "normal").split(".")[0]
  normal_file_output.file_slots[0].path = join(normal_dir, normal_name) + "#"
  # import pdb;pdb.set_trace()

  # albedo_file_output.file_slots[0].path = "./RGB/" + output_path + "#"
  # albedo_file_output.file_slots[0].path = scene.render.filepath + "_albedo.png"

  for o in bpy.data.objects:
    if o.type == 'CAMERA':
      bpy.context.scene.camera = o
      break

  bpy.ops.render.render(write_still=True)  # render still

  # render_to_file(output_path)

if __name__ == '__main__':

  parser = argparse.ArgumentParser()
  parser.add_argument('--anno_idx', type=int, default=0,
                      help='index of annotion')
  parser.add_argument('--division_num', type=int, default=23,
                      help='division number 0<=division_num<60')
  parser.add_argument('--output_path', type=str, default='/media/hdd/YCBvideo/YCB_Video_Dataset/Generated_YCB_Video_Dataset',
                      help='output image path')
  if '--' not in sys.argv:
    argv = []
  else:
    argv = sys.argv[sys.argv.index('--') + 1:]
  args = parser.parse_args(argv)

  with open('{:04d}.json'.format(args.division_num), "r") as j_file:
    data_list = json.load(j_file)

  render(data_list[args.anno_idx], args.output_path, args.division_num)
  #
  # data_list = json.load(open('mug.json'))
  # render(data_list[args.anno_idx], args.output_path)
  # print('Original Image:', data_list[args.anno_idx]['img'])
  # print('Saved to:', args.output_path)
  # import gen_mask_and_json as gsif
  #
  # obj_pose_dict = gsif.rt_from_label("/media/hdd/YCBvideo/YCB_Video_Dataset/YCB_Video_Dataset/data/0000/000001-label.png")
  # data = dict()
  # data['img_size'] = (640, 480)
  # data['model'] = '/media/hdd/YCBvideo/YCB_Video_Models/models/009_gelatin_box/textured.obj'
  # data['rot_mat'] = obj_pose_dict['8'][:,:-1]
  # data['trans_mat'] = obj_pose_dict['8'][:, -1]
  # data['focal_length'] = 35.27039762259147
  # render(data, 'test')
  # data_list = json.load(open('0000.json'))
  # with open('0000.json', "r") as j_file:
  #   data_list = json.load(j_file)
  # output_path = "./test"
  # render(data_list[0], output_path)


# -*- coding: utf-8 -*-
from django.contrib import admin

from django import forms
#

from util.fx.models import Ccy
#
#
from .models import *
from .forms import *
#
from tiers.corresp.models import *
#
from django.contrib.contenttypes.admin import GenericTabularInline
from cashflow.models import *
from cashflow.signals import *
from cashflow.forms import *
from settlement.models import TreasuryPosition
#

from django.contrib.auth.models import Permission
admin.site.register(Permission)

class CashflowDetailInline(admin.TabularInline):
  model = CashFlowDetail
  form = CashFlowDetailForm
  extra = 0
  fieldsets = (
    (None, {
      'fields': ('id','nature','date_val','montant','corresp','account','chk_verify','date_pay','releveCmpt','chk_pay') #,
      }),
  )
  readonly_fields = ('id','nature','content_type','ref_id','date_val','montant','releveCmpt','chk_verify',)
  show_change_link = True
  can_delete = False

  def has_add_permission(self, request):
    return False

class CashflowDetailGenericInline(GenericTabularInline):
  model = CashFlowDetail
  form = CashFlowDetailForm
  extra = 0
  ct_field_name = 'content_type'
  id_field_name = 'object_id'
  fieldsets = (
    (None, {
      'fields': ('id','nature','date_val','montant','corresp','account','chk_verify','date_pay','releveCmpt','chk_pay') #,
      }),
  )
  show_change_link = True
  can_delete = False

class CashFlowDetailTwoWayFxInline(GenericTabularInline):
  model = CashFlowDetailTwoWayFxProxy
  form = CashFlowDetailForm
  extra = 0
  ct_field_name = 'content_type'
  id_field_name = 'object_id'
  fieldsets = (
    (None, {
      'fields': ('id','nature','date_val','montant','corresp','account','chk_verify','date_pay','sygmaCtrl','chk_pay') #,
      }),
  )
  readonly_fields = ('id','nature','date_val','montant','corresp','account','chk_verify')
  show_change_link = True
  can_delete = False

class TresorLimitAdmin(admin.ModelAdmin):

  list_display = ('id','account','date_val','montant_val','montant_act','montant_min','montant_max',)
  list_filter = ['corresp',]
  search_fields = []

# Register your models here.
admin.site.register(TresorLimit, TresorLimitAdmin)

class MMFileInline(admin.TabularInline):
  model = MMFile

class MMAdmin(admin.ModelAdmin):

  form = MMForm
  inlines = [CashflowDetailGenericInline, MMFileInline]

  list_display = ('id','ref_id','cpty','date_val','date_end','jour_restant','ccy_in','nominal','taux','repayment','chk_end',)
  list_filter = [
    ('type_product', admin.RelatedOnlyFieldListFilter),
    'chk_end',
  ]
  #search_fields = ['counterparty','ref_id','nominal']
  search_fields = ['ref_id','nominal',]
  date_hierarchy = 'date_val'

  #actions
  #----------------------------------------------------------------------
  def export_csv(modeladmin, request, queryset):
    import csv
    from django.http import HttpResponse
    from django.utils.encoding import smart_str
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=MoneyMarket.csv'
    writer = csv.writer(response, csv.excel)
    response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
    writer.writerow([
      smart_str(u"ref_id"),
      smart_str(u"type_product"),
      smart_str(u"statut"),
      smart_str(u"date_val"),
      smart_str(u"date_end"),
      smart_str(u"cpty"),
      smart_str(u"ccy_in"),
      smart_str(u"nominal"),
      smart_str(u"taux"),
      smart_str(u"repayment"),

    ])
    for obj in queryset:
      writer.writerow([
        smart_str(obj.ref_id),
        smart_str(obj.type_product),
        smart_str(obj.statut),
        smart_str(obj.date_val),
        smart_str(obj.date_end),
        smart_str(obj.cpty),
        smart_str(obj.ccy),
        smart_str(obj.nominal),
        smart_str(obj.taux),
        smart_str(obj.repayment),
      ])
    return response
  export_csv.short_description = u"Export CSV"

  def chk_end(modeladmin, request, queryset):
    statut = queryset.values_list('chk_end', flat=True)
    #date = request.POST['date']
    if not statut[0]:
      queryset.update(chk_end=True)
    else:
      queryset.update(chk_end=False)
    #modeladmin.message_user(request, ("Successfully updated statut for %d rows") % (queryset.count(),), messages.SUCCESS)
  chk_end.short_description = "Check/Uncheck End"

  actions = [export_csv, chk_end]

# Register your models here.
admin.site.register(MM, MMAdmin)

class FIFileInline(admin.TabularInline):
  model = FIFile

class FIAdmin(admin.ModelAdmin):

  inlines = [CashflowDetailGenericInline, FIFileInline]

  list_display = ('id','type_product','ref_id','ccy','nominal','date_val','taux','repayment','date_end','jour_restant','chk_end')
  list_filter = [
    ('type_product', admin.RelatedOnlyFieldListFilter),
    'chk_end',
    'ccy'
  ]
  search_fields = ['ref_id']
  date_hierarchy = 'date_val'

  #actions
  #----------------------------------------------------------------------
  def export_csv(modeladmin, request, queryset):
    import csv
    from django.http import HttpResponse
    from django.utils.encoding import smart_str
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=FixedIncome.csv'
    writer = csv.writer(response, csv.excel)
    response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
    writer.writerow([
      smart_str(u"ref_id"),
      smart_str(u"statut"),
      smart_str(u"date_val"),
      smart_str(u"nominal"),
      smart_str(u"ccy"),
      smart_str(u"taux"),
      smart_str(u"date_end"),
      smart_str(u"repayment"),
    ])
    for obj in queryset:
      writer.writerow([
        smart_str(obj.ref_id),
        smart_str(obj.statut),
        smart_str(obj.date_val),
        smart_str(obj.nominal),
        smart_str(obj.ccy),
        smart_str(obj.taux),
        smart_str(obj.date_end),
        smart_str(obj.repayment),
      ])
    return response

  export_csv.short_description = u"Export CSV"

  def chk_end(modeladmin, request, queryset):
    statut = queryset.values_list('chk_end', flat=True)
    #date = request.POST['date']
    if not statut[0]:
      queryset.update(chk_end=True)
    else:
      queryset.update(chk_end=False)
    #modeladmin.message_user(request, ("Successfully updated statut for %d rows") % (queryset.count(),), messages.SUCCESS)
  chk_end.short_description = "Check/Uncheck End"

  actions = [export_csv, chk_end]

# Register your models here.
admin.site.register(FI, FIAdmin)

class FXFileInline(admin.TabularInline):
  model = FXFile

class FXAdmin(admin.ModelAdmin):

  form = FXForm
  inlines = [CashFlowDetailTwoWayFxInline, FXFileInline]

  list_display = ('id','chk_end','cpty','type_product','date_val','ccy_pair','fx_rate', #'counterparty',
                  'corresp_in','ccy_in','montant_in',
                  'corresp_out','ccy_out','montant_out')

  list_filter = [('type_product', admin.RelatedOnlyFieldListFilter),
                 'chk_end',
                 ('ccy_pair', admin.RelatedOnlyFieldListFilter),
                 ('cpty', admin.RelatedOnlyFieldListFilter),
                 ]
  search_fields = ['montant_in','montant_out',]
  date_hierarchy = 'date_val'

  #actions
  #----------------------------------------------------------------------
  def export_csv(modeladmin, request, queryset):
    import csv
    from django.http import HttpResponse
    from django.utils.encoding import smart_str
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=FX.csv'
    writer = csv.writer(response, csv.excel)
    response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
    writer.writerow([
      smart_str(u"id"),
      smart_str(u"type_product"),
      smart_str(u"cpty"),
      smart_str(u"date_val"),
      smart_str(u"ccy_pair"),
      smart_str(u"fx_rate"),
      smart_str(u"ccy_in"),
      smart_str(u"montant_in"),
      smart_str(u"ccy_out"),
      smart_str(u"montant_out"),
    ])
    for obj in queryset:
      writer.writerow([
        smart_str(obj.pk),
        smart_str(obj.type_product),
        smart_str(obj.cpty),
        smart_str(obj.date_val),
        smart_str(obj.ccy_pair),
        smart_str(obj.fx_rate),
        smart_str(obj.ccy_in),
        smart_str(obj.montant_in),
        smart_str(obj.ccy_out),
        smart_str(obj.montant_out),
      ])
    return response

  export_csv.short_description = u"Export CSV"

  def chk_end(modeladmin, request, queryset):
    statut = queryset.values_list('chk_end', flat=True)
    #date = request.POST['date']
    if not statut[0]:
      queryset.update(chk_end=True)
    else:
      queryset.update(chk_end=False)
    #modeladmin.message_user(request, ("Successfully updated statut for %d rows") % (queryset.count(),), messages.SUCCESS)
  chk_end.short_description = "Check/Uncheck End"

  actions = [export_csv, chk_end]

# Register your models here.
admin.site.register(FX, FXAdmin)

class FXCliAdmin(admin.ModelAdmin):

  form = FXCliForm

  list_display = ('id','nature','object_id','ref_id','client','date_trd','date_val','fx_rate','fee_rate',
                  'montant_in','montant_out','chk_end','date_end')

  list_filter = ['nature',
                 ('type_product', admin.RelatedOnlyFieldListFilter),
                 'chk_ok',
                 'chk_end',
                 ('ccy_pair', admin.RelatedOnlyFieldListFilter),
                 ]
  search_fields = ['ref_id','montant_in','montant_out',]
  date_hierarchy = 'date_val'

  fieldsets = (
    ('Front Office', {
      'fields': (
                 'type_product',
                 'content_type','object_id',
                 'client',
                 'ccy_pair',
                 'date_trd',
                 'date_val',
                 'nature',
                 'ref_id',
                 ('ccy_out','montant_out'),
                 ('ccy_in','montant_in'),
                 'fx_rate','fee_rate',

                 )
      }),
    ('Contrôle de Trésorerie', {
      #'classes': ('collapse', 'open'),
      'fields': ('chk_ok',
                 #'ctrl_cf',
                 'cliFile',
                 'chk_end','date_end',
                 ),
      }),
  )

  def get_readonly_fields(self, request, obj=None):
    #if not request.user.is_superuser:
    self.readonly_fields = ['type_product','content_type','object_id',] #
    return self.readonly_fields

  def save_model(self, request, obj, form, change):

    if obj.chk_ok and obj.montant_out >= 0:

      if obj.nature:
        ct_value = float(obj.nature)*round(float(obj.montant_out) * (1 + float(obj.fee_rate)) * float(obj.fx_rate),0)
      else:
        ct_value = round(float(obj.montant_out) * float(obj.fx_rate),0)
        #
        obj.date_pay = obj.date_val
        obj.chk_pay = True

      tva = float(obj.nature)*round(ct_value * 0.189,0)
      #
      obj.montant_in = ct_value
      obj.montant_tva = tva

    obj.save()

  #actions
  #----------------------------------------------------------------------
  def export_csv(modeladmin, request, queryset):
    import csv
    from django.http import HttpResponse
    from django.utils.encoding import smart_str
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=FX.csv'
    writer = csv.writer(response, csv.excel)
    response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
    writer.writerow([
      smart_str(u"id"),
      smart_str(u"type_product"),
      smart_str(u"client"),
      smart_str(u"date_val"),
      smart_str(u"ccy_pair"),
      smart_str(u"fx_rate"),
      smart_str(u"ccy_in"),
      smart_str(u"montant_in"),
      smart_str(u"ccy_out"),
      smart_str(u"montant_out"),
    ])
    for obj in queryset:
      writer.writerow([
        smart_str(obj.pk),
        smart_str(obj.type_product),
        smart_str(obj.client),
        smart_str(obj.date_val),
        smart_str(obj.ccy_pair),
        smart_str(obj.fx_rate),
        smart_str(obj.ccy_in),
        smart_str(obj.montant_in),
        smart_str(obj.ccy_out),
        smart_str(obj.montant_out),
      ])
    return response

  export_csv.short_description = u"Export CSV"

  def chk_end(modeladmin, request, queryset):
    statut = queryset.values_list('chk_end', flat=True)
    #date = request.POST['date']
    if not statut[0]:
      queryset.update(chk_end=True)
    else:
      queryset.update(chk_end=False)
    #modeladmin.message_user(request, ("Successfully updated statut for %d rows") % (queryset.count(),), messages.SUCCESS)
  chk_end.short_description = "Check/Uncheck End"

  actions = [export_csv, chk_end]

# Register your models here.
admin.site.register(FXCli, FXCliAdmin)


from dal import autocomplete
from django.urls import path
from django.conf.urls import url
from account.views import (
    LoginView,
    PhotoSignatureView,
    ProfileView,
    SignupView,
    organization_autocomplete,
    school_autocomplete,
    VerificationView,
)
from account.models import School
urlpatterns = [
    path('login/', LoginView.as_view(), name='login'),
    path('signup/', SignupView.as_view(), name="signup"),
    path('signup/photo-signature/<pk>',
         PhotoSignatureView.as_view(), name="photo-sig"),
    path('activate/<uidb64>/<token>', VerificationView.as_view(), name='activate'),
    path('profile/', ProfileView.as_view(), name="profile"),
    url(r'^school-autocomplete/$', school_autocomplete.as_view(create_field='school_name'),
        name='school-autocomplete'),
    url(r'^organization-autocomplete/$', organization_autocomplete.as_view(create_field='organization_name'),
        name='organization-autocomplete')

]


# PYTHON3 FILE

# This program store words of a file text in a list and
# sort the words by alphabetical order

fname = input("Enter a file name :")
fh = open(fname)
lst = list()
for line in fh:
    line = line.rstrip()
    line = line.split()
    for word in line :
        if word not in lst :
            lst.append(word)
lst.sort()
print(lst)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('quiz', '0003_auto_20151031_1247'),
    ]

    operations = [
        migrations.CreateModel(
            name='QuizAnswer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('correct', models.NullBooleanField()),
                ('question', models.ForeignKey(to='quiz.Question', on_delete=django.db.models.deletion.PROTECT)),
            ],
        ),
    ]


#!/usr/bin/env python
import numpy as np
import pandas as pd
from skimage.io import imread
from skimage.transform import resize
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from keras.models import model_from_json
from keras.utils import np_utils
from skimage.color import rgb2gray
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import sys
from netmodel import *
from flask import *

app = Flask(__name__)

@app.route('/Check', methods=['POST'])
def Check():
    data = request.get_json(force=True)
    filepath = data['filepath']
    img = imread(filepath)
    img = rgb2gray(img)
    img = resize(img, (28,28))
    img = img.reshape(-1,28,28,1)
    test_pred = loaded_model.predict(img)
    print(test_pred)
    test_pred = np.argmax(test_pred,1)
    return str(test_pred)

def trainingmodel(trainingdata):
    train = pd.read_csv(trainingdata)
    # test = pd.read_csv('digit-recognizer/test.csv')
    train_y = train['label']
    train_x = train.drop('label',axis=1)
    train_x = train_x.astype("float32")/255.0
    train_x = train_x.values.reshape(-1,28,28,1)
    # test_x = test.astype("float32")/255.0
    # test_x = test_x.values.reshape(-1,28,28,1)
    train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size = 0.1) # split into training and validation 10%
    """
    from PIL import Image
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(15,15))
    rows = 3
    columns = 3
    for i in range(9):
        fig.add_subplot(rows, columns, i+1)
        img_arr = test.iloc[i,1:].values.reshape(28,28)
        plt.imshow(img_arr)
        plt.axis('off')
        plt.title("Label" + ' ' + str(test.iloc[i,0]))
    """
    train_y = np_utils.to_categorical(train_y,10)
    val_y = np_utils.to_categorical(val_y,10)
    model = modelmaker2()
    print(model.summary())
    model.compile(loss='categorical_crossentropy',
                 optimizer='adam',
                 metrics=['accuracy'])
    model.fit(train_x,train_y,batch_size=64,epochs=2,verbose=1)
    t = input("save to model file? [y/n] ")
    if t == 'y' or t == 'Y':
        model_json = model.to_json()
        with open("model1.json","w") as json_file:
            json_file.write(model_json)
            model.save_weights("model.h5")
        print("model saved to model.h5 and model1.json")

if __name__ == '__main__':
    if len(sys.argv) > 1:
        trainingmodel(sys.argv[1])
    else:
        json_file = open("model1.json", 'r')
        lmodel = json_file.read()
        json_file.close()
        loaded_model = model_from_json(lmodel)
        loaded_model.load_weights("model.h5")
        loaded_model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
    app.run(port=8081, debug=False)


from PIL import Image as Image
from PIL import ImageFilter as IFilter
from PIL import ImageEnhance as IEnhance
import numpy as np

path="D:\imageInfo"
imageName="\\apple.jpg"
img=Image.open(path+imageName)
print("图片格式："+img.format)
print(""+img.mode)
print(img.size)

'保存图片信息'
img.save(path+"\save.jpg")
'重置大小'
reSizeImage=img.resize((256,256))
reSizeImage.save(path+"\\reSize.jpg")
'旋转'
img_rote=img.rotate(30)
img_rote.save(path+"\img_rote.png")
'灰度处理   1，L，P，RGB，RGBA，CMYK，YCbCr,I，F '
vert_img=img.convert("L")

img_arr=np.array(vert_img)
print("shape: ",img_arr.shape)
print(img_arr)
vert_img.save(path+"\\vert_img.jpg")

'''
输入图像为 (H,W)
卷积核为 （N,N）
输出图像为：A , B
对图像卷积：
采用valid模式：不添加padding 那么输出的结果图像为：A=H-N+1,B=W-N+1
采用same模式：需要添加padding，一般添加0作为填充，上下左右都需要填充，根据A=H ,B=W
计算出添加的padding层数为：H+2p-N+1=H =>p=(N-1)/2
'''

def num_conv(path,name,padding,core_arr):
    img=Image.open(path+name)
    r,g,b=img.split()
    img_arr=np.array(r)
    o_h,o_w=img_arr.shape
    img_h,img_w=0,0
    if padding=='valid':
        img_h=o_h-len(core_arr)
        img_w=o_w-len(core_arr[0])
        print("核卷积的shape为：",(img_h,img_w))

    else:
        print("else")
print("finish")

'把图像分解为rgb  然后 合并的'

def deal_img(path,name):
    img=Image.open(path+name)
    r,g,b=img.split()
    r.save(path+"\\r.jpg")
    g.save(path + "\\g.jpg")
    b.save(path + "\\b.jpg")
    res_img=Image.merge('RGB',[r,g,b])
    res_img.save(path+"\\result_img.jpg")

'''

'''

if __name__ =='__main__':
    # core_arr=[[1,0,1],
    #           [1,0,1],
    #           [1,0,1]]
    path = "D:\imageInfo"
    imageName = "\\apple.jpg"
    # num_conv(path,imageName,'valid',core_arr)
    deal_img(path,imageName)


import pickle
import argparse
import neat

from specialist_neat_training import Individual, EvomanEnvironment

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--best', help='full path to best player', type = str)
    args = parser.parse_args()

    # SETTINGS!
    FILE = args.best
    config_vars = [int(i[0]) for i in FILE.replace(".pkl", "").split('-')[-3:]]
    RUNS = config_vars[0]
    ENEMIES = config_vars[1]
    INDIVIDUAL_TYPE = config_vars[2]

    # Load configuration file.
    if INDIVIDUAL_TYPE == 1:
        config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                             neat.DefaultSpeciesSet, neat.DefaultStagnation,
                             "neat.config")
    else:
        config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         "neat_fixed_topology.config")

    # Load (best) genome.
    with open(FILE, "rb") as f:
        genome = pickle.load(f)
    print(f'{FILE} succesfully loaded with config:\nEnemy type {ENEMIES}\nNumber of runs: {RUNS}\nIndividual type: {INDIVIDUAL_TYPE}')

    # Run game
    env = EvomanEnvironment(ENEMIES, RUNS, Individual=Individual)
    while True:
        fitness, gain = env.evaluate_individual(genome, config, show=True)
        print(fitness, gain)


from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def click_me(event=''):
    s = E.get()
    if s == 'tia':
        print(s)
        print('good')
        messagebox.showinfo('message', 'You got it')
    else:
        print(s)
        print('bad')
        messagebox.showinfo('message', 'Invalid input')

class entry:
    def __init__(self, master):
        master.title('Label')
        master.geometry('200x200')
        global E
        E = ttk.Entry(master)
        E.pack()
        E.focus()
        E.bind('<Return>', click_me)

        B = ttk.Button(master, text='click me', command=click_me)
        B.pack()

root = Tk()
app = entry(root)
root.mainloop()


class Solution(object):
    def maxSlidingWindow(self, nums, k):
        dequeue = []
        result = []
        for idx in range(len(nums)):
            while dequeue and nums[dequeue[-1]] < nums[idx]:
                dequeue.pop()
            dequeue += [idx]
            if idx + 1 < k:
                continue
            while dequeue[-1] - dequeue[0] + 1 > k:
                dequeue.pop(0)
            result += [nums[dequeue[0]]]
        return result


#!/usr/bin/python3

import fs
import argparse
import config as conf
from paths import paths as p
import util
import ports
import hosts as dns
import apache

defaults = conf.config('defaults').get()
p = p()
apache = apache.apache()

parser = argparse.ArgumentParser()
parser.add_argument('site', metavar='Site', help='Name of the site', nargs=1)
parser.add_argument('-p', '--port', metavar='Port', default=defaults['port'], help='Port for the site', nargs=1)
parser.add_argument('-t', '--tld', metavar='TLD', default=defaults['tld'], help='Top Level Domain', nargs=1)
parser.add_argument('-r', '--no-restart', dest='restart', help='Prevent the restart httpd action', action='store_false')
parser.add_argument('-d', '--disable', help='Disables the site after her creation (-r implicit)', action='store_true')
parser.add_argument('-w', '--hosts', help='Creates a DNS in the server: site.tld->127.0.0.1', action='store_true')

a = parser.parse_args()

site 	= a.site[0]
port 	= a.port[0]
tld  	= a.tld[0]
restart = a.restart
disable = a.disable
hosts	= a.hosts

if tld[0] is not '.':
	tld = '.'+tld

if disable:
	restart = False

domain=site+tld

# Create site
if fs.touch(p.get('asites')+domain+conf.ext('apache')):
	# sites_available
	tpl = fs.getFileContent(p.get('config')+'tpl.conf').format(port=port, sites=p.get('sites'), domain=domain, error=p.get('error'), exterror=conf.ext('error'), log=p.get('log'), extlog=conf.ext('log'))
	fs.setFileContent(p.get('asites')+domain+conf.ext('apache'), tpl)
	# error
	fs.touch(p.get('error')+domain+conf.ext('error'))
	# log
	fs.touch(p.get('log')+domain+conf.ext('log'))
	# sites/{site}/
	fs.mkdir(p.get('sites')+domain)
	# ports
	ports.ports(port).add()

	# apache
	if restart:
		apache.restart()
	# enable
	if not disable:
		apache.ensite(domain)
	# hosts
	if hosts:
		h = dns.host(domain)
		if not h.exist():
			h.add()

else:
	util.error('Site \"'+site+'\" already exist', None, 'csite')


'''
@author Swarbhanu Chatterjee
@file ion/processes/data/transforms/ctdbp/ctdbp_L0.py
@description Transforms incoming CTD parsed data into L0 products to send out through the L0 stream
'''

from pyon.core.exception import BadRequest
from pyon.public import log

from ion.core.process.transform import TransformDataProcess
from ion.services.dm.utility.granule.record_dictionary import RecordDictionaryTool
from ion.core.function.transform_function import MultiGranuleTransformFunction
from interface.services.dm.ipubsub_management_service import PubsubManagementServiceProcessClient

class CTDBP_L0_all(TransformDataProcess):
    """
    L0 listens to the parsed and pulls only C, T, & Pressure from the parsed and puts it onto the L0 stream. The other parameters are dropped.
    """
    def on_start(self):
        super(CTDBP_L0_all, self).on_start()

        self.L0_stream_id = self.CFG.process.publish_streams.values()[0]

        log.debug("the output stream: %s", self.L0_stream_id)

        pubsub = PubsubManagementServiceProcessClient(process=self)
        self.stream_def_L0 = pubsub.read_stream_definition(stream_id=self.L0_stream_id)

        self.params = {'L0_stream' : self.stream_def_L0._id }

        log.debug("the params: %s", self.params)

    def recv_packet(self, packet,stream_route, stream_id):
        """Processes incoming data!!!!
            @param packet granule
            @param stream_route StreamRoute
            @param stream_id str
        """

        log.debug("CTDBP_L0_all received in the stream, %s, the packet: %s", stream_id, packet)

        if packet == {}:
            return
        granules = ctdbp_L0_algorithm.execute([packet], params=self.params)
        for granule in granules:
            self.publisher.publish(msg=granule)

class ctdbp_L0_algorithm(MultiGranuleTransformFunction):

    @staticmethod
    @MultiGranuleTransformFunction.validate_inputs
    def execute(input=None, context=None, config=None, params=None, state=None):
        """
        @param input granule
        @retval result_list list of dictionaries containing granules as values
        """

        result_list = []
        for x in input:
            rdt = RecordDictionaryTool.load_from_granule(x)

            conductivity = rdt['conductivity']
            pressure = rdt['pressure']
            temperature = rdt['temperature']
            time = rdt['time']

            # build the granule for conductivity, temperature and pressure
            granule = ctdbp_L0_algorithm._build_granule(stream_definition_id= params['L0_stream'],
                field_names= ['conductivity', 'pressure', 'temperature', 'time'], # these are the field names for the output record dictionary
                values= [conductivity, pressure, temperature, time])

            result_list.append(granule)

        return result_list

    @staticmethod
    def _build_granule(stream_definition_id=None, field_names=None, values=None):
        """
        Builds a granule with values corresponding only to the params specified in the field names

        @param param_dictionary ParameterDictionary
        @param field_name str
        @param value numpy.array

        @retval Granule
        """
        root_rdt = RecordDictionaryTool(stream_definition_id=stream_definition_id)
        zipped = zip(field_names, values)

        for k,v in zipped:
            root_rdt[k] = v

        return root_rdt.to_granule()


import serial
import time
import logging

PORT = 'COM4'

class Pilot:
    dict_of_codes = {"On\Off": {"Position": (3, 0), "Hex code": ("0x...", )},
                     "Volume Up": {"Position": (1, 1), "Hex code": ("0x...", )},
                     "Volume Down": {"Position": (2, 1), "Hex code": ("0x...", )},
                     "Canal Up": {"Position": (1, 0), "Hex code": ("0x...", )},
                     "Canal Down": {"Position": (2, 0), "Hex code": ("0x...", )},
                     "Mute": {"Position": (3, 1), "Hex code": ("0x...", )}}

    def __init__(self, dict_of_codes=dict_of_codes):
        self.dict_of_codes = dict_of_codes  # put here list of codes, to send to pilot (maybe dict better)
        self.ser = ""
        self.written = ""
        """
        if self.open_serial_port(self, PORT) is False:  # if connection established, allow sending signal buttons
            self.send_button_signal(self, self.dict_of_codes["Mute"])
        """

    def open_serial_port(self, port):

        """ Open a connection to the serial port.
        Args:
            self: class parameter
            port: the number of port (trying to open)
        Returns:
            True if connection established, False otherwise
        Raises:
            False: if connection is not established

        >>> self.open_serial_port(4)
        False
        """
        try:
            self.ser = serial.Serial(port)
            return True
        except Exception as err:
            print("{}".format(err))
            return False

    @staticmethod
    def send_raw_button_signal(self, button_pressed):
        try:
            self.timer(0.5)  # sleep for desired time in seconds (from function timer)
            # self.written = self.ser.write(button_pressed)
            # self.ser.close()  # close serial connection
            print("Button pressed: {0} \nHex code: {1}".format(button_pressed, self.dict_of_codes[button_pressed]))

        except Exception as err:
            print("An error {} occured!".format(err))

    @staticmethod
    def timer(t_sleep):
        return time.sleep(t_sleep)


import sys
sys.stdin = open("중위순회.txt")

def inorder(tree):
    if tree:
        inorder(G[tree][2])
        print(G[tree][1], end='')
        inorder(G[tree][3])

T = 10
for tc in range(1, T+1):
    V = int(input())
    G = [[0, 0, 0, 0]]
    for i in range(V):
        arr = list(input().split())
        if len(arr) != 4:
            for i in range(4-len(arr)):
                arr.append('0')
        for i in range(4):
            if i != 1:
                arr[i] = int(arr[i])
        G.append(arr)
    print('#{}'.format(tc), end=' ')
    inorder(1)
    print()


import cv2
import time
from simple_pid import PID
from mytellopy import Tello

from morse import CameraMorse
from PoseModule import PoseModule
from Utils import FPS, stackImages

TESTING = 1
# HEIGHT, WIDTH = 360, 480
HEIGHT, WIDTH = 180, 240
CY, CX = HEIGHT // 2, WIDTH // 2

class TelloController:
    """
    TelloController builds velocity controls
    and pose commands over mytellopy class
    """

    def __init__(self, tracking=True):

        # yv : yaw, rl : roll, fb : pitch, ud : throttle
        self.speed_def = {"yv": 50, "rl": 30, "fb": 30, "ud": 30}
        self.speed_curr = {"yv": 0, "rl": 0, "fb": 0, "ud": 0}
        self.speed_prev = self.speed_curr.copy()
        self.speed_null = self.speed_curr.copy()

        self.drone = Tello()
        self.drone.connect()
        self.drone.start_video_feed()

        self.pd = PoseModule(face=False)
        self.FPS = FPS()

        self.tracking = None
        self.searching = None
        self.scheduled_picture = None
        self.scheduled_takeoff = None
        self.scheduled_throwgo = None
        self.scheduled_landing = None
        self.scheduled_palmland = None

        self.tilt = -1
        self.distance = 300
        self.pose = None

        self.pid_speed_fb = PID(
            Kp=0.1,
            Ki=0.1,
            Kd=0.1,
            setpoint=self.distance,
            sample_time=0,
            output_limits=(-100, 100),
            auto_mode=True,
        )
        self.pid_speed_yv = PID(
            Kp=0.1,
            Ki=0.1,
            Kd=0.1,
            setpoint=CX,
            sample_time=0,
            output_limits=(-100, 100),
            auto_mode=True,
        )
        self.pid_speed_ud = PID(
            Kp=0.1,
            Ki=0.1,
            Kd=0.1,
            setpoint=CY,
            sample_time=0,
            output_limits=(-100, 100),
            auto_mode=True,
        )

        self.morse = CameraMorse()
        # self.morse.define_command("---", self.scheduled_takeoff)
        # self.morse.define_command("...", self.scheduled_throwgo)

    def get_stream(self):
        """
        returns the raw stream from the drone
        """
        self.fps = self.FPS.update()
        self.stream_raw = self.drone.get_frame()
        self.stream_raw = cv2.resize(self.stream_raw, (WIDTH, HEIGHT))
        self.stream_pro = self.pd.processPose(self.stream_raw)
        self.stream_trm = self.pd.processBBox(self.stream_raw)

    def process_stream(self):
        pose, target, distance, tilt = self.pd.getInfo()

        try:
            self.pose = pose
            tilt = (tilt // 15) * 15
            distance = (distance // 10) * 10
            target[0] = (target[0] // 10) * 10
            target[1] = (target[1] // 10) * 10
        except:
            tilt = None
            distance = None

        if not self.drone.is_flying:
            morse_code = self.morse.eval(self.stream_raw)
            if morse_code == "...":
                self.scheduled_throwgo = True
            elif morse_code == "---":
                self.scheduled_takeoff = True
        elif self.drone.battery < 20:
            self.drone.set_speed(self.speed_null)
            self.scheduled_landing = time.time() + 10

        if self.scheduled_takeoff:
            self.drone.set_speed(self.speed_null)
            if self.scheduled_takeoff > time.time():
                self.drone.takeoff()
                self.tracking = True
                self.scheduled_takeoff = None

        elif self.scheduled_throwgo:
            self.drone.set_speed(self.speed_null)
            if self.scheduled_throwgo > time.time():
                self.drone.takeoff()
                self.tracking = True
                self.scheduled_throwgo = None

        elif self.scheduled_picture:
            self.drone.set_speed(self.speed_null)
            if self.scheduled_picture > time.time():
                self.drone.take_picture()
                self.tracking = True
                self.scheduled_picture = None

        elif self.scheduled_landing:
            self.drone.set_speed(self.speed_null)
            if self.scheduled_landing > time.time():
                self.drone.land()
                self.tracking = False
                self.scheduled_landing = None

        elif self.scheduled_palmland:
            self.drone.set_speed(self.speed_null)
            if distance > 270:
                self.drone.palm_land()
                self.tracking = False
                self.scheduled_palmland = None
            else:
                self.speed_curr["fb"] = 20
                self.drone.set_speed(self.speed_curr)

        elif self.tracking == True and target is None:
            # rotate to find target
            self.searching = True
            self.speed_curr["yv"] = 30
            self.drone.set_speed(self.speed_curr)

        elif self.tracking == True and target is not None:

            self.searching = False

            # use pid to keep target at center
            self.speed_curr["yv"] = self.pid_speed_yv(target[0])
            self.speed_curr["ud"] = self.pid_speed_ud(target[1])

            # update defalut distance when tilt changes
            if self.tilt != tilt:
                self.distance = distance
                self.tilt = tilt
                self.pid_speed_fb.setpoint = self.distance

            # use pid to maintain distance
            self.speed_curr["fb"] = self.pid_speed_fb(distance)

            # pose control
            if pose is None:
                self.speed_curr["rl"] = 0
            elif pose == "BOTH_HANDS_UP":
                self.scheduled_picture = time.time() + 3
            elif pose == "LEFT_HAND_ROCKON":
                self.scheduled_landing = time.time() + 1
            elif pose == "RIGHT_HAND_ROCKON":
                self.scheduled_palmland = time.time() + 1
            elif pose == "LEFT_HAND_OPEN":
                self.speed_curr["rl"] = self.speed_def["rl"]
            elif pose == "RIGHT_HAND_OPEN":
                self.speed_curr["rl"] = -self.speed_def["rl"]
            elif pose == "LEFT_HAND_CLOSE":
                self.tilt = -1
                self.speed_curr["fb"] = -self.speed_def["fb"]
            elif pose == "RIGHT_HAND_CLOSE":
                self.tilt = -1
                self.speed_curr["fb"] = self.speed_def["fb"]

            # send speed to drone
            self.drone.set_speed(self.speed_curr)

        self.frame = self.write_hud(self.stream_raw)

        self.speed_curr = self.speed_null.copy()

        return self.frame

    def write_hud(self, frame):
        """
        writes the hud to the frame
        """
        frame = frame.copy()

        RED = (0, 0, 255)
        GREEN = (0, 255, 0)
        BLUE = (255, 0, 0)
        PURPLE = (128, 0, 128)
        CYAN = (0, 255, 255)
        YELLOW = (255, 255, 0)

        class HUD:
            def __init__(self):
                self.infos = []

            def add(self, info, color=CYAN):
                self.infos.append((info, color))

            def draw(self, frame):
                i = 0
                for i, (info, color) in enumerate(self.infos):
                    cv2.putText(
                        frame,
                        info,
                        (0, 30 + (i * 30)),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        1.0,
                        color,
                        2,
                    )  # lineType=30)

        hud = HUD()

        hud.add(f"FPS {self.fps}", PURPLE)
        hud.add(f"BAT {self.drone.battery}", PURPLE)
        hud.add(
            f"{'FLYING' if self.drone.is_flying else 'NOT_FLYING'}",
            GREEN if self.drone.is_flying else RED,
        )
        hud.add(
            f"TRACKING {'ON' if self.tracking else 'OFF'}",
            GREEN if self.tracking else RED,
        )

        if self.speed_curr["yv"] >= 0:
            hud.add(f"CW {self.speed_curr['yv']}", GREEN)
        elif self.speed_curr["yv"] < 0:
            hud.add(f"CCW {-self.speed_curr['yv']}", RED)
        if self.speed_curr["rl"] >= 0:
            hud.add(f"RIGHT {self.speed_curr['rl']}", GREEN)
        elif self.speed_curr["rl"] < 0:
            hud.add(f"LEFT {-self.speed_curr['rl']}", RED)
        if self.speed_curr["fb"] >= 0:
            hud.add(f"FORWARD {self.speed_curr['fb']}", GREEN)
        elif self.speed_curr["fb"] < 0:
            hud.add(f"BACKWARD {-self.speed_curr['fb']}", RED)
        if self.speed_curr["ud"] >= 0:
            hud.add(f"UP {self.speed_curr['ud']}", GREEN)
        elif self.speed_curr["ud"] < 0:
            hud.add(f"DOWN {-self.speed_curr['ud']}", RED)

        hud.add(f"POSE: {self.pose}", GREEN if self.pose else RED)
        hud.add(f"distance: {self.distance}", BLUE)
        hud.add(f"tilt: {self.tilt}", BLUE)

        if self.scheduled_picture:
            hud.add(f"picture in: {self.scheduled_picture - time.time()}")
        if self.scheduled_landing:
            hud.add(f"landing in: {self.scheduled_landing - time.time()}")
        if self.scheduled_palmland:
            hud.add(f"palmland in: {self.scheduled_palmland - time.time()}")
        if self.scheduled_throwgo:
            hud.add(f"throw in: {self.scheduled_throwgo - time.time()}")
        if self.scheduled_takeoff:
            hud.add(f"takeoff in: {self.scheduled_takeoff - time.time()}")
        if self.searching:
            hud.add("Searching for person..", PURPLE)

        hud.draw(frame)
        return frame

def main():

    # detector = PoseModule(face=False)
    # fps = FPS()
    drone = TelloController()

    print("///////////////////////////")
    print("started streaming from main")
    print("///////////////////////////")
    while True:

        drone.get_stream()
        img = drone.process_stream()

        # cv2.putText(
        #     img_trimmed,
        #     str(pose) + str(target) + str(distance),
        #     (10, 30),
        #     cv2.FONT_HERSHEY_SIMPLEX,
        #     0.5,
        #     (0, 0, 255),
        #     2,
        # )

        # imgList = [img, img_processed, img_trimmed]
        # cv2.imshow("Image", stackImages(imgList, 2, 1))

        cv2.imshow("img", img)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            cv2.destroyAllWindows()
            break

if __name__ == "__main__":
    main()


from glob import glob
import pandas as pd
from Bio import Entrez
from ete3 import NCBITaxa
from collections import defaultdict
import numpy as np

Entrez.email = "joshua.batson@czbiohub.org"

# load ncbi taxonomy database
ncbi = NCBITaxa()

def load_lca():
    nr_df = load_blast_output(sorted(glob('/Users/josh/src/skeeters/data/s3/'
                                          'contig_quality/*/blast_lca_nr_filtered.m9')))
    nt_df = load_blast_output(sorted(glob('/Users/josh/src/skeeters/data/s3/'
                                          'contig_quality/*/blast_lca_nt_filtered.m9')))

    # Get all taxids present in or ancestors of our dataset
    taxids = set(nr_df.taxid).union(nt_df.taxid)
    taxids = {t for taxid in taxids for t in ncbi.get_lineage(taxid)}

    taxid2name = ncbi.get_taxid_translator(taxids)
    taxid2name[-1] = 'Ambiguous'
    taxid2rank = ncbi.get_rank(taxids)
    def taxid2kingdom(taxid):
        lineage = ncbi.get_rank(ncbi.get_lineage(taxid))
        for k, v in lineage.items():
            if v == 'superkingdom':
                kingdom = taxid2name.get(k)
                return kingdom

    for df in [nt_df, nr_df]:
        df['name'] = df['taxid'].map(taxid2name)
        df['rank'] = df['taxid'].map(taxid2rank)
        df['kingdom'] = df['taxid'].map(taxid2kingdom)

    lca_df = merge_nr_nt(nr_df, nt_df)

    return lca_df

def merge_nr_nt(nr_df, nt_df):
    lca_df = nr_df.merge(nt_df, how='outer', on='contig_key', suffixes= ('_nr', '_nt'))

    lca_df.loc[lca_df['bitscore_nr'].isna(), 'bitscore_nr'] = 0
    lca_df.loc[lca_df['bitscore_nt'].isna(), 'bitscore_nt'] = 0

    selector = lca_df['bitscore_nr'] < lca_df['bitscore_nt']

    lca_df['db'] = np.where(selector, 'nt', 'nr')

    for field in ['contig', 'sample', 'name', 'rank', 'taxid', 'kingdom', 'align_length', 'identity', 'bitscore']:
        lca_df[field] = np.where(selector,
                                   lca_df[field + '_' + 'nt'],
                                   lca_df[field + '_' + 'nr'])

    lca_df['align_length'] = np.where(selector, lca_df['align_length'], lca_df['align_length'] * 3)

    return lca_df

def load_blast_output(files):
    def get_sample_from_path(path):
        return path.split('contig_quality/')[1].split('/')[0]

    dfs = []

    for file in files:
        df = pd.read_csv(file, sep='\t')
        df['sample'] = get_sample_from_path(file)
        dfs.append(df)

    df = pd.concat(dfs, axis=0)
    df = df.rename(columns={'query': 'contig'})
    df['contig_key'] = df['sample'] + '~' + df['contig']
    return df

def get_kingdom(taxid, taxid2name):
    if taxid is None:
        return None
    lineage = ncbi.get_rank(ncbi.get_lineage(taxid))
    for k, v in lineage.items():
        if v == 'superkingdom':
            kingdom = taxid2name.get(k)
            return kingdom

rank_list = ['superkingdom',
            'kingdom',
            'subkingdom',
            'superphylum', 'phylum', 'subphylum'
            'superclass', 'class', 'subclass', 'infraclass',
            'cohort', 'subcohort',
            'superorder', 'order', 'parvorder', 'suborder', 'infraorder',
            'section',
            'superfamily', 'family', 'subfamily',
            'tribe', 'subtribe',
            'genus', 'subgenus',
            'section',
            'species group', 'species subgroup', 'species', 'subspecies',
            'varietas', 'forma']

def rank_to_int(rank):
    """Gives an integer to each rank, such that i < j means j is more specific."""
    if rank in rank_list:
        return rank_list.index(rank)
    else:
        return -1

def member_key(member):
    return member['sample'] + '~' + member['contig']

def parse_cdhit_row(row):
    if '*' in row:
        index, length, name, percent_id = row.split()
        percent_id_sign, percent_id = '0', 100
        is_ref = True
    else:
        index, length, name, _, percent_id = row.split()
        is_ref = False
    length = int(length.strip(',nt'))
    name = name.strip('>').strip('.')
    sample, contig = name.split('~')
    coverage = float(contig.split('_')[-1])
    if not is_ref:
        percent_id_sign, percent_id = percent_id.strip('%').split('/')
        percent_id = float(percent_id)
    return {'contig':contig,
            'sample':sample,
            'length':length,
            'percent_id':percent_id,
            'percent_id_sign':percent_id_sign,
            'coverage':coverage,
            'is_ref':is_ref}

def load_cdhit_clusters(filename):
    clusters = defaultdict(list)
    with open(filename, 'r') as file:
        for line in file:
            if line.startswith('>Cluster'):
                cluster_id = line.split()[-1]
            else:
                member = parse_cdhit_row(line)
                clusters[cluster_id].append(member)
    return clusters

def get_cluster_rep(cluster):
    for member in cluster:
        if member['is_ref']:
            return member

def get_cluster_kingdom(cluster):
    taxid_list = [member.taxid for member in cluster]
    kingdoms = [get_kingdom(taxid) for taxid in taxid_list]
    kingdoms = [k for k in kingdoms if k is not None]
    if len(kingdoms) > 0:
        return mode(kingdoms)
    else:
        return None

def merge_clusters_lca(clusters, lca_df):
    cluster_ids = [int(id) for id in clusters]
    cluster_reps = [get_cluster_rep(clusters[id]) for id in clusters]
    cluster_lengths = [rep['length'] for rep in cluster_reps]
    cluster_sizes = [len(clusters[id]) for id in clusters]
    cluster_contig_keys = [member_key(rep) for rep in cluster_reps]

    df = pd.DataFrame({'contig_key': cluster_contig_keys,
                             'cluster': cluster_ids,
                             'cluster_size': cluster_sizes,
                             'contig_length': cluster_lengths})

    df = df.merge(lca_df, on='contig_key', how='inner')

    df['align_percent'] = np.round(100*df['align_length']/df['contig_length'], 1)
    df['percent_identity'] = np.round(df['identity'], 1)

    df = df[['cluster', 'cluster_size', 'contig_length',
                         'name', 'rank', 'kingdom', 'taxid', 'db', 'align_length',
                         'align_percent', 'percent_identity', 'bitscore',
                        'contig', 'sample', 'contig_key']]
    df = df.sort_values('cluster')

    return df

def add_lca_to_clusters(clusters, lca_df):
    contig_to_lca = dict(zip(lca_df['contig_key'], lca_df['taxid']))
    for id, cluster in clusters.items():
        for member in cluster:
            taxid = contig_to_lca.get(member_key(member))
            member['taxid'] = taxid
            member['name'] = taxid2name.get()
            member['name'] = taxid2name.get()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
import scrapy
import sys
import scrapy_splash
from scrapy.http import Request
from cloud_price.items import AliyunItem
from cloud_price.utils import tx

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

log = logging.getLogger(__name__)

class AliyunSpider(scrapy.spiders.Spider):
    name = "aliyun"
    allowed_domains = ["aliyun.com"]
    start_urls = [
        "https://www.aliyun.com/product/ecs",
    ]

    splash_args = {
        'wait': 0.5,
        # 'http_method': 'GET',
        # 'html': 1,
        # 'png': 1,
        # 'width': 600,
        # 'render_all': 1
    }

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy_splash.SplashRequest(url, self.parse, endpoint='render.html',
                                              args=self.splash_args)

    def parse(self, response):
        log.info(u'----------aliyun crawl begin-----------')

        instance_type_panes = response.xpath(
            '//div[@class="instanceContainer"]//div[@class="instanceTypePane"]')
        # for idx,instance_type_pane in enumerate(instance_type_panes):
        for instance_type_pane in instance_type_panes:
            item = AliyunItem()
            item['name'] = tx(instance_type_pane.xpath(
                'span[@class="introArea"]/div[@class="infoArea"]/div[@class="titleContext"]/text()'))
            item['price'] = tx(instance_type_pane.xpath(
                'span[@class="detailArea"]//div[@class="priceDom"]/span[@class="valueLabel"]/span/text()'))
            fits = instance_type_pane.xpath(
                'span[@class="introArea"]/div[@class="infoArea"]/div[@class="fitForContext"]/ul/li[@class="fitForItem"]')
            fit_for_item = ""
            for fit in fits:
                li = tx(fit.xpath('span[2]/text()'))
                if li:
                    fit_for_item = fit_for_item + li + ";"

            item['fit'] = fit_for_item

            card_content = instance_type_pane.xpath(
                'span[@class="detailArea"]//div[@class="instanceFamilyProperty"]/div[@class="cardContent"]')
            item['cpu_mem'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "vCpuMemory")]//span[@class="valueLabel"]/text()'))
            item['cpu_type'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "cpuType")]//div[@class="valueDom"]//text()'))
            item['max_gbps'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "maxGbps")]//div[@class="valueDom"]//text()'))
            item['sys_disk'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "sysDisk")]//div[@class="valueDom"]//text()'))
            item['gpu_fpga'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "GPU/FPGA")]//div[@class="valueDom"]//text()'))
            item['max_pps'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "maxPPS")]//div[@class="valueDom"]//text()'))
            item['data_disk'] = tx(card_content.xpath(
                'div[@class="familyPropertyCard" and contains(@data-reactid, "dataDisk")]//div[@class="valueDom"]//text()'))

            if item['name']:
                item.output()

            yield item

        # # 获取所有的url，继续访问，并在其中寻找相同的url
        # all_urls = response.xpath('//a/@href').extract()
        # for url in all_urls:
        #     if url.startswith('http://www.xiaohuar.com/list-1-'):
        #         yield Request(url, callback=self.parse)

        log.info(u'---------------success----------------')


import celery
import pytz
from datetime import MINYEAR
from dateutil.rrule import (
    rrule as dateutil_rrule,
    YEARLY,
    MONTHLY,
    WEEKLY,
    DAILY,
    HOURLY,
    MINUTELY,
    SECONDLY
)
try:  # celery 4.x
    from celery.schedules import BaseSchedule as schedule
except ImportError:  # celery 3.x
    from celery.schedules import schedule

# from celery.utils.log import get_logger
# logger = get_logger(__name__)

class rrule(schedule):
    RRULE_REPR = (
        '<rrule: freq: {0.freq}, dtstart: {0.dtstart}, interval: {0.interval}, '
        'wkst: {0.wkst}, count: {0.count}, until: {0.until}, bysetpos: {0.bysetpos}, '
        'bymonth: {0.bymonth}, bymonthday: {0.bymonthday}, byyearday: {0.byyearday}, '
        'byeaster: {0.byeaster}, byweekno: {0.byweekno}, byweekday: {0.byweekday}, '
        'byhour: {0.byhour}, byminute: {0.byminute}, bysecond: {0.bysecond}, '
        'tzid: {0.tzid}>'
    )

    FREQ_MAP = {
        'YEARLY': YEARLY,
        'MONTHLY': MONTHLY,
        'WEEKLY': WEEKLY,
        'DAILY': DAILY,
        'HOURLY': HOURLY,
        'MINUTELY': MINUTELY,
        'SECONDLY': SECONDLY
    }

    def __init__(self, freq, dtstart=None,
                 interval=1, wkst=None, count=None, until=None, bysetpos=None,
                 bymonth=None, bymonthday=None, byyearday=None, byeaster=None,
                 byweekno=None, byweekday=None,
                 byhour=None, byminute=None, bysecond=None, tzid='UTC',
                 **kwargs):
        super(rrule, self).__init__(**kwargs)

        if type(freq) == str:
            freq_str = freq.upper()
            assert freq_str in rrule.FREQ_MAP
            freq = rrule.FREQ_MAP[freq_str]

        # Strip timezone data from dates. This is done because start/end times
        # are always assumed to be naive datetimes in the timezone defined by tzid.
        # Adjustments for DST are handled by normalizing the local time to UTC,
        # which accounts for variable offsets caused by DST.
        rrule_tz = pytz.timezone(tzid)
        if dtstart:
            dtstart = dtstart.replace(tzinfo=None)
        else:
            dtstart = pytz.utc.localize(self.now().replace(tzinfo=None)) \
                .astimezone(rrule_tz).replace(tzinfo=None)
        if until:
            until = until.replace(tzinfo=None)

        self.freq = freq
        self.dtstart = dtstart
        self.interval = interval
        self.wkst = wkst
        self.count = count
        self.until = until
        self.bysetpos = bysetpos
        self.bymonth = bymonth
        self.bymonthday = bymonthday
        self.byyearday = byyearday
        self.byeaster = byeaster
        self.byweekno = byweekno
        self.byweekday = byweekday
        self.byhour = byhour
        self.byminute = byminute
        self.bysecond = bysecond
        self.tzid = tzid
        self.rrule_tz = rrule_tz
        self.rrule = dateutil_rrule(freq, dtstart, interval, wkst, count, until,
                                    bysetpos, bymonth, bymonthday, byyearday, byeaster,
                                    byweekno, byweekday, byhour, byminute, bysecond)

    def remaining_estimate(self, last_run_at):
        # last_run_at is UTC. Convert to specified timezone for comparison with rrule.
        last_run_at_utc = pytz.utc.localize(last_run_at.replace(tzinfo=None))
        if last_run_at_utc.year == MINYEAR:
            # Redbeat uses a date with year == datetime.MINYEAR if the job was never run.
            # Since converting to local TZ may go out of range if this is the case,
            # don't convert it.
            last_run_at_local = last_run_at_utc
        else:
            last_run_at_local = last_run_at_utc.astimezone(self.rrule_tz)
        # Remove tzinfo from last_run_at_local
        # because rrule's dtstart does not have tzinfo.
        next_run = self.rrule.after(last_run_at_local.replace(tzinfo=None))
        if next_run:
            next_run_local = self.rrule_tz.localize(next_run)
            # Get UTC time of next run
            next_run_utc = pytz.utc.normalize(next_run_local)
            # Get delta between now and next run (using UTC times)
            now_utc = pytz.utc.localize(self.now().replace(tzinfo=None))
            delta = next_run_utc - now_utc
            return delta
        return None

    def is_due(self, last_run_at):
        rem_delta = self.remaining_estimate(last_run_at)
        if rem_delta is not None:
            rem = max(rem_delta.total_seconds(), 0)
            due = rem == 0
            if due:
                rem_delta = self.remaining_estimate(self.now())
                if rem_delta is not None:
                    rem = max(rem_delta.total_seconds(), 0)
                else:
                    rem = None
            return celery.schedules.schedstate(due, rem)
        return celery.schedules.schedstate(False, None)

    def __repr__(self):
        return self.RRULE_REPR.format(self)

    def __reduce__(self):
        return (self.__class__, (self.rrule), None)


import sys
import re

class Solution:

    def diffWaysToCompute(self, myinput):
        mydict={}
        processed=[]
        myinput=re.split('(\D)', myinput)
        print(myinput)

        for x in range(0,len(myinput),2):
            myinput[x]=int(myinput[x])

        res=self.diffWaysToComputeHelper(myinput,0,len(myinput)-1,mydict)
        return res

    def compute(self,op1,ch,op2):
        sol=-1
        if(ch=='*'):
            sol=op1*op2
        elif(ch=='+'):
            sol=op1+op2
        elif(ch=='-'):
            sol=op1-op2
        return sol

    def diffWaysToComputeHelper(self, myinput,s,e,mydict):
        tup=(s,e)
        if(tup in mydict):
            return mydict[tup]
        solutions=[]
        if(e==s):
            solutions.append(myinput[e])
        else:
            for cut in range(s+1,e,2):
                op=myinput[cut]
                leftSet=self.diffWaysToComputeHelper(myinput,s,cut-1,mydict)
                rightSet=self.diffWaysToComputeHelper(myinput,cut+1,e,mydict)
                for n in leftSet:
                    for m in rightSet:
                        solutions.append(self.compute(n,op,m))
        mydict[tup]=solutions
        return solutions
s=Solution()
print(s.diffWaysToCompute("11+1+2-5-4*2"));


from __future__ import print_function
import random
import re
import string

class Chatbot(object):
    def __init__(self, pairs):
        self._pairs = pairs
        self._confused_response = "I am afraid I did not understand that. You can ask me about Jaime's college experience or his work at Google and IBM or about the meaning of life."
        self._logger = ""
        self._unixCommands = set(["ls", "pwd", "cd", "touch", "cat", "man", "help"])

    def isUnixCommand(self, query):
        queryArr = query.split()
        return queryArr[0] in self._unixCommands

    def unixResponse(self, dictionary):
        query = dictionary.get('q', [''])[0]
        context = dictionary.get('t', [''])[0]
        directory = dictionary.get('dir', [''])[0]
        queryArr = query.split()
        unixCommand = queryArr[0]
        response = "Unix\n"
        if(unixCommand == "ls"):
            response += showFiles(directory)
        elif(unixCommand == "pwd"):
            response += directory
        return response

    def respond(self, dictionary):
        """
        Generate a response to the user input.

        :type str: str
        :param str: The string to be mapped
        :rtype: str
        """
        query = dictionary.get('q', [''])[0]
        context = dictionary.get('t', [''])[0]
        directory = dictionary.get('dir', [''])[0]
        if(query and self.isUnixCommand(query)):
            return self.unixResponse(dictionary)
        if(context in self._pairs):
            contextPair = [(re.compile(x, re.IGNORECASE),y) for (x,y) in self._pairs[context]]
            for (pattern, response) in contextPair:
                    match = pattern.match(query)
                    # did the pattern match?
                    if match:
                        return context + "\n" + response

        # check each different pair
        for pair in self._pairs:
            contextPair = [(re.compile(x, re.IGNORECASE),y) for (x,y) in self._pairs[pair]]
            for (pattern, response) in contextPair:
                match = pattern.match(query)
                # did the pattern match?
                if match:
                    return pair + "\n" + response
        #Getting here means no pair found
        self._logger += ("(" + context + ")" + ": " + query + "\n")
        return pair + "\n" + self._confused_response

    def printLog(self):
        return self._logger


from typing import Callable, Optional, Type, cast

import jwt
from fastapi import APIRouter, Body, HTTPException, Request, status
from pydantic import UUID4, EmailStr

from fastapi_users import models
from fastapi_users.jwt import SecretType, decode_jwt, generate_jwt
from fastapi_users.router.common import ErrorCode, run_handler
from fastapi_users.user import (
    GetUserProtocol,
    UserAlreadyVerified,
    UserNotExists,
    VerifyUserProtocol,
)

VERIFY_USER_TOKEN_AUDIENCE = "fastapi-users:verify"

def get_verify_router(
    verify_user: VerifyUserProtocol,
    get_user: GetUserProtocol,
    user_model: Type[models.BaseUser],
    verification_token_secret: SecretType,
    verification_token_lifetime_seconds: int = 3600,
    after_verification_request: Optional[
        Callable[[models.UD, str, Request], None]
    ] = None,
    after_verification: Optional[Callable[[models.UD, Request], None]] = None,
):
    router = APIRouter()

    @router.post("/request-verify-token", status_code=status.HTTP_202_ACCEPTED)
    async def request_verify_token(
        request: Request, email: EmailStr = Body(..., embed=True)
    ):
        try:
            user = await get_user(email)
            if not user.is_verified and user.is_active:
                token_data = {
                    "user_id": str(user.id),
                    "email": email,
                    "aud": VERIFY_USER_TOKEN_AUDIENCE,
                }
                token = generate_jwt(
                    token_data,
                    verification_token_secret,
                    verification_token_lifetime_seconds,
                )

                if after_verification_request:
                    await run_handler(after_verification_request, user, token, request)
        except UserNotExists:
            pass

        return None

    @router.post("/verify", response_model=user_model)
    async def verify(request: Request, token: str = Body(..., embed=True)):
        try:
            data = decode_jwt(
                token, verification_token_secret, [VERIFY_USER_TOKEN_AUDIENCE]
            )
        except jwt.exceptions.ExpiredSignatureError:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_TOKEN_EXPIRED,
            )
        except jwt.PyJWTError:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
            )

        user_id = data.get("user_id")
        email = cast(EmailStr, data.get("email"))

        if user_id is None:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
            )

        try:
            user_check = await get_user(email)
        except UserNotExists:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
            )

        try:
            user_uuid = UUID4(user_id)
        except ValueError:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
            )

        if user_check.id != user_uuid:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
            )

        try:
            user = await verify_user(user_check)
        except UserAlreadyVerified:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.VERIFY_USER_ALREADY_VERIFIED,
            )

        if after_verification:
            await run_handler(after_verification, user, request)

        return user

    return router


#Código simples, que trás todas as informações se pesquisa de filme mas apenas da primeira página.
#Em formato de dicionário.

import requests
import json

req = None

def consulta_filme(titulo):
    try:
        req = requests.get('http://www.omdbapi.com/?apikey=4dee5e89&s=' + titulo)
        dicio = json.loads(req.text)
        return dicio
    except Exception as erro:
        print("Erro de conexão", erro)
        return None

sair = False
while not sair:
    nome_filme = input("Pesquisar nome do filme ou SAIR para fechar o programa: ")
    if nome_filme == "SAIR":
        sair = True
        print('Saindo do programa...')
    else:
         print_filme = consulta_filme(nome_filme)


# %% codecell
#Получаем данные

from sklearn import datasets
import  numpy as np

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

#%% codecell
#Разделяем массив данных на тренировочный и тестовый

from sklearn.model_selection import train_test_split

#train_test_split - мы произвольным образом разделяем массивы Х и у на тестовые данные в размере 30% от об щего объема (45 образцов) и тренировочные данные в размере 70% (105 образцов).
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = .3, random_state = 0)

# %% codecell
#Cтандартизация

# В приведенном ниже примера мы загрузили класс StandardScaler из модуля предобработки
# и инициализировали новый объект StandardScaler, который мы присвоили
# переменной sc. Затем мы воспользовались методом fit объекта StandardScaler,
# чтобы вычислить параметры μ (эмпирическое среднее) и cr (стандартное отклонение)
# для каждой размерности признаков из тренировочных данных. Вызвав метод
# transform, мы затем стандартизировали тренировочные данные, используя для этого
#расчетные параметры μ и cr.
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

# %% codecell
from sklearn.linear_model import Perceptron

ppn = Perceptron(n_iter_no_change = 40, eta0 = .1, random_state = 0)
    #n_iter_no_change - Количество итераций без каких-либо улучшений, чтобы дождаться ранней остановки
ppn.fit(X_train_std, y_train)

# Проверка. Проверим насколько точно наша модель классифицирует
#у_test - это истинные метки классов,
y_pred = ppn.predict(X_test_std)
#ошибки классификации и метрика верности
print('Чиcлo ошибочно классифицированных образцов : %d' % (y_test != y_pred).sum())
n = (y_test != y_pred).sum()
p = n*100 / len(y_test)
print('Ошибки классификации : ', p, '%')
print('Метрика верности : ', (100-p), '%')

from sklearn.metrics import accuracy_score

print('Bepнocть: %.2f ' %accuracy_score(y_test, y_pred))

# %% codecell

from matplotlib.colors import ListedColormap

def plot_decision_regions(X, y, classifier, test_idx = None, resolution = .02) :
    markers = ('s', 'x', 'o', '^', 'v ')
    colors = ('red', 'blue', 'lightgreen' , 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    #Определяем поверхность
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)

    #Строим график
    plt.contourf(xx1, xx2, Z, alpha = .4, cmap = cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    #Добавляем точки
    for idx, cl in enumerate(np.unique(y)) :
        plt.scatter(x = X[y == cl, 0], y = X[y == cl, 1], color = cmap(idx), edgecolors = 'black',
                alpha = .8, marker = markers[idx], label = cl, linewidths = .2, s = 17)

        # показать все образцы
        if test_idx :
            X_test, y_test = X[test_idx, :], y[test_idx]
            plt.scatter(X_test[:, 0], X_test[:, 1], color = cmap(idx), edgecolors = 'black',
                    alpha = 1.0, marker = 'o', label = 'тестовый набор', linewidths = 10, s = 1)

# %% codecell
import matplotlib.pyplot as plt

X_combinad_std = np.vstack((X_train_std, X_test_std))
    #np.vstack(X, X1) - объединяет все элементы из X, X1 в столбец
y_combinad_std = np.hstack((y_train, y_test))
    #np.hstack(X, X1) - "прилепляет" снизу X, матрицу X1
plot_decision_regions(X = X_combinad_std, y = y_combinad_std, classifier = ppn, test_idx = range(105, 150))
plt.xlabel('дли на ле пе стка [стандартизованная]')
plt.ylabel('ширина лепестка [стандартизованная]')
plt.legend(loc = 'upper left')
plt.show()


from .helper import share

def _jupyter_nbextension_paths():
    """Required to load JS button"""
    return [dict(
        section="notebook",
        src="static",
        dest="callisto_bundler",
        require="callisto_bundler/index")]

def _jupyter_bundlerextension_paths():
    """Declare bundler extensions provided by this package."""
    return [{
        # unique bundler name
        "name": "callisto_bundler",
        # module containing bundle function
        "module_name": "callisto_bundler",
        # human-readable menu item label
        "label": "Upload to Callisto Gallery",
        # group under 'deploy' menu
        "group": "deploy",
    }]

def bundle(handler, model):
    share(handler, model)


"""Contains configuration related functions."""

import argparse
import os.path
import sys
import requests
import yaml
from twitchgamenotify.cache import unlock_cache
from twitchgamenotify.constants import (
    CONFIG_FILE_NAME,
    EXAMPLE_CONFIG_FILE_URL,
    LOGLEVEL_CHOICES,
    LOGLEVEL_DICT,
    PROJECT_BASE_DIR,
    PROJECT_CONFIG_HOME,
    WARNING,
)
from twitchgamenotify.version import NAME, VERSION, DESCRIPTION

class ConfigFileNotFound(Exception):
    """Raised when a config file can't be found."""

    pass

class PrintExampleConfigAction(argparse.Action):
    """argparse action to print example config file."""

    def __call__(self, parser, namespace, values, option_string=None):
        """Download and print example config file."""
        r = requests.get(EXAMPLE_CONFIG_FILE_URL)

        if r.status_code != 200:
            print(
                "ERROR: Failed to download example config file!",
                file=sys.stderr,
            )
            sys.exit(1)
        else:
            print(r.content.decode())
            sys.exit(0)

class RemoveCacheLockAction(argparse.Action):
    """argparse action to remove cache lock (should one exist)."""

    def __call__(self, parser, namespace, values, option_string=None):
        """Remove the cache lock."""
        try:
            unlock_cache(catch_failure=False)
        except FileNotFoundError:
            print(
                "WARNING: No existing cache lock to remove!", file=sys.stderr
            )

        # Get out
        sys.exit(0)

class TranslateLogLevelAction(argparse.Action):
    """argparse action to translate loglevel to a number."""

    def __call__(self, parser, namespace, values, option_string=None):
        """Set a logging loglevel number."""
        setattr(namespace, self.dest, LOGLEVEL_DICT[values])

def find_config_file():
    """Find and return the path of a config file.

    The config file looked for is "config.yaml" and it is looked for at
    the base of the respository first (if you're running from source),
    and then in $XDG_CONFIG_HOME/twitch-game-notify/ (XDG_CONFIG_HOME
    defaults to $HOME/.config).

    Returns:
        A string containing the absolute path to the config file.

    Raises:
        ConfigFileNotFound: A config file couldn't be found.
    """
    # Check the base of the project
    config_path = os.path.join(PROJECT_BASE_DIR, CONFIG_FILE_NAME)

    if os.path.exists(config_path):
        return config_path

    # Check XDG_CONFIG_HOME
    config_path = os.path.join(PROJECT_CONFIG_HOME, CONFIG_FILE_NAME)

    if os.path.exists(config_path):
        return config_path

    # Couldn't find anything :thinking:
    raise ConfigFileNotFound

def parse_config_file():
    """Find and parse a config file."""
    # Find the config file first
    config_path = find_config_file()

    # Now parse and return it - note that PyYAML doesn't come with any
    # schema validation, which might be desirable at some point
    with open(config_path, "r") as config_file:
        return yaml.safe_load(config_file)

def parse_runtime_args():
    """Parse runtime args using argparse.

    Returns:
        An object of type 'argparse.Namespace' containing the runtime
        arguments as attributes. See argparse documentation for more
        details.
    """
    parser = argparse.ArgumentParser(
        prog=NAME, description="%(prog)s - " + DESCRIPTION
    )
    parser.add_argument(
        "-l",
        "--loglevel",
        default=LOGLEVEL_DICT[WARNING],
        choices=LOGLEVEL_CHOICES,
        action=TranslateLogLevelAction,
        help="how much to log",
    )
    parser.add_argument(
        "--one-shot", action="store_true", help="query once then exit"
    )
    parser.add_argument(
        "--no-app-indicator",
        action="store_true",
        help="don't launch an app indicator",
    )
    parser.add_argument(
        "--no-caching", action="store_true", help="don't cache static API data"
    )
    parser.add_argument(
        "--print-config",
        nargs=0,
        action=PrintExampleConfigAction,
        help="downloads and prints example config file",
    )
    parser.add_argument(
        "--print-to-terminal",
        action="store_true",
        help="print to terminal (doesn't connect to D-Bus)",
    )
    parser.add_argument(
        "--remove-cache-lock",
        nargs=0,
        action=RemoveCacheLockAction,
        help="removes a cache lock if one exists",
    )
    parser.add_argument(
        "--version", action="version", version="%(prog)s " + VERSION
    )

    return parser.parse_args()


import math
import nltk
import os
import sys

def main():
    """Calculate top term frequencies for a corpus of documents."""

    if len(sys.argv) != 2:
        sys.exit("Usage: python tfidf.py corpus")
    print("Loading data...")
    corpus = load_data(sys.argv[1])

    # Get all words in corpus
    print("Extracting words from corpus...")
    words = set()
    for filename in corpus:
        words.update(corpus[filename])

    # Calculate TF-IDFs
    print("Calculating term frequencies...")
    tfidfs = dict()
    for filename in corpus:
        tfidfs[filename] = []
        for word in corpus[filename]:
            tf = corpus[filename][word]
            tfidfs[filename].append((word, tf))

    # Sort and get top 5 term frequencies for each file
    print("Computing top terms...")
    for filename in corpus:
        tfidfs[filename].sort(key=lambda tfidf: tfidf[1], reverse=True)
        tfidfs[filename] = tfidfs[filename][:5]

    # Print results
    print()
    for filename in corpus:
        print(filename)
        for term, score in tfidfs[filename]:
            print(f"    {term}: {score:.4f}")

def load_data(directory):
    files = dict()
    for filename in os.listdir(directory):
        with open(os.path.join(directory, filename)) as f:

            # Extract words
            contents = [
                word.lower() for word in
                nltk.word_tokenize(f.read())
                if word.isalpha()
            ]

            # Count frequencies
            frequencies = dict()
            for word in contents:
                if word not in frequencies:
                    frequencies[word] = 1
                else:
                    frequencies[word] += 1
            files[filename] = frequencies

    return files

if __name__ == "__main__":
    main()


import os
import shutil

from collective.transmogrifier.interfaces import ITransmogrifier

from plone.app.controlpanel.form import ControlPanelForm
from plone.fieldsets.form import FieldsetsEditForm
from zope.app.component.hooks import getSite
from zope.formlib import form

from zope.interface import directlyProvides
from zope.interface import Interface
from zope.interface import implements

from zope.schema import Choice #TextLine
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

from Products.CMFCore.utils import getToolByName
from Products.CMFDefault.formlib.schema import ProxyFieldProperty
from Products.CMFDefault.formlib.schema import SchemaAdapterBase
from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile

from logging import getLogger
LOG = getLogger('oops.staticdump')

def DumpersVocabularyFactory(context):
    """Vocabulary factory for dumpers ids
    """
    portal = getSite()
    tool = getToolByName(portal, 'portal_dumper')
    terms = [SimpleTerm(o.getId(), title=o.title) for o in tool.objectValues()]
    return SimpleVocabulary(terms)

directlyProvides(DumpersVocabularyFactory, IVocabularyFactory)

class IDumpPanelSchema(Interface):

    dumper = Choice(
        title=u'Dumpers',
        description=u'Available dumpers',
        vocabulary=u'oops.staticdump.dumpers'
    )

class DumpPanelAdapter(SchemaAdapterBase):

    implements(IDumpPanelSchema)

    def __init__(self, context):
        super(DumpPanelAdapter, self).__init__(context)
        self.portal = getSite()
        dumper_tool = getToolByName(self.portal, 'portal_dumper')
        self.context = dumper_tool

    dumper = ProxyFieldProperty(IDumpPanelSchema['dumper'])

class DumpPanel(ControlPanelForm):
    """A simple form to run the site dump."""

    implements(IDumpPanelSchema)

    form_fields = form.FormFields(IDumpPanelSchema)
    label = u'Static version'
    description = u"Update static version of this site"
    form_name = u'Dump'

    def __call__(self):
        self.update()

        portal = getSite()
        if dump_is() == running:
            if not 'Dump' in self.status:
                self.status =  '%s %s'%('Dump running.', self.status)
            else:
                self.status = 'Dump running.'

        return self.render()

    @form.action(u'Save', name=u'save')
    def handle_save(self, action, data):
        tool = getToolByName(self.context, 'portal_dumper')
        tool.setDumper(data['dumper'])
        self.status = u'Save completed.'

    @form.action(u'Save and dump now', name=u'dump')
    def handle_dump(self, action, data):
        tool = getToolByName(self.context, 'portal_dumper')
        name = data['dumper']
        tool.setDumper(name)

        if dump_is() == running:
            self.status = 'Dump running.'
        else:
            LOG.info('started: %s.'%name)
            dump_is(running)

            # do dump
            destination = tool.getDumperProperty('filesystem_target')
            tmp = destination + '_tmp'

            transmogrifier_overrides = {
                'destination': tmp,
                'static_base': tool.getDumperProperty('html_base')
            }

            transmogrifier = ITransmogrifier(self.context)
            configuration_name = tool.getDumperProperty(
                                    'dump_configuration_name',
                                    'dump_sample')

            try:
                transmogrifier(
                    configuration_name,
                    transmogrifier = transmogrifier_overrides
                )
            except:
                dump_is(not_running)
                raise

            backup_and_switch(destination, tmp)
            dump_is(not_running)
            self.status = u'Dump completed.'
            LOG.info('completed.')

def backup_and_switch(destination, tmp):
    if os.path.exists(destination):
        old = destination + '_old'
        if os.path.exists(old):
            shutil.rmtree(old)
        shutil.move(destination, old)
    shutil.move(tmp, destination)

running = True
not_running = False
def dump_is(status=None):
    portal = getSite()
    tool = getToolByName(portal, 'portal_dumper')
    return tool.status(status)


import os
import pandas as pd

trainfile_code = "4"
#trainfile_code = "4-1"

#mainfile = open("genefusion-data/snli-dl-version/processed_train" + trainfile_code + "_unique_id_180521_lim_result_pre.tsv",encoding='utf8').readlines()
mainfile = open("genefusion-data/snli-dl-version/dl_train" + trainfile_code + "_unique_id_lim_result_pre.tsv",encoding='utf8').readlines()
mainfile_dict = dict()

#for l in open("genefusion-data/DL_uniqueid/[id]processed_train" + trainfile_code + "_unique_id_180521_lim_result_pre.tsv", encoding='utf8'):
for l in open("genefusion-data/DL_uniqueid/[id]dl_train" + trainfile_code + "_unique_id_lim_result_pre.tsv", encoding='utf8'):
    tokens = l.strip().split("\t")
    mainfile_dict[tokens[0]] = int(tokens[1])

#emb_name = "ensemble_index_180523/"
#emb_name = "yuanfang_undersample_100/"
emb_name = "yuanfang_undersample_300/"
#emb_name = "yuanfang_undersample_indexes/"
#save_name = "dl_" + trainfile_code + "_oversampling/"
save_name = "dl_" + trainfile_code + "_undersampling_300/"
target_dir = "genefusion-data/snli-dl-version/%s"%save_name
if not os.path.isdir(target_dir):
    os.mkdir(target_dir)

for i in range(300):
    idx_ = i+1
    ids = open("/mnt/smb/Q/sunkyu/gpower4/%s/train%s_%s_index.txt"%(emb_name,trainfile_code,idx_)).readlines()

    indices = [mainfile_dict[v.strip()] for v in ids]

    with open(target_dir+str(idx_)+".tsv", "w", encoding="utf8") as fw:
        for j in indices:
            fw.write(mainfile[j])

idx_ = "val"
ids = open("/mnt/smb/Q/sunkyu/gpower4/%s/train%s_%s_index.txt"%(emb_name,trainfile_code, idx_)).readlines()

indices = [mainfile_dict[v.strip()] for v in ids]

with open(target_dir+str(idx_)+".tsv", "w", encoding="utf8") as fw:
    for j in indices:
        fw.write(mainfile[j])


from services.crypto_compare import CryptoCompare

def crypto(bot, update):
    current_rate = CryptoCompare.get_rate(["BTC", "BCH", "ETH", "XMR"], ["USD"])
    message = "```\n"
    for crypt, rates in sorted(current_rate.items()):
        message += "1 " + str(crypt) + " = $" + str(round(rates['USD'], 2))
        message += "\n"
    message += "```"
    bot.send_message(chat_id=update.message.chat_id,
                     text=message,
                     parse_mode="markdown")


#   Copyright (C) 2013 Canonical Ltd.
#
#   Author: Scott Moser <scott.moser@canonical.com>
#
#   Curtin is free software: you can redistribute it and/or modify it under
#   the terms of the GNU Affero General Public License as published by the
#   Free Software Foundation, either version 3 of the License, or (at your
#   option) any later version.
#
#   Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
#   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
#   more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with Curtin.  If not, see <http://www.gnu.org/licenses/>.

import yaml
import json

ARCHIVE_HEADER = "#curtin-config-archive"
ARCHIVE_TYPE = "text/curtin-config-archive"
CONFIG_HEADER = "#curtin-config"
CONFIG_TYPE = "text/curtin-config"

try:
    # python2
    _STRING_TYPES = (str, basestring, unicode)
except NameError:
    # python3
    _STRING_TYPES = (str,)

def merge_config_fp(cfgin, fp):
    merge_config_str(cfgin, fp.read())

def merge_config_str(cfgin, cfgstr):
    cfg2 = yaml.safe_load(cfgstr)
    if not isinstance(cfg2, dict):
        raise TypeError("Failed reading config. not a dictionary: %s" % cfgstr)

    merge_config(cfgin, cfg2)

def merge_config(cfg, cfg2):
    # update cfg by merging cfg2 over the top
    for k, v in cfg2.items():
        if isinstance(v, dict) and isinstance(cfg.get(k, None), dict):
            merge_config(cfg[k], v)
        else:
            cfg[k] = v

def merge_cmdarg(cfg, cmdarg, delim="/"):
    merge_config(cfg, cmdarg2cfg(cmdarg, delim))

def cmdarg2cfg(cmdarg, delim="/"):
    if '=' not in cmdarg:
        raise ValueError('no "=" in "%s"' % cmdarg)

    key, val = cmdarg.split("=", 1)
    cfg = {}
    cur = cfg

    is_json = False
    if key.startswith("json:"):
        is_json = True
        key = key[5:]

    items = key.split(delim)
    for item in items[:-1]:
        cur[item] = {}
        cur = cur[item]

    if is_json:
        try:
            val = json.loads(val)
        except (ValueError, TypeError):
            raise ValueError("setting of key '%s' had invalid json: %s" %
                             (key, val))

    # this would occur if 'json:={"topkey": "topval"}'
    if items[-1] == "":
        cfg = val
    else:
        cur[items[-1]] = val

    return cfg

def load_config_archive(content):
    archive = yaml.load(content)
    config = {}
    for part in archive:
        if isinstance(part, (str,)):
            if part.startswith(ARCHIVE_HEADER):
                merge_config(config, load_config_archive(part))
            elif part.startswith(CONFIG_HEADER):
                merge_config_str(config, part)
        elif isinstance(part, dict) and isinstance(part.get('content'), str):
            payload = part.get('content')
            if (part.get('type') == ARCHIVE_TYPE or
                    payload.startswith(ARCHIVE_HEADER)):
                merge_config(config, load_config_archive(payload))
            elif (part.get('type') == CONFIG_TYPE or
                  payload.startswith(CONFIG_HEADER)):
                merge_config_str(config, payload)
    return config

def load_config(cfg_file):
    with open(cfg_file, "r") as fp:
        content = fp.read()
    if not content.startswith(ARCHIVE_HEADER):
        return yaml.safe_load(content)
    else:
        return load_config_archive(content)

def load_command_config(args, state):
    if hasattr(args, 'config') and args.config:
        return args.config
    else:
        # state 'config' points to a file with fully rendered config
        cfg_file = state.get('config')

    if not cfg_file:
        cfg = {}
    else:
        cfg = load_config(cfg_file)
    return cfg

def dump_config(config):
    return yaml.dump(config, default_flow_style=False, indent=2)

def value_as_boolean(value):
    if value in (False, None, '0', 0, 'False', 'false', ''):
        return False
    return True