import os
import time
import sys
import ffmpeg_lib as ffmpeg
import array as arr
from collections import namedtuple

MAX_VIDEO_FRAME_SIZE = 1000000
MAX_AUDIO_FRAME_SIZE = 64000
frame_header_size = 32
es_info = namedtuple('es_info', "type es_file")
frame_header = namedtuple('frame_header', "size pts dts utc flags")

def cmdline_parse():
    options = {}
    options['out_ts_file'] = sys.argv[1]
    options['vid_codec_type'] = sys.argv[2]
    options['vid_es_file'] = sys.argv[3]

    options['audio_es_files'] = []
    for codec, es_file in zip(sys.argv[4::2], sys.argv[5::2]):
        if codec == "mp2" or codec == "aac" or codec == "ac3":
            es = es_info(codec, es_file)
            options['audio_es_files'].append(es)

    return options

def read_frame_hdr(es_fd):
    b = es_fd.read(4)
    if len(b) == 0:
        return -1, None
    size = int.from_bytes(b, byteorder='little', signed=True)

    b = es_fd.read(8)
    if len(b) == 0:
        return -1, None
    pts = int.from_bytes(b, byteorder='little', signed=True)

    b = es_fd.read(8)
    if len(b) == 0:
        return -1, None
    dts = int.from_bytes(b, byteorder='little', signed=True)

    b = es_fd.read(8)
    if len(b) == 0:
        return -1, None
    utc = int.from_bytes(b, byteorder='little', signed=True)

    b = es_fd.read(4)
    if len(b) == 0:
        return -1, None
    flags = int.from_bytes(b, byteorder='little', signed=True)

    return 0, frame_header(size, pts, dts, utc, flags)

def write_muxed_output(mux, o_buf, mux_out_fd):
    bytes_wr = 0
    read_bytes = 188*7
    size_avail = ffmpeg.get_muxed_output_avail_size(mux)
    if size_avail <= read_bytes:
        return bytes_wr
    while(1):
        ret = ffmpeg.read_muxed_data(mux, o_buf, 188*7)
        if ret == 0:
            mux_out_fd.write(o_buf)
            bytes_wr += 188*7
        else:
            break
    return bytes_wr

def muxer_process(mux, video_es_fd, num_aud,
                  audio_es_fd, mux_out_fd):

    o_buf = bytearray(188*7)

    while(1):
        write_muxed_output(mux, o_buf, mux_out_fd)

        ret, video_frame_hdr = read_frame_hdr(video_es_fd)
        if ret == -1:
            break

        print(f"PY_DEBUG: vid, {video_frame_hdr.size}")

        if ffmpeg.get_video_write_avail_size(mux) > video_frame_hdr.size:
            vid_frame = video_es_fd.read(video_frame_hdr.size)
            print("Calling write_video_frame")
            ffmpeg.write_video_frame(mux, vid_frame, video_frame_hdr.size,
                                     video_frame_hdr.pts, video_frame_hdr.dts)
            #print("Finish write_video_frame")
        else:
            video_es_fd.seek(-frame_header_size, os.SEEK_CUR)
            time.sleep(0.040)

        for i in range(num_aud):
            ret, audio_frame_hdr = read_frame_hdr(audio_es_fd[i])
            if ret == -1:
                break

            print(f"PY_DEBUG: aud, {audio_frame_hdr.size}")

            if (ffmpeg.get_audio_write_avail_size(mux, i) >
                    audio_frame_hdr.size)  and (
                    audio_frame_hdr.pts <= video_frame_hdr.pts):

                audio_frame = audio_es_fd[i].read(audio_frame_hdr.size)
                ffmpeg.write_audio_frame(mux, audio_frame, audio_frame_hdr.size,
                                         audio_frame_hdr.pts,
                                         audio_frame_hdr.dts, i)
            else:
                audio_es_fd[i].seek(-frame_header_size, os.SEEK_CUR)
                break

    for i in range(10):
        bytes_wr = write_muxed_output(mux, o_buf, mux_out_fd)
        if bytes_wr == 0:
            time.sleep(0.050)

def main():

    options = cmdline_parse()

    #a1 = ffmpeg.new_intArray(32)
    #audio_pids = arr.array('i', [0] * 32)
    audio_pids = [0] * 32
    audio_codec = [0] * 32
    params = ffmpeg.ts_muxer_params_t()

    params.vid_pid   = 2064
    params.vid_codec = ffmpeg.AV_CODEC_VID_H264
    params.frame_rate = 29.97

    params.num_aud_tracks = len(options['audio_es_files'])
    audio_es_fd = []
    audio_pid_val = 2068
    for i in range(params.num_aud_tracks ):
        audio_pids[i] = audio_pid_val
        audio_es_fd.append(open(options['audio_es_files'][i].es_file, 'rb'))
        audio_pid_val += 1
        if options['audio_es_files'][i].type == "mp2":
            audio_codec[i] = ffmpeg.AV_CODEC_AUD_MP2
        elif options['audio_es_files]'][i].type == "aac":
            audio_codec[i] = ffmpeg.AV_CODEC_AUD_AAC

    params.aud_codec = audio_codec
    params.aud_pid = audio_pids
    params.mux_rate = 30000000

    mux = ffmpeg.create_ts_muxer(params)

    print(mux)

    video_es_fd = open(options['vid_es_file'], "rb")

    mux_out_fd = open(options['out_ts_file'], 'wb')

    muxer_process(mux, video_es_fd, params.num_aud_tracks,
                  audio_es_fd, mux_out_fd)

if __name__ == "__main__":
    main()


import pycparser
def _traverse_tree(root):
    num_nodes = 1
    queue = [root]
    root_json = {
        "node": _name(root),

        "children": []
    }
    queue_json = [root_json]
    while queue:
        current_node = queue.pop(0)
        num_nodes += 1
        current_node_json = queue_json.pop(0)
        global alltokenlist
        alltokenlist = []
        vistast(current_node)
        if alltokenlist != []:
            ss = alltokenlist[0]
            sss = ss.replace(' ', '')
            if sss.startswith('"') or sss.startswith("'"):
                k=1
            else:
                child_json = {
                "node": sss,
                "children": []
                }

                current_node_json['children'].append(child_json)
        for item in current_node.children():
            if _name(item[1]).startswith('"') or _name(item[1]).startswith("'"):
                pass
            else:
                queue.append(item[1])
                child_json = {
                "node": _name(item[1]),
                "children": []
                }
                current_node_json['children'].append(child_json)
                queue_json.append(child_json)
    return root_json, num_nodes

def _name(node):
    return type(node).__name__
def vistast(node):
    nvlist=[(n,getattr(node,n)) for n in node.attr_names]
    # print("nvlists:")
    # print(nvlist)
    # print("nvliste:")
    if nvlist!=[]:
        if hasattr(node,'op'):
            word=node.op
            #print("word_op:" + word)
        elif hasattr(node,'declname'):
            word=node.declname
            #print("word_declname:" + word)
            if word is None:
                word=node.__class__.__name__
        elif isinstance(node,pycparser.c_ast.IdentifierType):
            word=node.names[0]
            #print("word_IdentifierType:" + word)
        elif isinstance(node,pycparser.c_ast.Constant):
            word=node.value
            #print("word_Constant:" + word)
        elif isinstance(node,pycparser.c_ast.ID):
            word=node.name
            #print("word_ID:" + word)
        else:
            word=nvlist[0][1]
            word=str(word)
            #print("word_word:" + word)
        alltokenlist.append(word)
numnodes=0
def dfsDict(root):
    global listtfinal
    listtfinal.append(str(root['node']))
    global numnodes
    numnodes+=1
    if len(root['children']):
        pass
    else:
        return
    for dictt in root['children']:
        dfsDict(dictt)
f=open("flistPOJ.txt",'r')
ff=open("sentencePOJwithid.txt",'w')
line=f.readline().rstrip("\t")
li=line.split("\t")
for l in li:
    tree = pycparser.parse_file(l)
    listtfinal = []
    numnodes = 0
    sample,num_nodes=_traverse_tree(tree)
    dfsDict(sample)
    for lis in listtfinal:
        ff.write(lis)
        ff.write(" ")
    ff.write("\n")
    #print(listtfinal)
ff.close()


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

import dash
import dash_core_components as dcc
import dash_daq as daq
import dash_html_components as html
from dash.dependencies import Input, Output

from zvt.api.business import get_trader
from zvt.api.technical import get_current_price
from zvt.charts.dcc_components import get_trader_detail_figures
from zvt.domain import TradingLevel
from zvt.reader.business_reader import AccountReader, OrderReader

trader = 'singlecointrader'
security = 'coin_binance_EOS/USDT'

trader_domain = get_trader(trader_name=trader, return_type='domain')[0]
account_reader = AccountReader(trader_names=['singlecointrader'], level=TradingLevel.LEVEL_1MIN)
order_reader = OrderReader(trader_names=['singlecointrader'])

def serve_layout():
    layout = html.Div(
        [
            daq.LEDDisplay(
                id='current_price',
                label="EOS/USDT current price",
                value=get_current_price(security_list=[security]),
                color="#FF5E5E"
            ),

            html.Div(id='trader-details', children=get_trader_detail_figures(trader_domain=trader_domain,
                                                                             account_reader=None,
                                                                             order_reader=order_reader)),

            dcc.Interval(
                id='interval-component',
                interval=20 * 1000,  # in milliseconds
                n_intervals=0
            ),
            dcc.Interval(
                id='interval-component1',
                interval=3 * 1000,  # in milliseconds
                n_intervals=0
            )
        ])

    return layout

app = dash.Dash(__name__)

app.layout = serve_layout

@app.callback(
    Output('trader-details', 'children'),
    [Input('interval-component', 'n_intervals')])
def update_trader_details(n):
    # order_reader.move_on()
    return get_trader_detail_figures(trader_domain=trader_domain,
                                     account_reader=None,
                                     order_reader=order_reader)

@app.callback(
    Output('current_price', 'value'),
    [Input('interval-component1', 'n_intervals')])
def update_current_price(n):
    price_reader.move_on()
    return price_reader.get_data_df()['price'][-1]

if __name__ == '__main__':
    app.run_server(debug=True)


#!/usr/bin/env python

# The MIT License (MIT)
#
# Copyright (c) 2015 Stany MARCEL <stanypub@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import ctypes
import _ctypes
import time
from math import pi, copysign, sqrt
from enum import IntEnum
from steamcontroller.cheader import defines

from steamcontroller.tools import get_so_extensions

from collections import deque

# Get All defines from linux headers
if os.path.exists('/usr/include/linux/input-event-codes.h'):
    CHEAD = defines('/usr/include', 'linux/input-event-codes.h')
else:
    CHEAD = defines('/usr/include', 'linux/input.h')

# Keys enum contains all keys and button from linux/uinput.h (KEY_* BTN_*)
Keys = IntEnum('Keys', {i: CHEAD[i] for i in CHEAD.keys() if (i.startswith('KEY_') or
                                                            i.startswith('BTN_'))})
# Keys enum contains all keys and button from linux/uinput.h (KEY_* BTN_*)
KeysOnly = IntEnum('KeysOnly', {i: CHEAD[i] for i in CHEAD.keys() if i.startswith('KEY_')})

# Axes enum contains all axes from linux/uinput.h (ABS_*)
Axes = IntEnum('Axes', {i: CHEAD[i] for i in CHEAD.keys() if i.startswith('ABS_')})

# Rels enum contains all rels from linux/uinput.h (REL_*)
Rels = IntEnum('Rels', {i: CHEAD[i] for i in CHEAD.keys() if i.startswith('REL_')})

# Scan codes for each keys (taken from a logitech keyboard)
Scans = {
    Keys.KEY_ESC: 0x70029,
    Keys.KEY_F1: 0x7003a,
    Keys.KEY_F2: 0x7003b,
    Keys.KEY_F3: 0x7003c,
    Keys.KEY_F4: 0x7003d,
    Keys.KEY_F5: 0x7003e,
    Keys.KEY_F6: 0x7003f,
    Keys.KEY_F7: 0x70040,
    Keys.KEY_F8: 0x70041,
    Keys.KEY_F9: 0x70042,
    Keys.KEY_F10: 0x70043,
    Keys.KEY_F11: 0x70044,
    Keys.KEY_F12: 0x70045,
    Keys.KEY_SYSRQ: 0x70046,
    Keys.KEY_SCROLLLOCK: 0x70047,
    Keys.KEY_PAUSE: 0x70048,
    Keys.KEY_GRAVE: 0x70035,
    Keys.KEY_1: 0x7001e,
    Keys.KEY_2: 0x7001f,
    Keys.KEY_3: 0x70020,
    Keys.KEY_4: 0x70021,
    Keys.KEY_5: 0x70022,
    Keys.KEY_6: 0x70023,
    Keys.KEY_7: 0x70024,
    Keys.KEY_8: 0x70025,
    Keys.KEY_9: 0x70026,
    Keys.KEY_0: 0x70027,
    Keys.KEY_MINUS: 0x7002d,
    Keys.KEY_EQUAL: 0x7002e,
    Keys.KEY_BACKSPACE: 0x7002a,
    Keys.KEY_TAB: 0x7002b,
    Keys.KEY_Q: 0x70014,
    Keys.KEY_W: 0x7001a,
    Keys.KEY_E: 0x70008,
    Keys.KEY_R: 0x70015,
    Keys.KEY_T: 0x70017,
    Keys.KEY_Y: 0x7001c,
    Keys.KEY_U: 0x70018,
    Keys.KEY_I: 0x7000c,
    Keys.KEY_O: 0x70012,
    Keys.KEY_P: 0x70013,
    Keys.KEY_LEFTBRACE: 0x7002f,
    Keys.KEY_RIGHTBRACE: 0x70030,
    Keys.KEY_ENTER: 0x70028,
    Keys.KEY_CAPSLOCK: 0x70039,
    Keys.KEY_A: 0x70004,
    Keys.KEY_S: 0x70016,
    Keys.KEY_D: 0x70007,
    Keys.KEY_F: 0x70009,
    Keys.KEY_G: 0x7000a,
    Keys.KEY_H: 0x7000b,
    Keys.KEY_J: 0x7000d,
    Keys.KEY_K: 0x7000e,
    Keys.KEY_L: 0x7000f,
    Keys.KEY_SEMICOLON: 0x70033,
    Keys.KEY_APOSTROPHE: 0x70034,
    Keys.KEY_BACKSLASH: 0x70032,
    Keys.KEY_LEFTSHIFT: 0x700e1,
    Keys.KEY_102ND: 0x70064,
    Keys.KEY_Z: 0x7001d,
    Keys.KEY_X: 0x7001b,
    Keys.KEY_C: 0x70006,
    Keys.KEY_V: 0x70019,
    Keys.KEY_B: 0x70005,
    Keys.KEY_N: 0x70011,
    Keys.KEY_M: 0x70010,
    Keys.KEY_COMMA: 0x70036,
    Keys.KEY_DOT: 0x70037,
    Keys.KEY_SLASH: 0x70038,
    Keys.KEY_RIGHTSHIFT: 0x700e5,
    Keys.KEY_LEFTCTRL: 0x700e0,
    Keys.KEY_LEFTMETA: 0x700e3,
    Keys.KEY_LEFTALT: 0x700e2,
    Keys.KEY_SPACE: 0x7002c,
    Keys.KEY_RIGHTALT: 0x700e6,
    Keys.KEY_RIGHTMETA: 0x700e7,
    Keys.KEY_COMPOSE: 0x70065,
    Keys.KEY_RIGHTCTRL: 0x700e4,
    Keys.KEY_INSERT: 0x70049,
    Keys.KEY_HOME: 0x7004a,
    Keys.KEY_PAGEUP: 0x7004b,
    Keys.KEY_DELETE: 0x7004c,
    Keys.KEY_END: 0x7004d,
    Keys.KEY_PAGEDOWN: 0x7004e,
    Keys.KEY_UP: 0x70052,
    Keys.KEY_LEFT: 0x70050,
    Keys.KEY_DOWN: 0x70051,
    Keys.KEY_RIGHT: 0x7004f,
    Keys.KEY_NUMLOCK: 0x70053,
    Keys.KEY_KPSLASH: 0x70054,
    Keys.KEY_KPASTERISK: 0x70055,
    Keys.KEY_KPMINUS: 0x70056,
    Keys.KEY_KP7: 0x7005f,
    Keys.KEY_KP8: 0x70060,
    Keys.KEY_KP9: 0x70061,
    Keys.KEY_KPPLUS: 0x70057,
    Keys.KEY_KP4: 0x7005c,
    Keys.KEY_KP5: 0x7005d,
    Keys.KEY_KP6: 0x7005e,
    Keys.KEY_KP1: 0x70059,
    Keys.KEY_KP2: 0x7005a,
    Keys.KEY_KP3: 0x7005b,
    Keys.KEY_KPENTER: 0x70058,
    Keys.KEY_KP0: 0x70062,
    Keys.KEY_KPDOT: 0x70063,
    Keys.KEY_CONFIG: 0xc0183,
    Keys.KEY_PLAYPAUSE: 0xc00cd,
    Keys.KEY_MUTE: 0xc00e2,
    Keys.KEY_VOLUMEDOWN: 0xc00ea,
    Keys.KEY_VOLUMEUP: 0xc00e9,
    Keys.KEY_HOMEPAGE: 0xc0223,

    Keys.KEY_PREVIOUSSONG: 0xc00f0,
    Keys.KEY_NEXTSONG: 0xc00f1,

    Keys.KEY_BACK: 0xc00f2,
    Keys.KEY_FORWARD: 0xc00f3,
}

class UInput(object):
    """
    UInput class permits to create a uinput device.

    See Gamepad, Mouse, Keyboard for examples
    """

    def __init__(self, vendor, product, version, name, keys, axes, rels, keyboard=False):
        self._lib = None
        self._k = keys
        if not axes or len(axes) == 0:
            self._a, self._amin, self._amax, self._afuzz, self._aflat = [[]] * 5
        else:
            self._a, self._amin, self._amax, self._afuzz, self._aflat = zip(*axes)

        self._r = rels
        self.vendor = vendor
        self.product = product
        self.name = name
        self.version = version
        self.keyboard = keyboard
        self._fd = None

    def createDevice(self):
        possible_paths = []
        for extension in get_so_extensions():
            possible_paths.append(
                os.path.abspath(
                    os.path.normpath(
                        os.path.join(
                            os.path.dirname(__file__),
                            '..',
                            'libuinput' + extension
                        )
                    )
                )
            )
        lib = None
        for path in possible_paths:
            if os.path.exists(path):
                lib = path
                break
        if not lib:
            raise OSError('Cant find libuinput. searched at:\n {}'.format(
                '\n'.join(possible_paths)
            )
        )

        self._lib = ctypes.CDLL(lib)

        c_k        = (ctypes.c_uint16 * len(self._k))(*self._k)
        c_a        = (ctypes.c_uint16 * len(self._a))(*self._a)
        c_amin     = (ctypes.c_int32  * len(self._amin ))(*self._amin )
        c_amax     = (ctypes.c_int32  * len(self._amax ))(*self._amax )
        c_afuzz    = (ctypes.c_int32  * len(self._afuzz))(*self._afuzz)
        c_aflat    = (ctypes.c_int32  * len(self._aflat))(*self._aflat)
        c_r        = (ctypes.c_uint16 * len(self._r))(*self._r)
        c_vendor   = ctypes.c_uint16(self.vendor)
        c_product  = ctypes.c_uint16(self.product)
        c_version  = ctypes.c_uint16(self.version)
        c_keyboard = ctypes.c_int(self.keyboard)

        c_name = ctypes.c_char_p(self.name)
        self._fd = self._lib.uinput_init(ctypes.c_int(len(self._k)),
                                         c_k,
                                         ctypes.c_int(len(self._a)),
                                         c_a,
                                         c_amin,
                                         c_amax,
                                         c_afuzz,
                                         c_aflat,
                                         ctypes.c_int(len(self._r)),
                                         c_r,
                                         c_keyboard,
                                         c_vendor,
                                         c_product,
                                         c_version,
                                         c_name)

    def keyEvent(self, key, val):
        """
        Generate a key or btn event

        @param int axis         key or btn event (KEY_* or BTN_*)
        @param int val          event value
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_key(self._fd,
                             ctypes.c_uint16(key),
                             ctypes.c_int32(val))

    def axisEvent(self, axis, val):
        """
        Generate a abs event (joystick/pad axes)

        @param int axis         abs event (ABS_*)
        @param int val          event value
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_abs(self._fd,
                             ctypes.c_uint16(axis),
                             ctypes.c_int32(val))

    def relEvent(self, rel, val):
        """
        Generate a rel event (move move)

        @param int rel          rel event (REL_*)
        @param int val          event value
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_rel(self._fd,
                             ctypes.c_uint16(rel),
                             ctypes.c_int32(val))

    def scanEvent(self, val):
        """
        Generate a scan event (MSC_SCAN)

        @param int val          scan event value (scancode)
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_scan(self._fd,
                              ctypes.c_int32(val))

    def synEvent(self):
        """
        Generate a syn event
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_syn(self._fd)

    def setDelayPeriod(self, delay, period):
        """
        Update delay period values for keyboard

        @param int delay        delay in ms
        @param int period       period is ms
        """

        if self._fd == None:
            self.createDevice()

        self._lib.uinput_set_delay_period(self._fd,
                                          ctypes.c_int32(delay),
                                          ctypes.c_int32(period))

    def keyManaged(self, ev):
        return ev in self._k

    def axisManaged(self, ev):
        return ev in self._a

    def relManaged(self, ev):
        return ev in self._r

    def __del__(self):

        if self._lib and self._fd:
            self._lib.uinput_destroy(self._fd)
            self._fd = None
            _ctypes.dlclose(self._lib._handle)
            self._lib = None

class Gamepad(UInput):
    """
    Gamepad uinput class, create a Xbox360 gamepad device
    """

    def __init__(self):
        super(Gamepad, self).__init__(vendor=0x045e,
                                      product=0x028e,
                                      version=0x110,
                                      name=b"Microsoft X-Box 360 pad",
                                      keys=[Keys.BTN_START,
                                            Keys.BTN_MODE,
                                            Keys.BTN_SELECT,
                                            Keys.BTN_A,
                                            Keys.BTN_B,
                                            Keys.BTN_X,
                                            Keys.BTN_Y,
                                            Keys.BTN_TL,
                                            Keys.BTN_TR,
                                            Keys.BTN_THUMBL,
                                            Keys.BTN_THUMBR],
                                      axes=[(Axes.ABS_X, -32768, 32767, 16, 128),
                                            (Axes.ABS_Y, -32768, 32767, 16, 128),
                                            (Axes.ABS_RX, -32768, 32767, 16, 128),
                                            (Axes.ABS_RY, -32768, 32767, 16, 128),
                                            (Axes.ABS_Z, 0, 255, 0, 0),
                                            (Axes.ABS_RZ, 0, 255, 0, 0),
                                            (Axes.ABS_HAT0X, -1, 1, 0, 0),
                                            (Axes.ABS_HAT0Y, -1, 1, 0, 0)],
                                      rels=[])

class Mouse(UInput):

    """
    Mouse uinput class, create a mouse device

    moveEvent can emulate free ball rotation of a track ball
    updateParams permit to upgrade ball model and move scale
    """

    DEFAULT_FRICTION = 10.0
    DEFAULT_XSCALE = 0.006
    DEFAULT_YSCALE = 0.006

    DEFAULT_MEAN_LEN = 10

    DEFAULT_SCR_FRICTION = 10.0
    DEFAULT_SCR_XSCALE = 0.0005
    DEFAULT_SCR_YSCALE = 0.0005

    DEFAULT_SCR_MEAN_LEN = 10

    def __init__(self):
        super(Mouse, self).__init__(vendor=0x28de,
                                    product=0x1142,
                                    version=1,
                                    name=b"Steam Controller Mouse",
                                    keys=[Keys.BTN_LEFT,
                                          Keys.BTN_RIGHT,
                                          Keys.BTN_MIDDLE,
                                          Keys.BTN_SIDE,
                                          Keys.BTN_EXTRA],
                                    axes=[],
                                    rels=[Rels.REL_X,
                                          Rels.REL_Y,
                                          Rels.REL_WHEEL,
                                          Rels.REL_HWHEEL])
        self._dx = 0.0
        self._dy = 0.0
        self._xvel = 0.0
        self._yvel = 0.0
        self._lastTime = time.time()
        self.updateParams()

        self._scr_dx = 0.0
        self._scr_dy = 0.0
        self._scr_xvel = 0.0
        self._scr_yvel = 0.0
        self._scr_lastTime = time.time()
        self.updateScrollParams()

    def updateParams(self,
                     mass=80.0,
                     r=0.02,
                     friction=DEFAULT_FRICTION,
                     ampli=65536,
                     degree=40.0,
                     xscale=DEFAULT_XSCALE,
                     yscale=DEFAULT_YSCALE,
                     mean_len=DEFAULT_MEAN_LEN):
        """
        Update Movement parameters

        @param float mass       mass in g of the ball
        @param float r          radius in m of the ball
        @param float friction   constat friction force applied to the ball
        @param int ampli        integer amplitude for move from border to border
        @param float degree     degree of rotation of the ball for move from border to border
        @param float xscale     scale applied on move param to input event on x axis
        @param float yscale     scale applied on move param to input event on y axis
        """
        self._xscale = xscale
        self._yscale = yscale

        self._ampli  = ampli
        self._degree = degree
        self._radscale = (degree * pi / 180) / ampli
        self._mass = mass
        self._friction = friction
        self._rad = r
        self._I = (2 * self._mass * self._rad**2) / 5.0
        self._acc = self._rad * self._friction / self._I

        self._xvel_dq = deque(maxlen=mean_len)
        self._yvel_dq = deque(maxlen=mean_len)

    def updateScrollParams(self,
                           mass=20.0,
                           r=0.005,
                           friction=DEFAULT_SCR_FRICTION,
                           ampli=65536,
                           degree=120.0,
                           xscale=DEFAULT_SCR_XSCALE,
                           yscale=DEFAULT_SCR_YSCALE,
                           mean_len=DEFAULT_SCR_MEAN_LEN):
        """
        Update Scroll parameters

        @param float mass       mass in g of the ball
        @param float r          radius in m of the ball
        @param float friction   constat friction force applied to the ball
        @param int ampli        integer amplitude for move from border to border
        @param float degree     degree of rotation of the ball for move from border to border
        @param float xscale     scale applied on move param to input event on x axis
        @param float yscale     scale applied on move param to input event on y axis
        """
        self._scr_xscale = xscale
        self._scr_yscale = yscale
        self._scr_ampli  = ampli
        self._scr_degree = degree
        self._scr_radscale = (degree * pi / 180) / ampli
        self._scr_mass = mass
        self._scr_friction = friction
        self._scr_r = r
        self._scr_I = (2 * self._mass * self._rad**2) / 5.0
        self._scr_a = self._rad * self._friction / self._I

        self._scr_xvel_dq = deque(maxlen=mean_len)
        self._scr_yvel_dq = deque(maxlen=mean_len)

    def moveEvent(self, dx=0, dy=0, free=False):
        """
        Generate move events from parametters and displacement

        @param int dx           delta movement from last call on x axis
        @param int dy           delta movement from last call on y axis
        @param bool free        set to true for free ball move

        @return float           absolute distance moved this tick

        """
        # Compute time step
        _tmp = time.time()
        dt = _tmp - self._lastTime
        self._lastTime = _tmp

        def _genevt():
            _syn = False
            if int(self._dx):
                self.relEvent(rel=Rels.REL_X, val=int(self._dx))
                self._dx -= int(self._dx)
                _syn = True
            if int(self._dy):
                self.relEvent(rel=Rels.REL_Y, val=int(self._dy))
                self._dy -= int(self._dy)
                _syn = True
            if _syn:
                self.synEvent()

        if not free:
            # Compute mouse mouvement from interger part of d * scale
            self._dx += dx * self._xscale
            self._dy += dy * self._yscale

            _genevt()

            # Compute instant velocity
            try:
                self._xvel = sum(self._xvel_dq) / len(self._xvel_dq)
                self._yvel = sum(self._yvel_dq) / len(self._yvel_dq)
            except ZeroDivisionError:
                self._xvel = 0.0
                self._yvel = 0.0

            self._xvel_dq.append(dx * self._radscale / dt)
            self._yvel_dq.append(dy * self._radscale / dt)

        else:

            # Free movement update velocity and compute movement
            self._xvel_dq.clear()
            self._yvel_dq.clear()

            _hyp = sqrt((self._xvel**2) + (self._yvel**2))
            if _hyp != 0.0:
                _ax = self._acc * (abs(self._xvel) / _hyp)
                _ay = self._acc * (abs(self._yvel) / _hyp)
            else:
                _ax = self._acc
                _ay = self._acc

            # Cap friction desceleration
            _dvx = min(abs(self._xvel), _ax * dt)
            _dvy = min(abs(self._yvel), _ay * dt)

            # compute new velocity
            _xvel = self._xvel - copysign(_dvx, self._xvel)
            _yvel = self._yvel - copysign(_dvy, self._yvel)

            # compute displacement
            dx = (((_xvel + self._xvel) / 2) * dt) / self._radscale
            dy = (((_yvel + self._yvel) / 2) * dt) / self._radscale

            self._xvel = _xvel
            self._yvel = _yvel

            self._dx += dx * self._xscale
            self._dy += dy * self._yscale

            _genevt()

        return sqrt((dx**2) + (dy**2))

    def scrollEvent(self, dx=0, dy=0, free=False):
        """
        Generate scroll events from parametters and displacement

        @param int dx           delta movement from last call on x axis
        @param int dy           delta movement from last call on y axis
        @param bool free        set to true for free ball move

        @return float           absolute distance moved this tick

        """
        # Compute time step
        _tmp = time.time()
        dt = _tmp - self._scr_lastTime
        self._scr_lastTime = _tmp

        def _genevt():
            _syn = False
            if int(self._scr_dx):
                self.relEvent(rel=Rels.REL_HWHEEL, val=int(copysign(1, self._scr_dx)))
                self._scr_dx -= int(self._scr_dx)
                _syn = True
            if int(self._scr_dy):
                self.relEvent(rel=Rels.REL_WHEEL,  val=int(copysign(1, self._scr_dy)))
                self._scr_dy -= int(self._scr_dy)
                _syn = True
            if _syn:
                self.synEvent()
            return _syn

        if not free:
            # Compute mouse mouvement from interger part of d * scale
            self._scr_dx += dx * self._scr_xscale
            self._scr_dy += dy * self._scr_yscale

            _nev = _genevt()

            # Compute instant velocity
            try:
                self._scr_xvel = sum(self._scr_xvel_dq) / len(self._scr_xvel_dq)
                self._scr_yvel = sum(self._scr_yvel_dq) / len(self._scr_yvel_dq)
            except ZeroDivisionError:
                self._scr_xvel = 0.0
                self._scr_yvel = 0.0

            self._scr_xvel_dq.append(dx * self._scr_radscale / dt)
            self._scr_yvel_dq.append(dy * self._scr_radscale / dt)

        else:
            # Free movement update velocity and compute movement

            self._scr_xvel_dq.clear()
            self._scr_yvel_dq.clear()

            _hyp = sqrt((self._scr_xvel**2) + (self._scr_yvel**2))
            if _hyp != 0.0:
                _ax = self._scr_a * (abs(self._scr_xvel) / _hyp)
                _ay = self._scr_a * (abs(self._scr_yvel) / _hyp)
            else:
                _ax = self._scr_a
                _ay = self._scr_a

            # Cap friction desceleration
            _dvx = min(abs(self._scr_xvel), _ax * dt)
            _dvy = min(abs(self._scr_yvel), _ay * dt)

            # compute new velocity
            _xvel = self._scr_xvel - copysign(_dvx, self._scr_xvel)
            _yvel = self._scr_yvel - copysign(_dvy, self._scr_yvel)

            # compute displacement
            dx = (((_xvel + self._scr_xvel) / 2) * dt) / self._scr_radscale
            dy = (((_yvel + self._scr_yvel) / 2) * dt) / self._scr_radscale

            self._scr_xvel = _xvel
            self._scr_yvel = _yvel

            self._scr_dx += dx * self._scr_xscale
            self._scr_dy += dy * self._scr_yscale

            _nev = _genevt()

        return _nev

class Keyboard(UInput):
    """
    Keyboard uinput class, create a keyboard device.

    pressEvent permit to generate a key pressed and with scan events
    releaseEvent permit to generate a key released and with scan events

    autorepead delay and period are preset respectively to 250ms and 33ms
    setDelayPeriod permits to update these values
    """

    def __init__(self):
        super(Keyboard, self).__init__(vendor=0x28de,
                                       product=0x1142,
                                       version=1,
                                       name=b"Steam Controller Keyboard",
                                       keys=Scans.keys(),
                                       axes=[],
                                       rels=[],
                                       keyboard=True)
        self.setDelayPeriod(250, 33)
        self._dx = 0.0
        self._pressed = set()

    def pressEvent(self, keys):
        """
        Generate key press event with corresponding scan codes.
        Events are generated only for new keys.

        @param list of Keys keys        keys to press
        """

        new = [k for k in keys if k not in self._pressed]
        for i in new:
            self.scanEvent(Scans[i])
            self.keyEvent(i, 1)
        if len(new):
            self.synEvent()
            self._pressed |= set(new)

    def releaseEvent(self, keys=None):
        """
        Generate key release event with corresponding scan codes.
        Events are generated only for keys that was pressed

        @param list of Keys keys        keys to release, give None or empty list
                                        to release all
        """
        if keys and len(keys):
            rem = [k for k in keys if k in self._pressed]
        else:
            rem = list(self._pressed)
        for i in rem:
            self.scanEvent(Scans[i])
            self.keyEvent(i, 0)
        if len(rem):
            self.synEvent()
            self._pressed -= set(rem)


import numpy as np
import h5py
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from matplotlib.pyplot import imshow

import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt

# ----------------------------------------------------------------
class HappyCNN:
    def __init__(self, train=True, has_weights=False):
        # hyper parameter
        max_epoch = 40
        batch_size = 40

        X_train, y_train, X_test, y_test, classes = self.load_dataset()
        print(X_train.shape, y_train.shape)

        # Normalize image vectors
        self.X_train = X_train / 255.
        self.X_test = X_test / 255.

        # Reshape
        self.Y_train = y_train.T
        self.Y_test = y_test.T

        print("number of training examples = " + str(self.X_train.shape[0]))
        print("number of test examples = " + str(self.X_test.shape[0]))
        print("X_train shape: " + str(self.X_train.shape))
        print("Y_train shape: " + str(self.Y_train.shape))
        print("X_test shape: " + str(self.X_test.shape))
        print("Y_test shape: " + str(self.Y_test.shape))

        # create the model.
        self.model = self.BuildModel(X_train.shape[1:])
        # compile the model.
        self.model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

        if train:
            self.train(max_epoch, batch_size, has_weights)

    def load_dataset(self):
        train_dataset = h5py.File('datasets/train_happy.h5', "r")
        train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
        train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

        test_dataset = h5py.File('datasets/test_happy.h5', "r")
        test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
        test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

        classes = np.array(test_dataset["list_classes"][:])  # the list of classes

        train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
        test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

        return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

    def mean_pred(self, y_true, y_pred):
        return K.mean(y_pred)

    def BuildModel(self, input_shape):
        """
        Implementation of the Cifar10Model.

        Arguments:
        input_shape -- shape of the images of the dataset

        Returns:
        model -- a Model() instance in Keras
        """
        x_input = Input(input_shape)

        x = ZeroPadding2D((3, 3))(x_input)
        x = Conv2D(32, (7, 7), strides=(1, 1), name='conv0')(x)
        x = BatchNormalization(axis=3, name='btn0')(x)
        x = Activation('relu')(x)

        x = MaxPooling2D((2, 2), name='max_pool')(x)

        x = Flatten()(x)
        x = Dense(1, activation='sigmoid', name='fc')(x)

        model = Model(inputs=x_input, outputs=x, name='cifar10Model')

        return model

    def train(self, max_epoch, batch_size, has_weights):
        if has_weights:
            self.model.load_weights('happycnn.h5')

        self.model.fit(x=self.X_train, y=self.Y_train, epochs=max_epoch, batch_size=batch_size)
        self.model.save_weights('happycnn.h5')

    def test(self):
        self.model.load_weights('happycnn.h5')
        preds = self.model.evaluate(x=self.X_test, y=self.Y_test)
        print()
        print("Loss = " + str(preds[0]))
        print("Test Accuracy = " + str(preds[1]))

    def predict(self, path):
        # 'images/test.jpg'
        img_path = path
        img = image.load_img(img_path, target_size=(64, 64))
        imshow(img)

        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)

        self.model.load_weights('happycnn.h5')
        print(self.model.predict(x))

# ----------------------------------------------------------------
if __name__ == "__main__":
    model = HappyCNN(train=False, has_weights=True)
    model.test()
    model.predict('datasets/test.jpg')


##################################################
#-Pi Quick Tools
#-Coded on 18/08/21
#-Made by Splocko
#-Contributed by @R2Boyo25
#-https://github.com/Splocko
#################################################
tempWin= 'sudo bash /home/pi/Pi-Quick-Tools/Temp.sh'
import os
import gi
import time
gi.require_version('Gtk','3.0')
from gi.repository import Gtk
class window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self,title='Pi Quick Tools')
        Gtk.Window.set_default_size(self, 640, 480)

        self.box = Gtk.Box(spacing=6)
        self.add(self.box)

        self.addButton('Free up space', 'sudo apt -y autoremove\nsudo apt purge\nsudo apt clean', 'Cleaning APT...')
        self.addButton('Update All Packages', 'sudo apt update\nsudo apt -y upgrade', 'Updating all packages... This may take a long time.')
        self.addButton('Reboot', 'sudo reboot')
        self.addButton('Shutdown', 'sudo shutdown now')
        self.addButton('Raspberry Pi Config', f'bash {script_dir}/rpicnfgscrpt.sh')
        self.addButton('Temperature', 'sudo bash /home/pi/Pi-Quick-Tools/Temp.sh', 'Temperature opened')
        self.addButton('Cpu Stats','sudo bash /home/pi/Pi-Quick-Tools/Htopstats.sh', 'Stats opened')

    def runCommands(self, button, commands, successText, failText, startMessage = None) -> None:
        if startMessage:
            print(startMessage)
        failed = False
        for command in commands:
            if os.system(command.strip()) != 0:
                failed = True
        if failed:
            print(failText)
        else:
            print(successText)

    def addButton(self, buttonName, action, startMessage = None, successText = None, failText = None) -> None:

        if not failText:
            failText = f'{buttonName} failed...'
        if not successText:
            successText = f'{buttonName} successful...'

        if str(type(action)) == "<class 'function'>":
            button = Gtk.Button(label = buttonName)
            button.connect('clicked', action)
            self.box.pack_start(button, True, True, 0)
        else:
            button = Gtk.Button(label = buttonName)
            if not type(action) == list:
                if '\n' in action:
                    converted = action.split('\n')
                else:
                    converted = [action]
            else:
                converted = action
            button.connect('clicked', self.runCommands, converted, successText, failText, startMessage)
            self.box.pack_start(button, True, True, 0)

    def on_tempButton_clicked(self, widget):
        def measure_temp() -> str:
            temp = os.popen("vcgencmd measure_temp").readline()
            return (temp.replace("temp=",""))

        while True:
            self.temperature = measure_temp()
            self.tempButton = Gtk.Button(label= f'Temperature: {self.temperature}')
            time.sleep(1)

script_dir = os.path.abspath(os.getcwd())

win = window()
win.connect('destroy', Gtk.main_quit)
win.show_all()
Gtk.main()


# coding=utf-8
"""这个模块用来返回搜索结果"""
from whoosh.index import create_in,open_dir
from whoosh import query,qparser
from whoosh.fields import *
from jieba.analyse import ChineseAnalyzer
import json
from generateIndex import pageRank
from resultsProcess import resultProcess

def searchName(name):
    # 存储schema信息至'indexdir'目录下
    indexdir = 'indexdir/'
    ix=open_dir(indexdir)
    # 创建一个检索器
    searcher = ix.searcher()

    # 检索标题中出现'文档'的文档
    results = searcher.find("name",name)

    results=resultProcess(results)

    return results

def searchIntro(query_string):
    # 存储schema信息至'indexdir'目录下
    indexdir = 'indexdir/'
    ix=open_dir(indexdir)
    # 创建一个检索器
    searcher = ix.searcher()

    #对intro和basic共同进行搜索，得到的结构合并
    qp1 = qparser.QueryParser("intro", ix.schema)
    qp2 = qparser.QueryParser('basic', ix.schema)
    user_q1 = qp1.parse(query_string)
    user_q2 = qp2.parse(query_string)

    results1 = searcher.search(user_q1,limit=None)
    results = searcher.search(user_q2,limit=None)
    results.extend(results1)

    results = resultProcess(results)
    # 检索标题中出现'文档'的文档
    return results


import numpy as np
import torch
from torchvision.utils import save_image
from tqdm import tqdm

from model import Generator
from sampling import sample, get_cycling_grid_same, n_classes

device = 'cuda'

model = Generator(n_feat=36, codes_dim=24, n_classes=n_classes).to(device)
model.load_state_dict(torch.load('generator.pth'))

height = 8
width = 15
cycles = 8
frames_per_half_cycle = 24*4
all_zs = get_cycling_grid_same(height, width, cycles, frames_per_half_cycle)

for f_idx in tqdm(range(all_zs.size(2))):
    z_batch = all_zs[:, :, f_idx]
    z_batch = z_batch.view(-1, 120)

    aux_labels = np.arange(120)

    images = sample(model, z_batch, aux_labels=aux_labels)

    frame = torch.empty((3, height * 64, width * 64), device=device)
    for h in range(height):
        for w in range(width):
            frame[:, h * 64:(h + 1) * 64, w * 64:(w + 1) * 64] = images[h * width + w]
    save_image(frame, 'breed_frames/{}.png'.format(f_idx))


#!/usr/bin/env python3
# Converts the Bazaar CSV copied from https://ffxii.us/bazaar into a JSON file
import csv
import json

def convert():
    data = []
    with open('bazaar.csv') as f:
        reader = csv.reader(f)
        id = 1
        for row in reader:
            package, price, loot, reward = row
            loot = split_items(loot)
            reward = split_items(reward)
            data.append({
                'id': id,
                'package': package,
                'price': price,
                'loot': loot,
                'reward': reward
            })
            id += 1
    with open('bazaar.json', 'w') as f:
        json.dump(data, f, indent=2)

def split_items(raw):
    rows = raw.split('\n')
    pairs = [row.split(' x') for row in rows]
    items = [{
        'name': p[0],
        'quantity': int(p[1]) if len(p) > 1 else 1
    } for p in pairs]
    return items

if __name__ == "__main__":
    convert()


from telethon import events
from .. import jdbot, chat_id, _ConfigDir
from .utils import cmd

@jdbot.on(events.NewMessage(from_users=chat_id, pattern='^/up$'))
async def myup(event):
    try:
        msg = await jdbot.send_message(chat_id,'开始更新程序，请稍候')
        await cmd(f'bash {_ConfigDir}/bot.sh')
        await jdbot.delete_messages(chat_id,msg)
    except Exception as e:
        await jdbot.send_message(chat_id,str(e))


from PIL import Image
import cv2
import imutils

image = cv2.imread(r".jpg")
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])

# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image and initialize the
# shape detector
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = imutils.grab_contours(cnts)

image2=Image.open('cloth.jpg')
peri = cv2.arcLength(cnts, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
(x, y, w, h) = cv2.boundingRect(approx)

croppedIm = image2.crop((x,y,x+w,y+h))

croppedIm.save('cropped.jpg')


#! /usr/bin/env python
# coding=utf-8
#================================================================
#   Copyright (C) 2019 * Ltd. All rights reserved.
#
#   Editor      : VIM
#   File name   : torch_test.py
#   Author      : YunYang1994
#   Created date: 2019-07-30 11:39:32
#   Description :
#
#================================================================

import torch
import torchvision
import torchvision.transforms as transforms
from torch_model import torch_model

# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

test_dataset = torchvision.datasets.MNIST(root='../../data/',
                                          train=False,
                                          transform=transforms.ToTensor())

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=100,
                                          shuffle=False)
# Prepare model
torch_model = torch_model.to(device)
state_dict = torch.load("./model.pth")
torch_model.load_state_dict(state_dict)

# Test the model
torch_model.eval()  # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = torch_model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('=> Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))


from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
import uuid

class Game(models.Model):
	name = models.CharField(max_length=200, default='None', blank=False, null=True)
	description = models.TextField(default='Describe the Game Here!', blank=False, null=True)
	number_of_participants = models.PositiveIntegerField(verbose_name='Number Of Participants', blank=False, null=True)
	estimated_length = models.PositiveIntegerField(verbose_name='Estimated Length', default=10, blank=False, help_text='Minutes')
	communities = models.ManyToManyField(CommunityInstance )

	def __str__(self):
		return '{}'.format(self.name)

class Talent(models.Model):
	name = models.CharField(primary_key=True, max_length=200, help_text='Use what was done, Piano, Story, Violion, Etc', blank=False)
	description = models.TextField(default='Describe the talent here!', blank=True, null=True)
	author = models.CharField(max_length=200, help_text='Name the Preformer (optional)', blank=True, null=True)

	def __str__(self):
		return '{}'.format(self.name)

class CommunityInstance(models.Model):
	id = models.SlugField(primary_key=True, default=uuid.uuid4, unique=True)
	date = models.DateField(blank=False)

	number_of_games = models.PositiveIntegerField(blank=False, default=1)
	games = models.ManyToManyField(Game, help_text='Select the games that appeard this community!', blank=True)

	number_of_announcements = models.PositiveIntegerField(default=3, blank=False)
	length_of_announcements = models.PositiveIntegerField(default=10, help_text='minutes')
	announcements_made = models.TextField()

	host = models.CharField(max_length=20)

	number_of_talent = models.PositiveIntegerField(blank=False, verbose_name="Number of Student Preformances")
	talent = models.ManyToManyField(Talent, blank=True, help_text='Enter all the talent that was preformed, if there was no talent preformed, don\'t select anything. If the talent that was preformed is not listed, use the + button to add it to the list.')

	number_of_it_awards = models.PositiveIntegerField(blank=False, default=0, verbose_name='Independent Thinker Awards')
	number_of_pc_awards = models.PositiveIntegerField(blank=False, default=0, verbose_name='Profiles in Courage Awards')
	number_of_ds_awards = models.PositiveIntegerField(blank=False, default=0, verbose_name='Durkheim Scholar Awards')
	number_of_gp_awards = models.PositiveIntegerField(blank=False, default=0, verbose_name='Gary Phillips Awards')

	minutes_end_early = models.PositiveIntegerField(default=15, help_text='in minutes', verbose_name="Minutes Left When Community Ended")

	class Meta:
		verbose_name = 'Community Instance'
		verbose_name_plural = 'Community Instances'

	def __str__(self):
		return '{}'.format(self.date)

class CommunityInstanceReview(models.Model):
	user = models.ForeignKey(User, blank=False)
	community = models.ForeignKey(CommunityInstance, blank=False)


from rest_framework import viewsets, generics, views, response
from serializers import *
from django.db.models import Count

class PlayerViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows players to be viewed or edited.
    """
    queryset = Player.objects.all()
    serializer_class = PlayerSerializer

class DartViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows darts to be viewed or edited.
    """
    queryset = Dart.objects.all()
    serializer_class = DartSerializer

class ContestViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows darts to be viewed or edited.
    """
    queryset = Contest.objects.all()
    serializer_class = ContestSerializer

class ShotViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows darts to be viewed or edited.
    """
    queryset = Shot.objects.all()
    serializer_class = ShotSerializer

class PlayerListRandom(generics.ListAPIView):
    """
    API endpoint that lists players in a random order.
    """
    queryset = Player.objects.order_by('?')
    serializer_class = PlayerSerializer

class PlayerAccuracyList(views.APIView):
    """
    API endpoint that lists players by accuracy.
    """
    queryset = Player.objects.all()

    def get(self, request, format=None):
        players = list(self.queryset)
        players = sorted(players, key=lambda p: p.get_accuracy(), reverse=True)
        accuracies = [{"id": player.id, "name": player.name, "accuracy": player.get_accuracy()} for player in players]
        return response.Response(accuracies)

class PlayerHitsList(views.APIView):
    """
    API endpoint that lists players by hits.
    """
    queryset = Player.objects.all()

    def get(self, request, format=None):
        players = list(self.queryset)
        players = sorted(players, key=lambda p: p.get_hits(), reverse=True)
        hits = [{"id": player.id, "name": player.name, "hits": player.get_hits()} for player in players]
        return response.Response(hits)

class PlayerWinningsList(views.APIView):
    """
    API endpoint that lists players by total winnings.
    """
    queryset = Player.objects.all()

    def get(self, request, format=None):
        players = list(self.queryset)
        players = sorted(players, key=lambda p: p.get_total_winnings(), reverse=True)
        winnings = [{
            "id": player.id,
            "name": player.name,
            "winnings": player.get_total_winnings()
        } for player in players]
        return response.Response(winnings)

class WinningShotsList(views.APIView):
    """
    API endpoint that lists winning shots by amount won.
    """
    queryset = Shot.objects.all()

    def get(self, request, format=None):
        shots = list(self.queryset.filter(hit=True))
        shots = sorted(shots, key=lambda s: s.get_winnings(), reverse=True)
        winners = [{
            "id": shot.id,
            "player": shot.player.name,
            "date": shot.contest.date,
            "winnings": shot.get_winnings()
        } for shot in shots]
        return response.Response(winners)

class DartAccuracyList(views.APIView):
    """
    API endpoint that lists darts by accuracy.
    """
    queryset = Dart.objects.all()

    def get(self, request, format=None):
        darts = list(self.queryset)
        darts = sorted(darts, key=lambda d: d.get_accuracy(), reverse=True)
        accuracies = [{"id": dart.id, "name": dart.name, "accuracy": dart.get_accuracy()} for dart in darts]
        return response.Response(accuracies)

class FavoriteDartList(views.APIView):
    """
    API endpoint that lists each player's favorite dart.
    """
    queryset = Player.objects.all()

    def get(self, request, format=None):
        favorites = []
        for player in self.queryset:
            try:
                shots = player.shot_set.values("dart").annotate(num_shots=Count("dart")).order_by('-num_shots')[0]
                dart = Dart.objects.get(pk=shots.get('dart'))
                favorites.append({
                    "player_id": player.id,
                    "player_name": player.name,
                    "dart_id": dart.id,
                    "dart_name": dart.name,
                    "num_shots": shots.get('num_shots')
                })
            except IndexError:
                pass

        return response.Response(favorites)

class PotCarryoverView(views.APIView):
    """
    API endpoint that shows the amount of money carried over from the last contest.
    """
    queryset = Contest.objects.order_by('-date')

    def get(self, request, format=None):
        try:
            last_contest = self.queryset[0]
            if len(last_contest.get_winners()) > 0:
                pot = 0
            else:
                pot = last_contest.get_pot_end()
        except IndexError:
            pot = 0

        return response.Response({"pot_carryover": Decimal(pot)})


import logging

# 定义类存储日志信息
def get_logger(filename, print2screen=True):
    # 创建一个logger
    logger = logging.getLogger(filename)
    # 设置logger级别为INFO
    logger.setLevel(logging.INFO)
    # 将日志信息输出到磁盘文件上
    fh = logging.FileHandler(filename)
    # 输出到file的logging等级的开关
    fh.setLevel(logging.INFO)
    # 日志信息会输出到指定的stream中，如果stream为空则默认输出到sys.stderr
    ch = logging.StreamHandler()
    # 输出到sys.stderr的logging等级的开关
    ch.setLevel(logging.INFO)
    # 定义handler的输出格式
    formatter = logging.Formatter('[%(asctime)s][%(thread)d][%(filename)s][line: %(lineno)d][%(levelname)s] \
>> %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # 将logger添加到handler里面
    logger.addHandler(fh)
    # 如果需要输出到控制台，就将ch加入，并返回
    if print2screen:
        logger.addHandler(ch)
    return logger


from numba import jit
import numpy as np

@jit(nopython=True)
def matrix_multiply(X, Y):
    """ Matrix multiplication
    Inputs:
      - X: A numpy array of shape (N, M)
      - Y: A numpy array of shape (M, K)
    Output:
      - out: A numpy array of shape (N, K)
    """
    N = X.shape[0]
    M = X.shape[1]
    K = Y.shape[1]
    Z = np.zeros((N, K), dtype=np.float64)
    z = 0.0;
    for i in range(N):
        for j in range(K):
            z = 0.0
            for k in range(M):
                z += X[i, k] * Y[k, j]
            Z[i, j] = z
    return Z

@jit(nopython=True)
def matrix_rowmean(X, weights=np.empty(0)):
    """ Calculate mean of each row.
    In case of weights do weighted mean.
    For example, for matrix [[1, 2, 3]] and weights [0, 1, 2]
    weighted mean equals 2.6666 (while ordinary mean equals 2)
    Inputs:
      - X: A numpy array of shape (N, M)
      - weights: A numpy array of shape (M,)
    Output:
      - out: A numpy array of shape (N,)
    """
    N = X.shape[0]
    M = X.shape[1]
    Y = np.zeros((N), dtype=np.float64)
    s = 0.0;
    d = 0.0;
    W = np.zeros((M), dtype=np.float64)
    if weights.size:
        for l, x in enumerate(weights):
            W[l] = x
            d += x
        for i in range(N):
            s = 0.0
            for j in range(M):
                s += X[i, j] * W[j]
            Y[i] = s / d
    else:
        d = M
        for i in range(N):
            s = 0.0
            for j in range(M):
                s += X[i, j]
            Y[i] = s / d
    return Y

@jit(nopython=True)
def cosine_similarity(X, top_n=10, with_mean=True, with_std=True):
    """ Calculate cosine similarity between each pair of row.
    1. In case of with_mean: subtract mean of each row from row
    2. In case of with_std: divide each row on it's std
    3. Select top_n best elements in each row or set other to zero.
    4. Compute cosine similarity between each pair of rows.
    Inputs:
      - X: A numpy array of shape (N, M)
      - top_n: int, number of best elements in each row
      - with_mean: bool, in case of subtracting each row's mean
      - with_std: bool, in case of subtracting each row's std
    Output:
      - out: A numpy array of shape (N, N)

    Example (with top_n=1, with_mean=True, with_std=True):
        X = array([[1, 2], [4, 3]])
        after mean and std transform:
        X = array([[-1.,  1.], [ 1., -1.]])
        after top n choice
        X = array([[0.,  1.], [ 1., 0]])
        cosine similarity:
        X = array([[ 1.,  0.], [ 0.,  1.]])

    """
    N = X.shape[0]
    M = X.shape[1]
    Z = np.zeros((N, 1), dtype=np.float64)
    ZZ = np.zeros((N, N), dtype=np.float64)
    XX = np.zeros((N, N), dtype=np.float64)
    answer = np.zeros((N, N), dtype=np.float64)
    mean = 0.0;
    std = 0.0;
    d = M;

    if with_mean:
        for i in range(N):
            mean = 0.0
            for j in range(M):
                mean += X[i, j]
            for j in range(M):
                X[i, j] -= mean / d
    if with_std:
        for i in range(N):
            mean = 0.0
            for j in range(M):
                mean += X[i, j]
            mean /= d
            std = 0.0
            for j in range(M):
                std += (X[i, j] - mean) * (X[i, j] - mean)
            std = np.sqrt(std / d)
            for j in range(M):
                X[i, j] /= std
    Y = np.zeros((N, M - top_n), dtype=np.int32)
    for i in range(N):
        Y[i] = np.argsort(X[i])[0:M - top_n]
    for i in range(N):
        X[i][Y[i]] = 0.0
    for i in range(N):
        a = 0.0
        for j in range(M):
            a += X[i, j] * X[i, j]
        Z[i, 0] = np.sqrt(a)
    ZZ = matrix_multiply(Z, Z.T)
    XX = matrix_multiply(X, X.T)
    for i in range(N):
        for j in range(N):
            answer[i, j] = XX[i, j] / ZZ[i, j]
    return answer


import collections
import random

class Foo:
    def __contains__(self, x):
        return random.random() > 0.5

def main():
    foo = Foo()
    print(isinstance(foo, collections.abc.Container)) #-> True
    lis = [x in foo for x in range(16)]
    print(lis)

if __name__ == "__main__":
    main()


import itertools
import typing as t

def solve(puzzle: str) -> t.Optional[t.Dict[str, int]]:
    words = [word for word in puzzle.split() if word.isalpha()]
    first_letters = set(word[0] for word in words)
    other_letters = set("".join(words)) - first_letters
    letters = "".join(first_letters) + "".join(other_letters)

    for first_perm in itertools.permutations(
        map(str, range(1, 10)), len(first_letters)
    ):
        numbers = set(map(str, range(10))).difference(first_perm)
        for other_perm in itertools.permutations(numbers, len(other_letters)):
            context = str.maketrans(letters, "".join(first_perm + other_perm))
            if eval(puzzle.translate(context)) is True:
                return dict(zip(letters, map(int, first_perm + other_perm)))

    return None


from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import numpy as np
import matplotlib.pyplot as plt

from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False

# _URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'

# path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)
path_to_zip = os.path.dirname(os.path.abspath(__file__))

# PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')
PATH = os.path.join(os.path.dirname(path_to_zip), "real_and_deepfake")

train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_real_dir = os.path.join(train_dir, 'real')  # directory with our training real pictures
train_deepfake_dir = os.path.join(train_dir, 'deepfake')  # directory with our training deepfake pictures
validation_real_dir = os.path.join(validation_dir, 'real')  # directory with our validation real pictures
validation_deepfake_dir = os.path.join(validation_dir, 'deepfake')  # directory with our validation deepfake pictures

num_real_tr = len(os.listdir(train_real_dir))
num_deepfake_tr = len(os.listdir(train_deepfake_dir))

num_real_val = len(os.listdir(validation_real_dir))
num_deepfake_val = len(os.listdir(validation_deepfake_dir))

total_train = num_real_tr + num_deepfake_tr
total_val = num_real_val + num_deepfake_val

print('total training real images:', num_real_tr)
print('total training deepfake images:', num_deepfake_tr)

print('total validation real images:', num_real_val)
print('total validation deepfake images:', num_deepfake_val)
print("--")
print("Total training images:", total_train)
print("Total validation images:", total_val)

batch_size = 128
epochs = 1
IMG_HEIGHT = 150
IMG_WIDTH = 150

train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                           directory=train_dir,
                                                           shuffle=True,
                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                           class_mode='binary')

val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
                                                              directory=validation_dir,
                                                              target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                              class_mode='binary')

sample_training_images, _ = next(train_data_gen)

# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
        ax.axis('off')
    plt.tight_layout()
    # plt.show()

plotImages(sample_training_images[:5])

model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['acc'])

model.summary()

history = model.fit_generator(
    train_data_gen,
    steps_per_epoch=total_train // batch_size,
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps=total_val // batch_size
)

acc = history.history['acc']
val_acc = history.history['val_acc']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
# plt.show()

image_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)

train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_HEIGHT, IMG_WIDTH))

augmented_images = [train_data_gen[0][0][0] for i in range(5)]

# Re-use the same custom plotting function defined and used
# above to visualize the training images
plotImages(augmented_images)

image_gen = ImageDataGenerator(rescale=1./255, rotation_range=45)

train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_HEIGHT, IMG_WIDTH))

augmented_images = [train_data_gen[0][0][0] for i in range(5)]

plotImages(augmented_images)

image_gen_train = ImageDataGenerator(
                    rescale=1./255,
                    rotation_range=45,
                    width_shift_range=.15,
                    height_shift_range=.15,
                    horizontal_flip=True,
                    zoom_range=0.5
                    )

train_data_gen = image_gen_train.flow_from_directory(batch_size=batch_size,
                                                     directory=train_dir,
                                                     shuffle=True,
                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                     class_mode='binary')

augmented_images = [train_data_gen[0][0][0] for i in range(5)]
plotImages(augmented_images)

image_gen_val = ImageDataGenerator(rescale=1./255)

val_data_gen = image_gen_val.flow_from_directory(batch_size=batch_size,
                                                 directory=validation_dir,
                                                 target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                 class_mode='binary')

model_new = Sequential([
    Conv2D(16, 3, padding='same', activation='relu',
           input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Dropout(0.2),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1, activation='sigmoid')
])

model_new.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model_new.summary()

history = model_new.fit_generator(
    train_data_gen,
    steps_per_epoch=total_train // batch_size,
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps=total_val // batch_size
)

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn import metrics
from sklearn import calibration

import scipy.interpolate as interpolate

################################################################################
#                 Standard formulas for compound interest                      #
################################################################################

def interest_payments(amount, rate, term):
    """Total interest the borrower will pay if they pay on time monthly.
    See https://brownmath.com/bsci/loan.htm

    Parameters
    ----------
    amount : Amount loaned
    rate : interest rate fraction/year (decimal, not percent)
    term : Length of loan (years)

    Returns
    -------
    interest : Interest paid
    """
    # interest rate (fraction/month)
    i = rate/12.0
    # Number of payments
    N = term*12.0
    # Total payment per month
    P = i*amount / (1.0 - (1.0+i)**(-N))

    return P*N-amount

def calculate_interest_rate(amount_start, amount_end, term):
    """Calculate the interest rate for a loan paid monthly.

    Parameters
    ----------
    amount_start : Starting value of investment (loan amount)
    amount_end : End value of investment (total payed back including interest)
    term : length of loan in years

    Returns
    -------
    rate : interest rate in units of fraction/year
    """
    # Number of payments
    N = term*12.0

    P = amount_end/N
    y = P/amount_start

    ilist = np.logspace(-5, -1, 1000)
    ylist = np.array([i / (1.0 - (1+i)**(-N)) for i in ilist])
    iofy = interpolate.UnivariateSpline(ylist, ilist, k=1, s=0)
    i = iofy(y)
    return i*12

################################################################################
#                      Expectation value for payments                          #
################################################################################

def expectation_value_payment(loan_amount, rate, term, p_default, frac_recovered):
    """The expectation value of the amount the borrower pays back.

    Parameters
    ----------
    loan_amount : Amount loaned
    rate : interest rate fraction/year (decimal, not percent)
    term : Length of loan (years)
    p_default : probability of p_default
    frac_recovered : Estimate of the fraction recovered of the total owed
        (including interest) if the borrower defaults.

    Returns
    -------
    E[payment]
    """
    interest = interest_payments(loan_amount, rate, term)

    # Amount if borrower pays the loan in full
    total_paid_in_full = loan_amount + interest

    # Amount if borrower does not pay the loan in full
    total_not_paid_in_full = frac_recovered * total_paid_in_full

    return (1.0-p_default)*total_paid_in_full + p_default*total_not_paid_in_full

################################################################################
#                 Predict payments from a portfolio of loans                   #
################################################################################

def predict_default_probability(df, cal_clf, scaler):
    """Predict probability of default for all loans in the DataFrame.

    Parameters
    ----------
    df : DataFrame
    cal_clf : CalibratedClassifierCV object
    scaler : Scaler object

    Returns
    -------
    p_defaults : 1d-array
        Probability of default for each row in DataFrame
    """
    X = df.drop(['bad_loan', 'issue_d','total_rec_prncp', 'total_rec_int'], axis=1).values
    X = scaler.transform(X)
    p_defaults = cal_clf.predict_proba(X)[:, 1]

    return p_defaults

def total_payments_in_portfolio_predicted(
    df, cal_clf, scaler,
    frac_recovered=0.417, term=3.0):
    """Predicted amount the borrowers will pay back.

    Parameters
    ----------
    df : DataFrame
    cal_clf : CalibratedClassifierCV object
    scaler : Scaler object
        Feature scaling
    frac_recovered : float
        Amount you expect to recover on average if the borrowers default.
    term : float
        Length of loan in years

    Returns
    -------
    dict : dictionary of payment quantities
    """
    loan_amounts = df['loan_amnt'].values
    loan_rates = df['int_rate'].values / 100.0

    # Predict the probability of default
    p_defaults = predict_default_probability(df, cal_clf, scaler)

    # Predict the total payments you will receive
    tot_pay = 0.0
    nloans = len(loan_amounts)
    for i in range(nloans):
        loan_amount = loan_amounts[i]
        rate = loan_rates[i]
        p_default = p_defaults[i]
        tot_pay += expectation_value_payment(loan_amount, rate, term, p_default, frac_recovered)

    # Other quantities of interest
    tot_loan_amounts = np.sum(loan_amounts)
    tot_gains = tot_pay - tot_loan_amounts
    rate_eff = calculate_interest_rate(tot_loan_amounts, tot_pay, term)

    return {'tot_loan_amounts':tot_loan_amounts, 'tot_pay':tot_pay,
            'tot_gains':tot_gains, 'rate_eff':rate_eff}

def total_payments_in_portfolio_known(df, term=3.0):
    """The total amount that the borrowers did pay back.

    Parameters
    ----------
    df : DataFrame
    term : float
        Length of loan in years

    Returns
    -------
    dict : dictionary of payment quantities
    """
    loan_amounts = df['loan_amnt'].values
    total_principle_paid = df['total_rec_prncp'].values
    total_interest_paid = df['total_rec_int'].values

    tot_loan_amounts = np.sum(loan_amounts)
    tot_pay = np.sum(total_principle_paid) + np.sum(total_interest_paid)
    tot_gains = tot_pay - tot_loan_amounts
    rate_eff = calculate_interest_rate(tot_loan_amounts, tot_pay, term)

    return {'tot_loan_amounts':tot_loan_amounts, 'tot_pay':tot_pay,
            'tot_gains':tot_gains, 'rate_eff':rate_eff}

################################################################################
#                    Methods for optimally selecting loans                     #
################################################################################

def get_lowest_default_probability_loans(df, cal_clf, scaler, n):
    """Sort the loans by default probability then pick the n lowest.

    Parameters
    ----------
    df : DataFrame
    cal_clf : CalibratedClassifierCV object
    scaler : Scaler object
    n : int
        Number of loans.

    Returns
    -------
    p_default_chosen : 1d array
        Default probability of chosen loans
    df_chosen : DataFrame
        Data for the chosen loans
    """
    p_defaults = predict_default_probability(df, cal_clf, scaler)

    # Indices of n loans with lowest default probability
    p_sort_i = np.argsort(p_defaults)
    chosen_i = p_sort_i[:n]

    p_default_chosen = p_defaults[chosen_i]
    df_chosen = df.iloc[chosen_i, :]

    return p_default_chosen, df_chosen

def get_highest_predicted_roi_loans(df, cal_clf, scaler, n,
                                    frac_recovered=0.417, term=3.0):
    """Sort the loans by expected ROI in descending order,
    then pick the n highest.

    Parameters
    ----------
    df : DataFrame
    cal_clf : CalibratedClassifierCV object
    scaler : Scaler object
    n : int
        Number of loans.

    Returns
    -------
    rate_chosen : 1d array
        Expectation value for ROI of chosen loans
    df_chosen : DataFrame
        Data for the chosen loans
    """
    loan_amounts = df['loan_amnt'].values
    loan_rates = df['int_rate'].values / 100.0

    # Predict the probability of default
    p_defaults = predict_default_probability(df, cal_clf, scaler)

    # Predict the effective ROI
    rate_eff = []
    nloans = len(loan_amounts)
    for i in range(nloans):
        loan_amount = loan_amounts[i]
        rate = loan_rates[i]
        p_default = p_defaults[i]
        e_pay = expectation_value_payment(loan_amount, rate, term, p_default, frac_recovered)
        r = calculate_interest_rate(loan_amount, e_pay, term)
        rate_eff.append(r)
    rate_eff = np.array(rate_eff)

    # Indices of n loans with highest expected ROI
    rate_sort_i = np.flip(np.argsort(rate_eff), axis=0)
    chosen_i = rate_sort_i[:n]

    rate_chosen = rate_eff[chosen_i]
    df_chosen = df.iloc[chosen_i, :]

    return rate_chosen, df_chosen


from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('./templates/'),
    autoescape=select_autoescape(['html'])
)

template = env.get_template('receipt.html')

output = template.render({
 "category": "Meals",
 "date": "June 23,2019",
 "merchant": "Momofuko Ko",
 "total": 560.75,
 "date": "8:30 AM Wednesday, July 17 2017",
 "purpose": "",
 "location": "3617 Tully Street, Livonia, MI",
 "attendees_list": ["John Doe","Jane Doe"]
})

with open('my_new_html_file.html', 'w') as f:
    f.write(output)


from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers

from nautobot.core.api import (
    ChoiceField,
    SerializedPKRelatedField,
    ValidatedModelSerializer,
)
from nautobot.dcim.api.nested_serializers import (
    NestedDeviceRoleSerializer,
    NestedPlatformSerializer,
    NestedSiteSerializer,
)
from nautobot.dcim.choices import InterfaceModeChoices
from nautobot.extras.api.customfields import CustomFieldModelSerializer
from nautobot.extras.api.serializers import (
    StatusModelSerializerMixin,
    TaggedObjectSerializer,
)
from nautobot.extras.api.nested_serializers import NestedConfigContextSchemaSerializer
from nautobot.ipam.api.nested_serializers import (
    NestedIPAddressSerializer,
    NestedVLANSerializer,
)
from nautobot.ipam.models import VLAN
from nautobot.tenancy.api.nested_serializers import NestedTenantSerializer
from nautobot.virtualization.choices import *
from nautobot.virtualization.models import (
    Cluster,
    ClusterGroup,
    ClusterType,
    VirtualMachine,
    VMInterface,
)
from .nested_serializers import *

#
# Clusters
#

class ClusterTypeSerializer(CustomFieldModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="virtualization-api:clustertype-detail")
    cluster_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = ClusterType
        fields = [
            "id",
            "url",
            "name",
            "slug",
            "description",
            "cluster_count",
            "custom_fields",
            "created",
            "last_updated",
            "computed_fields",
        ]
        opt_in_fields = ["computed_fields"]

class ClusterGroupSerializer(CustomFieldModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="virtualization-api:clustergroup-detail")
    cluster_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = ClusterGroup
        fields = [
            "id",
            "url",
            "name",
            "slug",
            "description",
            "cluster_count",
            "custom_fields",
            "created",
            "last_updated",
            "computed_fields",
        ]
        opt_in_fields = ["computed_fields"]

class ClusterSerializer(TaggedObjectSerializer, CustomFieldModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="virtualization-api:cluster-detail")
    type = NestedClusterTypeSerializer()
    group = NestedClusterGroupSerializer(required=False, allow_null=True)
    tenant = NestedTenantSerializer(required=False, allow_null=True)
    site = NestedSiteSerializer(required=False, allow_null=True)
    device_count = serializers.IntegerField(read_only=True)
    virtualmachine_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Cluster
        fields = [
            "id",
            "url",
            "name",
            "type",
            "group",
            "tenant",
            "site",
            "comments",
            "tags",
            "custom_fields",
            "created",
            "last_updated",
            "device_count",
            "virtualmachine_count",
            "computed_fields",
        ]
        opt_in_fields = ["computed_fields"]

#
# Virtual machines
#

class VirtualMachineSerializer(TaggedObjectSerializer, StatusModelSerializerMixin, CustomFieldModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="virtualization-api:virtualmachine-detail")
    site = NestedSiteSerializer(read_only=True)
    cluster = NestedClusterSerializer()
    role = NestedDeviceRoleSerializer(required=False, allow_null=True)
    tenant = NestedTenantSerializer(required=False, allow_null=True)
    platform = NestedPlatformSerializer(required=False, allow_null=True)
    primary_ip = NestedIPAddressSerializer(read_only=True)
    primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True)
    primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True)
    local_context_schema = NestedConfigContextSchemaSerializer(required=False, allow_null=True)

    class Meta:
        model = VirtualMachine
        fields = [
            "id",
            "url",
            "name",
            "status",
            "site",
            "cluster",
            "role",
            "tenant",
            "platform",
            "primary_ip",
            "primary_ip4",
            "primary_ip6",
            "vcpus",
            "memory",
            "disk",
            "comments",
            "local_context_data",
            "local_context_schema",
            "tags",
            "custom_fields",
            "created",
            "last_updated",
            "computed_fields",
        ]
        validators = []
        opt_in_fields = ["computed_fields"]

class VirtualMachineWithConfigContextSerializer(VirtualMachineSerializer):
    config_context = serializers.SerializerMethodField()
    local_context_schema = NestedConfigContextSchemaSerializer(required=False, allow_null=True)

    class Meta(VirtualMachineSerializer.Meta):
        fields = [
            "id",
            "url",
            "name",
            "status",
            "site",
            "cluster",
            "role",
            "tenant",
            "platform",
            "primary_ip",
            "primary_ip4",
            "primary_ip6",
            "vcpus",
            "memory",
            "disk",
            "comments",
            "local_context_data",
            "local_context_schema",
            "tags",
            "custom_fields",
            "config_context",
            "created",
            "last_updated",
        ]

    @swagger_serializer_method(serializer_or_field=serializers.DictField)
    def get_config_context(self, obj):
        return obj.get_config_context()

#
# VM interfaces
#

class VMInterfaceSerializer(TaggedObjectSerializer, ValidatedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="virtualization-api:vminterface-detail")
    virtual_machine = NestedVirtualMachineSerializer()
    mode = ChoiceField(choices=InterfaceModeChoices, allow_blank=True, required=False)
    untagged_vlan = NestedVLANSerializer(required=False, allow_null=True)
    tagged_vlans = SerializedPKRelatedField(
        queryset=VLAN.objects.all(),
        serializer=NestedVLANSerializer,
        required=False,
        many=True,
    )

    class Meta:
        model = VMInterface
        fields = [
            "id",
            "url",
            "virtual_machine",
            "name",
            "enabled",
            "mtu",
            "mac_address",
            "description",
            "mode",
            "untagged_vlan",
            "tagged_vlans",
            "tags",
        ]

    def validate(self, data):

        # Validate many-to-many VLAN assignments
        virtual_machine = self.instance.virtual_machine if self.instance else data.get("virtual_machine")
        for vlan in data.get("tagged_vlans", []):
            if vlan.site not in [virtual_machine.site, None]:
                raise serializers.ValidationError(
                    {
                        "tagged_vlans": f"VLAN {vlan} must belong to the same site as the interface's parent virtual "
                        f"machine, or it must be global."
                    }
                )

        return super().validate(data)